xor can also accept 2+ params, rewrite tests to be consistent

This commit is contained in:
Bob Harper 2017-04-27 11:20:48 +01:00
parent 4ea0f440e9
commit 909df1b6a7
2 changed files with 18 additions and 18 deletions

View File

@ -38,10 +38,10 @@ public class Xor implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 && args[0] instanceof Boolean && args[1] instanceof Boolean) {
if (args.length >= 2 && args[0] instanceof Boolean && args[1] instanceof Boolean) {
return (Boolean) args[0] ^ (Boolean) args[1];
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 booleans");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 or more booleans");
}
@Override
@ -50,7 +50,7 @@ public class Xor implements Function {
writer.object();
writer.key("description");
writer.value("XORs two boolean values");
writer.value("XORs two or more boolean values");
writer.key("params");
writer.value("boolean a, boolean b");
writer.key("returns");

View File

@ -66,20 +66,20 @@ import com.google.refine.tests.util.TestUtils;
public class BooleanTests extends RefineTest {
private static String TRUTH_TABLE[][] = {
{"and","true","true","true"},
{"and","false","false","false"},
{"and","true","false","false"},
{"and","false","true","false"},
{"and","true","true","true","true"},
{"and","false","false","false","false"},
{"and","true","false","false","false"},
{"and","false","true","true","false"},
{"or","true","true","true"},
{"or","false","false","false"},
{"or","true","false","true"},
{"or","false","true","true"},
{"or","true","true","true","true"},
{"or","false","false","false","false"},
{"or","true","false","false","true"},
{"or","false","true","true","true"},
{"xor","true","true","false"},
{"xor","false","false","false"},
{"xor","true","false","true"},
{"xor","false","true","true"},
{"xor","true","true","true","false"},
{"xor","false","false","false","false"},
{"xor","true","false","false","true"},
{"xor","false","true","false","true"},
};
@ -155,7 +155,6 @@ public class BooleanTests extends RefineTest {
Assert.assertTrue(invoke(op, "foo", Boolean.TRUE) instanceof EvalError);
Assert.assertTrue(invoke(op, Boolean.TRUE) instanceof EvalError);
}
Assert.assertTrue(invoke("xor", Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) instanceof EvalError);
String op = "not";
Assert.assertTrue(invoke(op) instanceof EvalError);
Assert.assertTrue(invoke(op, Boolean.TRUE, Boolean.TRUE) instanceof EvalError);
@ -169,8 +168,9 @@ public class BooleanTests extends RefineTest {
String operator = test[0];
Boolean op1 = Boolean.valueOf(test[1]);
Boolean op2 = Boolean.valueOf(test[2]);
Boolean result = Boolean.valueOf(test[3]);
Assert.assertEquals(invoke(operator, op1, op2),result);
Boolean op3 = Boolean.valueOf(test[3]);
Boolean result = Boolean.valueOf(test[4]);
Assert.assertEquals(invoke(operator, op1, op2, op3),result);
}
Assert.assertEquals(invoke("not", Boolean.TRUE),Boolean.FALSE);
Assert.assertEquals(invoke("not", Boolean.FALSE),Boolean.TRUE);