From 990540ce1087e35e7aafe2071ce030713ab4f9f5 Mon Sep 17 00:00:00 2001 From: rachittiwari8562 <54476275+rachittiwari8562@users.noreply.github.com> Date: Sun, 22 Nov 2020 13:53:06 +0530 Subject: [PATCH] Fix #3330: argument checking in phonetic GREL function (#3345) * Fix for issue #3330 phonetic-function * Update main/src/com/google/refine/expr/functions/strings/Phonetic.java Co-authored-by: Antonin Delpeuch * Corrected Intendation Corrected intendation as suggested. * Added tests to check invalid parameters * Added tests to check invalid parameters Co-authored-by: Antonin Delpeuch --- .../refine/expr/functions/strings/Phonetic.java | 5 ++++- .../expr/functions/strings/PhoneticTests.java | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/main/src/com/google/refine/expr/functions/strings/Phonetic.java b/main/src/com/google/refine/expr/functions/strings/Phonetic.java index 7c2758fe1..8b1887948 100644 --- a/main/src/com/google/refine/expr/functions/strings/Phonetic.java +++ b/main/src/com/google/refine/expr/functions/strings/Phonetic.java @@ -72,7 +72,10 @@ public class Phonetic implements Function { return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string for the second argument"); } - } + } else { + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + + " expects a string for the second argument, the phonetic encoding to use."); + } } if (args.length < 3) { if ("doublemetaphone".equalsIgnoreCase(encoding)) { diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/PhoneticTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/PhoneticTests.java index de4aa0505..1281c868c 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/PhoneticTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/strings/PhoneticTests.java @@ -25,16 +25,26 @@ * POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ package com.google.refine.expr.functions.strings; - +import org.testng.Assert; import org.testng.annotations.Test; - +import com.google.refine.RefineTest; import com.google.refine.util.TestUtils; +import com.google.refine.expr.EvalError; -public class PhoneticTests { +public class PhoneticTests extends RefineTest { @Test public void serializePhonetic() { String json = "{\"description\":\"Returns the a phonetic encoding of s (optionally indicating which encoding to use')\",\"params\":\"string s, string encoding (optional, defaults to 'metaphone3')\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new Phonetic(), json); } + + @Test + public void testtoPhoneticInvalidParams() { + Assert.assertTrue(invoke("phonetic") instanceof EvalError); //if no arguments are provided + Assert.assertTrue(invoke("phonetic",(Object[])null) instanceof EvalError); //if first argument(value) is null + Assert.assertTrue(invoke("phonetic","one",(Object[])null) instanceof EvalError); //if second argument(encoding type) is null + Assert.assertTrue(invoke("phonetic","one","other") instanceof EvalError); //if second argument(encoding type) is not a valid encoding type + Assert.assertTrue(invoke("phonetic","one","metaphone3","three") instanceof EvalError); //if more than 2 arguments are provided + } }