Merge pull request #2586 from OpenRefine/issue-2510-type-boolean

Support "boolean" return for type() function.  Closes #2510
This commit is contained in:
Tom Morris 2020-05-18 17:24:47 -04:00 committed by GitHub
commit 557ffad920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 5 deletions

View File

@ -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

View File

@ -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<String> 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);
}
}