diff --git a/main/tests/server/src/com/google/refine/expr/functions/ToDateTests.java b/main/tests/server/src/com/google/refine/expr/functions/ToDateTests.java index 8a9622a78..49c57c17f 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/ToDateTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/ToDateTests.java @@ -26,16 +26,85 @@ ******************************************************************************/ package com.google.refine.expr.functions; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.time.OffsetDateTime; +import java.util.TimeZone; + import org.testng.annotations.Test; -import com.google.refine.expr.functions.ToDate; +import com.google.refine.RefineTest; +import com.google.refine.expr.EvalError; +import com.google.refine.expr.util.CalendarParser; +import com.google.refine.expr.util.CalendarParserException; import com.google.refine.util.TestUtils; -public class ToDateTests { +public class ToDateTests extends RefineTest { @Test public void serializeToDate() { String json = "{\"description\":\"Returns o converted to a date object, you can hint if the day or the month is listed first, or give an ordered list of possible formats using this syntax: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html\",\"params\":\"o, boolean month_first / format1, format2, ... (all optional)\",\"returns\":\"date\"}"; TestUtils.isSerializedTo(new ToDate(), json); } + + @Test + public void testToDate() throws CalendarParserException { + TimeZone originalTimeZone = TimeZone.getDefault(); + try { + // Inject a fixed non-UTC timezone + TimeZone.setDefault(TimeZone.getTimeZone("JST")); + + assertTrue(invoke("toDate") instanceof EvalError); + assertTrue(invoke("toDate", (Object) null) instanceof EvalError); + assertTrue(invoke("toDate", "") instanceof EvalError); + assertTrue(invoke("toDate", 1.0) instanceof EvalError); + assertTrue(invoke("toDate", "2012-03-01", "xxx") instanceof EvalError); // bad format string + assertTrue(invoke("toDate", "2012-03-01", 1L) instanceof EvalError); // non-string format arg + assertTrue(invoke("toDate", "P1M") instanceof EvalError); // Durations aren't supported + + assertTrue(invoke("toDate", "2012-03-01") instanceof OffsetDateTime); + assertEquals(invoke("toDate", "2012-03-01"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); + //parse as 'month first' date with and without explicit 'true' parameter + assertEquals(invoke("toDate", "01/03/2012"),CalendarParser.parseAsOffsetDateTime("2012-01-03")); + assertEquals(invoke("toDate", "01/03/2012",true),CalendarParser.parseAsOffsetDateTime("2012-01-03")); + //parse as 'month first' date with 'false' parameter + assertEquals(invoke("toDate", "01/03/2012",false),CalendarParser.parseAsOffsetDateTime("2012-03-01")); + //parse as 'month first' date without 'false' parameter but with format specified + assertEquals(invoke("toDate", "01/03/2012","dd/MM/yyyy"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); + assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); + //Two digit year + assertEquals(invoke("toDate", "02-02-01"),CalendarParser.parseAsOffsetDateTime("2001-02-02")); + // Multiple format strings should get tried sequentially until one succeeds or all are exhausted + assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalendarParser.parseAsOffsetDateTime("2012-03-01")); + + // Boolean argument combined with Multiple format strings + assertEquals(invoke("toDate", "01/03/2012",false, "MMM","yyyy-MM-dd","MM/dd/yyyy"), CalendarParser.parseAsOffsetDateTime("2012-03-01")); + + // First string can be a locale identifier instead of a format string + assertEquals(invoke("toDate", "2013-06-01","zh"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); + assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); + assertEquals(invoke("toDate", "01-六月-2013", "zh-CN", "dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); + assertEquals(invoke("toDate", "01-六月-2013", "zh", "MMM-dd-yyyy", "dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); + + // If a long, convert to string + assertEquals(invoke("toDate", (long) 2012), invoke("toDate", "2012-01-01")); + + // If already a date, leave it alone + assertEquals(invoke("toDate", CalendarParser.parseAsOffsetDateTime("2012-03-01")),CalendarParser.parseAsOffsetDateTime("2012-03-01")); + + // FIXME: Date/times without timezone info were interpreted as local up until May 2018 at which point they switch to UTC + // assertEquals(invoke("toDate", "2013-06-01T13:12:11"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); + + // These match current behavior, but would fail with the historic (pre-2018) behavior + assertEquals(invoke("toDate", "2013-06-01T13:12:11Z"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); + assertEquals(invoke("toDate", "2013-06-01Z"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); + + // TODO: more tests for datetimes with timezones and/or offsets + // assertEquals(invoke("toDate", "2013-06-01T13:12:11+06:00"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); + } finally { + TimeZone.setDefault(originalTimeZone); + } + } + } diff --git a/main/tests/server/src/com/google/refine/expr/functions/ToNumberTests.java b/main/tests/server/src/com/google/refine/expr/functions/ToNumberTests.java index 8274392bf..3942a7232 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/ToNumberTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/ToNumberTests.java @@ -33,11 +33,12 @@ import java.util.Properties; import org.testng.annotations.Test; +import com.google.refine.RefineTest; import com.google.refine.expr.EvalError; import com.google.refine.grel.Function; import com.google.refine.util.TestUtils; -public class ToNumberTests { +public class ToNumberTests extends RefineTest { private static final Double EPSILON = 0.000001; static Properties bindings = new Properties(); @@ -56,5 +57,20 @@ public class ToNumberTests { assertTrue((Double)f.call(bindings, new Object[] {"12345.6789"}) - Double.valueOf(12345.6789) < EPSILON); assertTrue(f.call(bindings, new Object[] {"abc"}) instanceof EvalError); } + + @Test + public void testToNumber() { + assertTrue(invoke("toNumber") instanceof EvalError); + assertTrue(invoke("toNumber", (Object) null) instanceof EvalError); + assertTrue(invoke("toNumber", "") instanceof EvalError); + assertTrue(invoke("toNumber", "string") instanceof EvalError); + assertEquals(invoke("toNumber", "0.0"), 0.0); + assertEquals(invoke("toNumber", "123"), Long.valueOf(123)); + assertTrue(Math.abs((Double) invoke("toNumber", "123.456") - 123.456) < EPSILON); + assertTrue(Math.abs((Double) invoke("toNumber", "001.234") - 1.234) < EPSILON); + assertTrue(Math.abs((Double) invoke("toNumber", "1e2") - 100.0) < EPSILON); + assertTrue(Math.abs((Double) invoke("toNumber", Double.parseDouble("100.0")) - 100.0) < EPSILON); + } + } diff --git a/main/tests/server/src/com/google/refine/expr/functions/ToStringTests.java b/main/tests/server/src/com/google/refine/expr/functions/ToStringTests.java index 7a4111bb8..606180887 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/ToStringTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/ToStringTests.java @@ -26,16 +26,44 @@ ******************************************************************************/ package com.google.refine.expr.functions; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + import org.testng.annotations.Test; -import com.google.refine.expr.functions.ToString; +import com.google.refine.RefineTest; +import com.google.refine.expr.EvalError; +import com.google.refine.expr.util.CalendarParser; +import com.google.refine.expr.util.CalendarParserException; import com.google.refine.util.TestUtils; -public class ToStringTests { +public class ToStringTests extends RefineTest { @Test public void serializeToString() { String json = "{\"description\":\"Returns o converted to a string\",\"params\":\"o, string format (optional)\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new ToString(), json); } + + @Test + public void testToString() throws CalendarParserException { + assertTrue(invoke("toString") instanceof EvalError); + assertEquals(invoke("toString", (Object) null), ""); + assertEquals(invoke("toString", Long.valueOf(100)),"100"); + assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0"); + assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100"); + + String inputDate = "2013-06-01"; + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate)), "2013-06-01T00:00:00Z"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd"), "2013-06-01"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy/dd/MM"), "2013/01/06"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd hh:mm:ss"), "2013-06-01 12:00:00"); + + String inputDateTime = "2013-06-01 13:12:11"; + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime)), "2013-06-01T13:12:11Z"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd"), "2013-06-01"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd hh:mm:ss"),"2013-06-01 01:12:11"); + assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd HH:mm:ss"),"2013-06-01 13:12:11"); + } + } diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/EscapeTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/EscapeTests.java index 1a5ec4231..c8a353dc3 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/EscapeTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/strings/EscapeTests.java @@ -26,16 +26,45 @@ ******************************************************************************/ package com.google.refine.expr.functions.strings; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + import org.testng.annotations.Test; -import com.google.refine.expr.functions.strings.Escape; +import com.google.refine.RefineTest; import com.google.refine.util.TestUtils; -public class EscapeTests { +public class EscapeTests extends RefineTest { @Test public void serializeEscape() { String json = "{\"description\":\"Escapes a string depending on the given escaping mode.\",\"params\":\"string s, string mode ['html','xml','csv','url','javascript']\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new Escape(), json); } + + @Test + public void testEscape() { + assertNull(invoke("escape")); + assertEquals(invoke("escape",null,"xml"),""); + assertEquals(invoke("escape", "mystring", "html"),"mystring"); + assertEquals(invoke("escape", "mystring", "xml"),"mystring"); + assertEquals(invoke("escape", "mystring", "csv"),"mystring"); + assertEquals(invoke("escape", "mystring", "url"),"mystring"); + assertEquals(invoke("escape", "mystring", "javascript"),"mystring"); + assertEquals(invoke("escape", 1, "html"),"1"); + assertEquals(invoke("escape", 1, "xml"),"1"); + assertEquals(invoke("escape", 1, "csv"),"1"); + assertEquals(invoke("escape", 1, "url"),"1"); + assertEquals(invoke("escape", 1, "javascript"),"1"); + assertEquals(invoke("escape", Long.parseLong("1"), "html"),"1"); + assertEquals(invoke("escape", Long.parseLong("1"), "xml"),"1"); + assertEquals(invoke("escape", Long.parseLong("1"), "csv"),"1"); + assertEquals(invoke("escape", Long.parseLong("1"), "url"),"1"); + assertEquals(invoke("escape", Long.parseLong("1"), "javascript"),"1"); + assertEquals(invoke("escape", Double.parseDouble("1.23"), "html"),"1.23"); + assertEquals(invoke("escape", Double.parseDouble("1.23"), "xml"),"1.23"); + assertEquals(invoke("escape", Double.parseDouble("1.23"), "csv"),"1.23"); + assertEquals(invoke("escape", Double.parseDouble("1.23"), "url"),"1.23"); + assertEquals(invoke("escape", Double.parseDouble("1.23"), "javascript"),"1.23"); + } } diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/StringCaseTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/StringCaseTests.java deleted file mode 100644 index 743115f6c..000000000 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/StringCaseTests.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - -Copyright 2011, Thomas F. Morris -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - -package com.google.refine.expr.functions.strings; - -import java.util.Properties; - -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 com.google.refine.RefineTest; -import com.google.refine.expr.EvalError; -import com.google.refine.grel.ControlFunctionRegistry; -import com.google.refine.grel.Function; - - -/** - * Tests for string up/low/title case functions. - * (A very brief start so far) - * - * @author Tom Morris - */ -public class StringCaseTests extends RefineTest { - - static Properties bindings; - - @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 testToTitlecaseInvalidParams() { - Assert.assertTrue(invoke("toTitlecase") instanceof EvalError); - Assert.assertTrue(invoke("toTitlecase", "one","two","three") instanceof EvalError); - } - - @Test - public void testToTitlecase() { - Assert.assertEquals((String)(invoke("toTitlecase", "one")),"One"); - Assert.assertEquals((String)(invoke("toTitlecase", "ONE")),"One"); - Assert.assertEquals((String)(invoke("toTitlecase", "one two three")),"One Two Three"); - Assert.assertEquals((String)(invoke("toTitlecase", "C.R. SANDIDGE WINES, INC.")),"C.R. Sandidge Wines, Inc."); - Assert.assertEquals((String)(invoke("toTitlecase", "C.R. SANDIDGE WINES, INC.",",. ")),"C.R. Sandidge Wines, Inc."); - Assert.assertEquals((String)(invoke("toTitlecase", "one-two-three","-")),"One-Two-Three"); - } - -} diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/ToFromConversionTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/ToFromConversionTests.java deleted file mode 100644 index d2857433d..000000000 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/ToFromConversionTests.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - -Copyright 2012, Thomas F. Morris -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - -package com.google.refine.expr.functions.strings; - -import java.time.OffsetDateTime; -import java.util.Properties; -import java.util.TimeZone; - -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 com.google.refine.RefineTest; -import com.google.refine.expr.EvalError; -import com.google.refine.expr.util.CalendarParser; -import com.google.refine.expr.util.CalendarParserException; -import com.google.refine.grel.ControlFunctionRegistry; -import com.google.refine.grel.Function; - - -/** - * Tests for string up/low/title case functions. - * (A very brief start so far) - * - * @author Tom Morris - */ -public class ToFromConversionTests extends RefineTest { - - private static final double EPSILON = 0.0001; - static Properties bindings; - - @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 testToNumber() { - Assert.assertTrue(invoke("toNumber") instanceof EvalError); - Assert.assertTrue(invoke("toNumber", (Object) null) instanceof EvalError); - Assert.assertTrue(invoke("toNumber", "") instanceof EvalError); - Assert.assertTrue(invoke("toNumber", "string") instanceof EvalError); - Assert.assertEquals(invoke("toNumber", "0.0"), 0.0); - Assert.assertEquals(invoke("toNumber", "123"), Long.valueOf(123)); - Assert.assertTrue(Math.abs((Double) invoke("toNumber", "123.456") - 123.456) < EPSILON); - Assert.assertTrue(Math.abs((Double) invoke("toNumber", "001.234") - 1.234) < EPSILON); - Assert.assertTrue(Math.abs((Double) invoke("toNumber", "1e2") - 100.0) < EPSILON); - Assert.assertTrue(Math.abs((Double) invoke("toNumber", Double.parseDouble("100.0")) - 100.0) < EPSILON); - } - - @Test - public void testToString() throws CalendarParserException { - Assert.assertTrue(invoke("toString") instanceof EvalError); - Assert.assertEquals(invoke("toString", (Object) null), ""); - Assert.assertEquals(invoke("toString", Long.valueOf(100)),"100"); - Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0"); - Assert.assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100"); - - String inputDate = "2013-06-01"; - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate)), "2013-06-01T00:00:00Z"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd"), "2013-06-01"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy/dd/MM"), "2013/01/06"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd hh:mm:ss"), "2013-06-01 12:00:00"); - - String inputDateTime = "2013-06-01 13:12:11"; - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime)), "2013-06-01T13:12:11Z"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd"), "2013-06-01"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd hh:mm:ss"),"2013-06-01 01:12:11"); - Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd HH:mm:ss"),"2013-06-01 13:12:11"); - } - - @Test - public void testToDate() throws CalendarParserException { - TimeZone originalTimeZone = TimeZone.getDefault(); - try { - // Inject a fixed non-UTC timezone - TimeZone.setDefault(TimeZone.getTimeZone("JST")); - - Assert.assertTrue(invoke("toDate") instanceof EvalError); - Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError); - Assert.assertTrue(invoke("toDate", "") instanceof EvalError); - Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError); - Assert.assertTrue(invoke("toDate", "2012-03-01", "xxx") instanceof EvalError); // bad format string - Assert.assertTrue(invoke("toDate", "2012-03-01", 1L) instanceof EvalError); // non-string format arg - Assert.assertTrue(invoke("toDate", "P1M") instanceof EvalError); // Durations aren't supported - - Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof OffsetDateTime); - Assert.assertEquals(invoke("toDate", "2012-03-01"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); - //parse as 'month first' date with and without explicit 'true' parameter - Assert.assertEquals(invoke("toDate", "01/03/2012"),CalendarParser.parseAsOffsetDateTime("2012-01-03")); - Assert.assertEquals(invoke("toDate", "01/03/2012",true),CalendarParser.parseAsOffsetDateTime("2012-01-03")); - //parse as 'month first' date with 'false' parameter - Assert.assertEquals(invoke("toDate", "01/03/2012",false),CalendarParser.parseAsOffsetDateTime("2012-03-01")); - //parse as 'month first' date without 'false' parameter but with format specified - Assert.assertEquals(invoke("toDate", "01/03/2012","dd/MM/yyyy"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); - Assert.assertEquals(invoke("toDate", "2012-03-01","yyyy-MM-dd"),CalendarParser.parseAsOffsetDateTime("2012-03-01")); - //Two digit year - Assert.assertEquals(invoke("toDate", "02-02-01"),CalendarParser.parseAsOffsetDateTime("2001-02-02")); - // Multiple format strings should get tried sequentially until one succeeds or all are exhausted - Assert.assertEquals(invoke("toDate", "2012-03-01","MMM","yyyy-MM-dd"), CalendarParser.parseAsOffsetDateTime("2012-03-01")); - - // Boolean argument combined with Multiple format strings - Assert.assertEquals(invoke("toDate", "01/03/2012",false, "MMM","yyyy-MM-dd","MM/dd/yyyy"), CalendarParser.parseAsOffsetDateTime("2012-03-01")); - - // First string can be a locale identifier instead of a format string - Assert.assertEquals(invoke("toDate", "2013-06-01","zh"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); - Assert.assertEquals(invoke("toDate", "01-六月-2013","zh","dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); - Assert.assertEquals(invoke("toDate", "01-六月-2013", "zh-CN", "dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); - Assert.assertEquals(invoke("toDate", "01-六月-2013", "zh", "MMM-dd-yyyy", "dd-MMM-yyyy"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); - - // If a long, convert to string - Assert.assertEquals(invoke("toDate", (long) 2012), invoke("toDate", "2012-01-01")); - - // If already a date, leave it alone - Assert.assertEquals(invoke("toDate", CalendarParser.parseAsOffsetDateTime("2012-03-01")),CalendarParser.parseAsOffsetDateTime("2012-03-01")); - - // FIXME: Date/times without timezone info were interpreted as local up until May 2018 at which point they switch to UTC - // Assert.assertEquals(invoke("toDate", "2013-06-01T13:12:11"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); - - // These match current behavior, but would fail with the historic (pre-2018) behavior - Assert.assertEquals(invoke("toDate", "2013-06-01T13:12:11Z"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); - Assert.assertEquals(invoke("toDate", "2013-06-01Z"), CalendarParser.parseAsOffsetDateTime("2013-06-01")); - - // TODO: more tests for datetimes with timezones and/or offsets - // Assert.assertEquals(invoke("toDate", "2013-06-01T13:12:11+06:00"), CalendarParser.parseAsOffsetDateTime("2013-06-01 13:12:11")); - } finally { - TimeZone.setDefault(originalTimeZone); - } - } - - @Test - public void testEscape() { - Assert.assertNull(invoke("escape")); - Assert.assertEquals(invoke("escape",null,"xml"),""); - Assert.assertEquals(invoke("escape", "mystring", "html"),"mystring"); - Assert.assertEquals(invoke("escape", "mystring", "xml"),"mystring"); - Assert.assertEquals(invoke("escape", "mystring", "csv"),"mystring"); - Assert.assertEquals(invoke("escape", "mystring", "url"),"mystring"); - Assert.assertEquals(invoke("escape", "mystring", "javascript"),"mystring"); - Assert.assertEquals(invoke("escape", 1, "html"),"1"); - Assert.assertEquals(invoke("escape", 1, "xml"),"1"); - Assert.assertEquals(invoke("escape", 1, "csv"),"1"); - Assert.assertEquals(invoke("escape", 1, "url"),"1"); - Assert.assertEquals(invoke("escape", 1, "javascript"),"1"); - Assert.assertEquals(invoke("escape", Long.parseLong("1"), "html"),"1"); - Assert.assertEquals(invoke("escape", Long.parseLong("1"), "xml"),"1"); - Assert.assertEquals(invoke("escape", Long.parseLong("1"), "csv"),"1"); - Assert.assertEquals(invoke("escape", Long.parseLong("1"), "url"),"1"); - Assert.assertEquals(invoke("escape", Long.parseLong("1"), "javascript"),"1"); - Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "html"),"1.23"); - Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "xml"),"1.23"); - Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "csv"),"1.23"); - Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "url"),"1.23"); - Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "javascript"),"1.23"); - } - - @Test - public void testUnescape() { - Assert.assertEquals(invoke("unescape", "Ä", "html"),"Ä"); - Assert.assertEquals(invoke("unescape", "\\u00C4", "javascript"),"Ä"); - } - -} diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/ToTitlecaseTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/ToTitlecaseTests.java index cc8ddd0c3..e7316c287 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/ToTitlecaseTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/strings/ToTitlecaseTests.java @@ -26,16 +26,35 @@ ******************************************************************************/ package com.google.refine.expr.functions.strings; +import org.testng.Assert; import org.testng.annotations.Test; -import com.google.refine.expr.functions.strings.ToTitlecase; +import com.google.refine.RefineTest; +import com.google.refine.expr.EvalError; import com.google.refine.util.TestUtils; -public class ToTitlecaseTests { +public class ToTitlecaseTests extends RefineTest { @Test public void serializeToTitlecase() { String json = "{\"description\":\"Returns s converted to titlecase\",\"params\":\"string s\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new ToTitlecase(), json); } + + @Test + public void testToTitlecaseInvalidParams() { + Assert.assertTrue(invoke("toTitlecase") instanceof EvalError); + Assert.assertTrue(invoke("toTitlecase", "one","two","three") instanceof EvalError); + } + + @Test + public void testToTitlecase() { + Assert.assertEquals((String)(invoke("toTitlecase", "one")),"One"); + Assert.assertEquals((String)(invoke("toTitlecase", "ONE")),"One"); + Assert.assertEquals((String)(invoke("toTitlecase", "one two three")),"One Two Three"); + Assert.assertEquals((String)(invoke("toTitlecase", "C.R. SANDIDGE WINES, INC.")),"C.R. Sandidge Wines, Inc."); + Assert.assertEquals((String)(invoke("toTitlecase", "C.R. SANDIDGE WINES, INC.",",. ")),"C.R. Sandidge Wines, Inc."); + Assert.assertEquals((String)(invoke("toTitlecase", "one-two-three","-")),"One-Two-Three"); + } + } diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/ToUppercaseTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/ToUppercaseTests.java index b92672d73..fadac139d 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/ToUppercaseTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/strings/ToUppercaseTests.java @@ -28,7 +28,6 @@ package com.google.refine.expr.functions.strings; import org.testng.annotations.Test; -import com.google.refine.expr.functions.strings.ToUppercase; import com.google.refine.util.TestUtils; public class ToUppercaseTests { diff --git a/main/tests/server/src/com/google/refine/expr/functions/strings/UnescapeTests.java b/main/tests/server/src/com/google/refine/expr/functions/strings/UnescapeTests.java index 79e869d50..fb93c21e6 100644 --- a/main/tests/server/src/com/google/refine/expr/functions/strings/UnescapeTests.java +++ b/main/tests/server/src/com/google/refine/expr/functions/strings/UnescapeTests.java @@ -26,16 +26,25 @@ ******************************************************************************/ package com.google.refine.expr.functions.strings; +import static org.testng.AssertJUnit.assertEquals; + import org.testng.annotations.Test; -import com.google.refine.expr.functions.strings.Unescape; +import com.google.refine.RefineTest; import com.google.refine.util.TestUtils; -public class UnescapeTests { +public class UnescapeTests extends RefineTest { @Test public void serializeUnescape() { String json = "{\"description\":\"Unescapes all escaped parts of the string depending on the given escaping mode.\",\"params\":\"string s, string mode ['html','xml','csv','url','javascript']\",\"returns\":\"string\"}"; TestUtils.isSerializedTo(new Unescape(), json); } + + @Test + public void testUnescape() { + assertEquals(invoke("unescape", "Ä", "html"),"Ä"); + assertEquals(invoke("unescape", "\\u00C4", "javascript"),"Ä"); + } + }