Update tests for improved range function

This commit is contained in:
Joanne 2018-02-13 22:39:21 +08:00 committed by Antonin Delpeuch
parent 03d80598aa
commit e2793daf54

View File

@ -45,12 +45,12 @@ public class RangeTests extends RefineTest {
// registry uses static initializer, so no need to set it up // registry uses static initializer, so no need to set it up
Function function = ControlFunctionRegistry.getFunction(name); Function function = ControlFunctionRegistry.getFunction(name);
if (function == null) { if (function == null) {
throw new IllegalArgumentException("Unknown function "+name); throw new IllegalArgumentException("Unknown function " + name);
} }
if (args == null) { if (args == null) {
return function.call(bindings,new Object[0]); return function.call(bindings, new Object[0]);
} else { } else {
return function.call(bindings,args); return function.call(bindings, args);
} }
} }
@ -60,68 +60,84 @@ public class RangeTests extends RefineTest {
Assert.assertTrue(invoke("range") instanceof EvalError); Assert.assertTrue(invoke("range") instanceof EvalError);
Assert.assertTrue(invoke("range", "") instanceof EvalError); Assert.assertTrue(invoke("range", "") instanceof EvalError);
Assert.assertTrue(invoke("range", "one","two","three") instanceof EvalError); Assert.assertTrue(invoke("range", "one","two","three") instanceof EvalError);
Assert.assertTrue(invoke("range", 1) instanceof EvalError); Assert.assertTrue(invoke("range", 1, 2, 3, 4) instanceof EvalError);
Assert.assertTrue(invoke("range", 1, 2, 3) instanceof EvalError);
// Test invalid single string arguments with dash separator // Test invalid single string argument types
Assert.assertTrue(invoke("range", "null") instanceof EvalError);
Assert.assertTrue(invoke("range", "a") instanceof EvalError); Assert.assertTrue(invoke("range", "a") instanceof EvalError);
Assert.assertTrue(invoke("range", "-") instanceof EvalError);
Assert.assertTrue(invoke("range", "a-") instanceof EvalError);
Assert.assertTrue(invoke("range", "-b") instanceof EvalError);
Assert.assertTrue(invoke("range", "a-b") instanceof EvalError);
Assert.assertTrue(invoke("range", "1") instanceof EvalError); // Test invalid single string numeric arguments
Assert.assertTrue(invoke("range", "-") instanceof EvalError); Assert.assertTrue(invoke("range", "-1") instanceof EvalError);
Assert.assertTrue(invoke("range", "1-") instanceof EvalError);
Assert.assertTrue(invoke("range", "-3") instanceof EvalError);
// Test invalid single string arguments with comma separator
Assert.assertTrue(invoke("range", ",") instanceof EvalError);
Assert.assertTrue(invoke("range", "a,") instanceof EvalError);
Assert.assertTrue(invoke("range", ",b") instanceof EvalError);
Assert.assertTrue(invoke("range", "a,b") instanceof EvalError);
Assert.assertTrue(invoke("range", "1") instanceof EvalError);
Assert.assertTrue(invoke("range", ",") instanceof EvalError);
Assert.assertTrue(invoke("range", "1,") instanceof EvalError); Assert.assertTrue(invoke("range", "1,") instanceof EvalError);
Assert.assertTrue(invoke("range", ",3") instanceof EvalError); Assert.assertTrue(invoke("range", ",") instanceof EvalError);
Assert.assertTrue(invoke("range", ",2") instanceof EvalError);
Assert.assertTrue(invoke("range", "1.5") instanceof EvalError);
Assert.assertTrue(invoke("range", ",12.3, 2") instanceof EvalError);
// Test invalid numeric arguments // Test invalid numeric arguments
Assert.assertTrue(invoke("range", 1.2) instanceof EvalError);
Assert.assertTrue(invoke("range", 1.2, 4.5) instanceof EvalError); Assert.assertTrue(invoke("range", 1.2, 4.5) instanceof EvalError);
Assert.assertTrue(invoke("range", -5, 1) instanceof EvalError); Assert.assertTrue(invoke("range", 1.2, 5, 3) instanceof EvalError);
Assert.assertTrue(invoke("range", -1, 5) instanceof EvalError); Assert.assertTrue(invoke("range", -1) instanceof EvalError);
Assert.assertTrue(invoke("range", -1, 2) instanceof EvalError);
Assert.assertTrue(invoke("range", -1, 5, 3) instanceof EvalError);
// Test invalid mixed arguments
Assert.assertTrue(invoke("range", 1, "2, 3") instanceof EvalError);
Assert.assertTrue(invoke("range", 1, "") instanceof EvalError);
} }
@Test @Test
public void testRangeValidStringParams() { public void testRangeValidStringParams() {
// Test valid string arguments with dash separator // Test valid single string argument
Assert.assertEquals((String)(invoke("range", "1-1")),"1"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "3"))), "0, 1, 2, 3");
Assert.assertEquals((String)(invoke("range", "1-5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", " 3 "))), "0, 1, 2, 3");
Assert.assertEquals((String)(invoke("range", "5-1")), "5, 4, 3, 2, 1");
Assert.assertEquals((String)(invoke("range", " 1-5")), "1, 2, 3, 4, 5"); // Test valid double string arguments
Assert.assertEquals((String)(invoke("range", "1 -5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 1"))), "1");
Assert.assertEquals((String)(invoke("range", "1- 5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 5"))), "1, 2, 3, 4, 5");
Assert.assertEquals((String)(invoke("range", "1-5 ")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "5, 1"))), "5, 4, 3, 2, 1");
Assert.assertEquals((String)(invoke("range", "1 - 5")), "1, 2, 3, 4, 5");
// Test valid string arguments with comma separator // Test valid triple string arguments
Assert.assertEquals((String)(invoke("range", "1,1")), "1"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 3, 0"))), "");
Assert.assertEquals((String)(invoke("range", "1,5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 1, 1"))), "1");
Assert.assertEquals((String)(invoke("range", "5,1")), "5, 4, 3, 2, 1"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 5, 10"))), "1");
Assert.assertEquals((String)(invoke("range", " 1,5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 5, 2"))), "1, 3, 5");
Assert.assertEquals((String)(invoke("range", "1 -5")), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "5, 1, 2"))), "5, 3, 1");
Assert.assertEquals((String)(invoke("range", "1- 5")), "1, 2, 3, 4, 5");
Assert.assertEquals((String)(invoke("range", "1-5 ")), "1, 2, 3, 4, 5");
Assert.assertEquals((String)(invoke("range", "1 - 5")), "1, 2, 3, 4, 5");
} }
@Test @Test
public void testRangeValidIntegerParams() { public void testRangeValidIntegerParams() {
Assert.assertEquals((String)(invoke("range", 1, 1)), "1"); // Test valid single integer argument
Assert.assertEquals((String)(invoke("range", 1, 5)), "1, 2, 3, 4, 5"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 0))), "0");
Assert.assertEquals((String)(invoke("range", 5, 1)), "5, 4, 3, 2, 1"); Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 5))), "0, 1, 2, 3, 4, 5");
// Test valid double integer arguments
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 1, 5))), "1, 2, 3, 4, 5");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 5, 1))), "5, 4, 3, 2, 1");
// Test valid triple integer arguments
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 1, 5, 0))), "");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 1, 5, 2))), "1, 3, 5");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 5, 1, 2))), "5, 3, 1");
} }
@Test
public void testRangeValidMixedParams() {
// Test when each argument is of type integer/ string
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1", 5))), "1, 2, 3, 4, 5");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "5", 1))), "5, 4, 3, 2, 1");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 1, "5"))), "1, 2, 3, 4, 5");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", 5, "1"))), "5, 4, 3, 2, 1");
// Test when step is an integer (e.g. range(value, 1))
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 3", 0))), "");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 1", 1))), "1");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 5", 10))), "1");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "1, 5", 2))), "1, 3, 5");
Assert.assertEquals(String.join(", ", (String[]) (invoke("range", "5, 1", 2))), "5, 3, 1");
}
} }