Add some string function tests

Including a test for Apache TEXT-149 behavior change
https://github.com/apache/commons-text/pull/119

Add some more string function tests
This commit is contained in:
Tom Morris 2020-08-22 18:57:05 -04:00
parent b5aea3b780
commit 441c069bc5
10 changed files with 129 additions and 40 deletions

View File

@ -48,9 +48,9 @@ public class ToStringTests extends RefineTest {
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");
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");
@ -61,8 +61,8 @@ public class ToStringTests extends RefineTest {
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");
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");
}
}

View File

@ -26,15 +26,29 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;
public class ChompTests {
public class ChompTests extends RefineTest {
@Test
public void serializeChomp() {
String json = "{\"description\":\"Removes separator from the end of str if it's there, otherwise leave it alone.\",\"params\":\"string str, string separator\",\"returns\":\"string\"}";
TestUtils.isSerializedTo(new Chomp(), json);
}
@Test
public void testChomp() {
// FIXME: These return null instead of an EvalError
// assertTrue(invoke("chomp") instanceof EvalError);
// assertTrue(invoke("chomp", "") instanceof EvalError);
// assertTrue(invoke("chomp", 1, 1) instanceof EvalError);
assertEquals(invoke("chomp", "test,", ","), "test");
assertEquals(invoke("chomp", "test,", ":"), "test,");
}
}

View File

@ -26,15 +26,25 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;
public class EndsWithTests {
public class EndsWithTests extends RefineTest {
@Test
public void serializeEndsWith() {
String json = "{\"description\":\"Returns whether s ends with sub\",\"params\":\"string s, string sub\",\"returns\":\"boolean\"}";
TestUtils.isSerializedTo(new EndsWith(), json);
}
@Test
public void testStartsWith() {
assertTrue((Boolean) invoke("endsWith", "testString", "ing"));
assertFalse((Boolean) invoke("startsWith", "testString", "banana"));
}
}

View File

@ -44,27 +44,31 @@ public class EscapeTests extends RefineTest {
@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");
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");
assertEquals("\",\"", invoke("escape", ",", "csv")); // commas get quoted
assertEquals("\"\n\"", invoke("escape", "\n", "csv")); // newlines get quoted
assertEquals("\"\"\"\"", invoke("escape", "\"", "csv")); // quotes get doubled
}
}

View File

@ -26,15 +26,34 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.expr.EvalError;
import com.google.refine.util.TestUtils;
public class ReplaceTests {
public class ReplaceTests extends RefineTest {
@Test
public void serializeReplace() {
String json = "{\"description\":\"Returns the string obtained by replacing f with r in s\",\"params\":\"string s, string or regex f, string r\",\"returns\":\"string\"}";
TestUtils.isSerializedTo(new Replace(), json);
}
@Test
public void testReplace() {
assertTrue(invoke("replace") instanceof EvalError);
assertTrue(invoke("replace", "test") instanceof EvalError);
assertTrue(invoke("replace", "test", "test") instanceof EvalError);
assertTrue(invoke("replace", "test", "test", null) instanceof EvalError);
assertEquals(invoke("replace", "", "ripe", "green"), "");
assertEquals(invoke("replace", "", "", ""), "");
assertEquals(invoke("replace", "ripe banana", "ripe", "green"), "green banana");
assertEquals(invoke("replace", "ripe banana", "ripe", ""), " banana");
assertEquals(invoke("replace", "ripe banana", "wrong", "green"), "ripe banana");
assertEquals(invoke("replace", "ripe ripe banana", "ripe", "green"), "green green banana");
}
}

View File

@ -26,15 +26,26 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;
public class SplitTests {
public class SplitTests extends RefineTest {
@Test
public void serializeSplit() {
String json = "{\"description\":\"Returns the array of strings obtained by splitting s with separator sep. If preserveAllTokens is true, then empty segments are preserved.\",\"params\":\"string s, string or regex sep, optional boolean preserveAllTokens\",\"returns\":\"array\"}";
TestUtils.isSerializedTo(new Split(), json);
}
@Test
public void testSplit() {
assertEquals(invoke("split", "a,,b,c,d", ","), new String[] {"a", "b", "c", "d"});
assertEquals(invoke("split", "a,,b,c,d", ",", true), new String[] {"a", "", "b", "c", "d"});
assertEquals(invoke("split", "", ","), new String[] {});
assertEquals(invoke("split", ",,,", ","), new String[] {""}); // TODO: Should this return an empty array?
}
}

View File

@ -26,15 +26,25 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;
public class StartsWithTests {
public class StartsWithTests extends RefineTest {
@Test
public void serializeStartsWith() {
String json = "{\"description\":\"Returns whether s starts with sub\",\"params\":\"string s, string sub\",\"returns\":\"boolean\"}";
TestUtils.isSerializedTo(new StartsWith(), json);
}
@Test
public void testStartsWith() {
assertTrue((Boolean) invoke("startsWith", "testString", "test"));
assertFalse((Boolean) invoke("startsWith", "testString", "banana"));
}
}

View File

@ -26,15 +26,34 @@
******************************************************************************/
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.expr.EvalError;
import com.google.refine.util.TestUtils;
public class ToLowercaseTests {
public class ToLowercaseTests extends RefineTest {
@Test
public void serializeToLowercase() {
String json = "{\"description\":\"Returns s converted to lowercase\",\"params\":\"string s\",\"returns\":\"string\"}";
TestUtils.isSerializedTo(new ToLowercase(), json);
}
@Test
public void testtoLowercaseInvalidParams() {
Assert.assertTrue(invoke("toLowercase") instanceof EvalError);
Assert.assertTrue(invoke("toLowercase", (Object[])null) instanceof EvalError);
Assert.assertTrue(invoke("toLowercase", "one", "two", "three") instanceof EvalError);
}
@Test
public void testtoLowercase() {
Assert.assertEquals((String)(invoke("toLowercase", "One")), "one");
Assert.assertEquals((String)(invoke("toLowercase", "Ône")), "ône");
Assert.assertEquals((String)(invoke("toLowercase", "ONE")), "one");
Assert.assertEquals((String)(invoke("toLowercase", 1)), "1");
Assert.assertEquals((String)(invoke("toLowercase", true)), "true");
}
}

View File

@ -26,8 +26,6 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import java.util.Properties;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
@ -40,7 +38,6 @@ import com.google.refine.util.TestUtils;
public class TrimTests extends RefineTest {
static Properties bindings;
private static String NBSP = "\u00A0";
private static String ENQUAD = "\u2000";
private static String EMQUAD = "\u2001";

View File

@ -26,7 +26,7 @@
******************************************************************************/
package com.google.refine.expr.functions.strings;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
@ -42,8 +42,13 @@ public class UnescapeTests extends RefineTest {
@Test
public void testUnescape() {
assertEquals(invoke("unescape", "Ä", "html"),"Ä");
assertEquals(invoke("unescape", "\\u00C4", "javascript"),"Ä");
assertEquals(invoke("unescape", "Ä", "html"), "Ä");
assertEquals(invoke("unescape", "\\u00C4", "javascript"), "Ä");
assertEquals(invoke("unescape", "\"Test\"", "csv"), "Test"); // Apache TEXT-149 https://github.com/apache/commons-text/pull/119
assertEquals(invoke("unescape", "\"This \"\"is\"\" a test\"", "csv"), "This \"is\" a test");
assertEquals(invoke("unescape", "\"\n\"", "csv"), "\n");
assertEquals(invoke("unescape", "\"a, b\"", "csv"), "a, b");
}
}