From 0e86619d86ffc98f9482f822cc4fa255091c44ef Mon Sep 17 00:00:00 2001 From: Antoine2711 Date: Fri, 24 Apr 2020 03:41:59 -0400 Subject: [PATCH] Fix the true.type() == "boolean" Fix the true.type() == "boolean" instead of java.lang.Boolean. Remove all the references to "error" result in Type(). This will be addressed in: @ToDo fix this with issue #2562 --- .../google/refine/expr/functions/Type.java | 8 ++- .../refine/expr/functions/TypeTests.java | 72 ++++++++++++++++++- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/main/src/com/google/refine/expr/functions/Type.java b/main/src/com/google/refine/expr/functions/Type.java index 6c01a6071..d8abc465c 100644 --- a/main/src/com/google/refine/expr/functions/Type.java +++ b/main/src/com/google/refine/expr/functions/Type.java @@ -57,8 +57,10 @@ public class Type implements Function { return "number"; } else if (v.getClass().isArray() || v instanceof List) { return "array"; - } else if (v instanceof EvalError) { - return "error"; + } else if (v instanceof Boolean) { + return "boolean"; +// } else if (v instanceof EvalError) { // @ToDo fix this with issue #2562 +// return "error"; } else { return v.getClass().getName(); } @@ -70,7 +72,7 @@ public class Type implements Function { @Override public String getDescription() { - return "Returns the type of o"; + return "Returns the type of o as a string ('string', 'date', 'number', 'array', 'boolean' or a class name)"; } @Override diff --git a/main/tests/server/src/com/google/refine/expr/functions/TypeTests.java b/main/tests/server/src/com/google/refine/expr/functions/TypeTests.java index d007b577c..4e18408ea 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/TypeTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/TypeTests.java @@ -26,15 +26,83 @@ ******************************************************************************/ package com.google.refine.expr.functions; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; + +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +import com.google.refine.RefineTest; +import com.google.refine.expr.EvalError; import com.google.refine.expr.functions.Type; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; import com.google.refine.util.TestUtils; -public class TypeTests { + +public class TypeTests extends RefineTest { + private static Properties bindings; + static final List listArray = Arrays.asList("v1", "v2", "v3"); + private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); + + @Override + @BeforeTest + public void init() { + logger = LoggerFactory.getLogger(this.getClass()); + } + + @BeforeMethod + public void setUp() { + bindings = new Properties(); + } + + @AfterMethod + public void tearDown() { + bindings = null; + } + + /** + * Lookup a control function by name and invoke it with a variable number of args + */ + private static Object invoke(String name,Object... args) { + // registry uses static initializer, so no need to set it up + Function function = ControlFunctionRegistry.getFunction(name); + if (function == null) { + throw new IllegalArgumentException("Unknown function "+name); + } + if (args == null) { + return function.call(bindings,new Object[0]); + } else { + return function.call(bindings,args); + } + } + + @Test + public void testTypeInvalidParams() { + Assert.assertTrue(invoke("type") instanceof EvalError); + } + + @Test + public void testType() { + Assert.assertEquals(invoke("type", (Object) null),"undefined"); + Assert.assertEquals(invoke("type", 1),"number"); + Assert.assertEquals(invoke("type", true),"boolean"); + Assert.assertEquals(invoke("type", "a string"),"string"); + Assert.assertEquals(invoke("type", dateTimeValue), "date"); + Assert.assertEquals(invoke("type", listArray), "array"); + } + @Test public void serializeType() { - String json = "{\"description\":\"Returns the type of o\",\"params\":\"object o\",\"returns\":\"string\"}"; + String json = "{\"description\":\"Returns the type of o as a string ('string', 'date', 'number', 'array', 'boolean' or a class name)\",\"params\":\"object o\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new Type(), json); } }