Merge pull request #1189 from BobHarper1/master
allow more than 2 AND and OR conditions
This commit is contained in:
commit
c5f9e6ab95
@ -46,7 +46,7 @@ public class And 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) {
|
||||
for (Object o : args) {
|
||||
if (!Not.objectToBoolean(o)) {
|
||||
return false;
|
||||
@ -54,7 +54,7 @@ public class And implements Function {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two booleans");
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two or more booleans");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,7 +62,7 @@ public class And implements Function {
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("ANDs two boolean values");
|
||||
writer.key("description"); writer.value("AND two or more booleans to yield a boolean");
|
||||
writer.key("params"); writer.value("boolean a, boolean b");
|
||||
writer.key("returns"); writer.value("boolean");
|
||||
writer.endObject();
|
||||
|
@ -46,7 +46,7 @@ public class Or 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) {
|
||||
for (Object o : args) {
|
||||
if (Not.objectToBoolean(o)) {
|
||||
return true;
|
||||
@ -54,7 +54,7 @@ public class Or implements Function {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two booleans");
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two or more booleans");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,7 +62,7 @@ public class Or implements Function {
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns a OR b");
|
||||
writer.key("description"); writer.value("OR two or more booleans to yield a boolean");
|
||||
writer.key("params"); writer.value("boolean a, boolean b");
|
||||
writer.key("returns"); writer.value("boolean");
|
||||
writer.endObject();
|
||||
|
@ -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");
|
||||
|
@ -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"},
|
||||
};
|
||||
|
||||
|
||||
@ -149,7 +149,6 @@ public class BooleanTests extends RefineTest {
|
||||
public void testInvalidParams() {
|
||||
for (String op : new String[] {"and","or","xor"}) {
|
||||
Assert.assertTrue(invoke(op) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Boolean.TRUE, Integer.valueOf(1)) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Integer.valueOf(1), Boolean.TRUE) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Boolean.TRUE,"foo") instanceof EvalError);
|
||||
@ -158,7 +157,7 @@ public class BooleanTests extends RefineTest {
|
||||
}
|
||||
String op = "not";
|
||||
Assert.assertTrue(invoke(op) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Boolean.TRUE,Boolean.TRUE) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Boolean.TRUE, Boolean.TRUE) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, Integer.valueOf(1)) instanceof EvalError);
|
||||
Assert.assertTrue(invoke(op, "foo") 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);
|
||||
|
Loading…
Reference in New Issue
Block a user