Merge pull request #1189 from BobHarper1/master

allow more than 2 AND and OR conditions
This commit is contained in:
Jacky 2017-04-29 09:58:08 -04:00 committed by GitHub
commit c5f9e6ab95
4 changed files with 48 additions and 48 deletions

View File

@ -23,8 +23,8 @@ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ -46,7 +46,7 @@ public class And implements Function {
@Override @Override
public Object call(Properties bindings, Object[] args) { 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) { for (Object o : args) {
if (!Not.objectToBoolean(o)) { if (!Not.objectToBoolean(o)) {
return false; return false;
@ -54,15 +54,15 @@ public class And implements Function {
} }
return true; return true;
} }
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two booleans"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two or more booleans");
} }
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); 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("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean"); writer.key("returns"); writer.value("boolean");
writer.endObject(); writer.endObject();

View File

@ -23,8 +23,8 @@ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ -46,7 +46,7 @@ public class Or implements Function {
@Override @Override
public Object call(Properties bindings, Object[] args) { 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) { for (Object o : args) {
if (Not.objectToBoolean(o)) { if (Not.objectToBoolean(o)) {
return true; return true;
@ -54,15 +54,15 @@ public class Or implements Function {
} }
return false; return false;
} }
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two booleans"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects two or more booleans");
} }
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); 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("params"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.value("boolean"); writer.key("returns"); writer.value("boolean");
writer.endObject(); writer.endObject();

View File

@ -38,10 +38,10 @@ public class Xor implements Function {
@Override @Override
public Object call(Properties bindings, Object[] args) { 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 (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 @Override
@ -50,7 +50,7 @@ public class Xor implements Function {
writer.object(); writer.object();
writer.key("description"); writer.key("description");
writer.value("XORs two boolean values"); writer.value("XORs two or more boolean values");
writer.key("params"); writer.key("params");
writer.value("boolean a, boolean b"); writer.value("boolean a, boolean b");
writer.key("returns"); writer.key("returns");

View File

@ -23,14 +23,14 @@ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.google.refine.tests.expr.functions.booleans; package com.google.refine.tests.expr.functions.booleans;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -66,40 +66,40 @@ import com.google.refine.tests.util.TestUtils;
public class BooleanTests extends RefineTest { public class BooleanTests extends RefineTest {
private static String TRUTH_TABLE[][] = { private static String TRUTH_TABLE[][] = {
{"and","true","true","true"}, {"and","true","true","true","true"},
{"and","false","false","false"}, {"and","false","false","false","false"},
{"and","true","false","false"}, {"and","true","false","false","false"},
{"and","false","true","false"}, {"and","false","true","true","false"},
{"or","true","true","true"}, {"or","true","true","true","true"},
{"or","false","false","false"}, {"or","false","false","false","false"},
{"or","true","false","true"}, {"or","true","false","false","true"},
{"or","false","true","true"}, {"or","false","true","true","true"},
{"xor","true","true","false"}, {"xor","true","true","true","false"},
{"xor","false","false","false"}, {"xor","false","false","false","false"},
{"xor","true","false","true"}, {"xor","true","false","false","true"},
{"xor","false","true","true"}, {"xor","false","true","false","true"},
}; };
static Properties bindings; static Properties bindings;
Project project; Project project;
Properties options; Properties options;
JSONObject engine_config; JSONObject engine_config;
Engine engine; Engine engine;
@Override @Override
@BeforeTest @BeforeTest
public void init() { public void init() {
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
} }
@BeforeMethod @BeforeMethod
public void SetUp() throws IOException, ModelException { public void SetUp() throws IOException, ModelException {
bindings = new Properties(); bindings = new Properties();
File dir = TestUtils.createTempDirectory("openrefine-test-workspace-dir"); File dir = TestUtils.createTempDirectory("openrefine-test-workspace-dir");
FileProjectManager.initialize(dir); FileProjectManager.initialize(dir);
project = new Project(); project = new Project();
@ -110,11 +110,11 @@ public class BooleanTests extends RefineTest {
int index = project.columnModel.allocateNewCellIndex(); int index = project.columnModel.allocateNewCellIndex();
Column column = new Column(index,"Column A"); Column column = new Column(index,"Column A");
project.columnModel.addColumn(index, column, true); project.columnModel.addColumn(index, column, true);
options = mock(Properties.class); options = mock(Properties.class);
bindings.put("project", project); bindings.put("project", project);
// Five rows of a's and five of 1s // Five rows of a's and five of 1s
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
Row row = new Row(1); Row row = new Row(1);
@ -128,7 +128,7 @@ public class BooleanTests extends RefineTest {
public void TearDown() { public void TearDown() {
bindings = null; bindings = null;
} }
/** /**
* Lookup a control function by name and invoke it with a variable number of args * Lookup a control function by name and invoke it with a variable number of args
*/ */
@ -144,12 +144,11 @@ public class BooleanTests extends RefineTest {
return function.call(bindings,args); return function.call(bindings,args);
} }
} }
@Test @Test
public void testInvalidParams() { public void testInvalidParams() {
for (String op : new String[] {"and","or","xor"}) { for (String op : new String[] {"and","or","xor"}) {
Assert.assertTrue(invoke(op) instanceof EvalError); 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, Boolean.TRUE, Integer.valueOf(1)) instanceof EvalError);
Assert.assertTrue(invoke(op, Integer.valueOf(1), Boolean.TRUE) instanceof EvalError); Assert.assertTrue(invoke(op, Integer.valueOf(1), Boolean.TRUE) instanceof EvalError);
Assert.assertTrue(invoke(op, Boolean.TRUE,"foo") instanceof EvalError); Assert.assertTrue(invoke(op, Boolean.TRUE,"foo") instanceof EvalError);
@ -158,19 +157,20 @@ public class BooleanTests extends RefineTest {
} }
String op = "not"; String op = "not";
Assert.assertTrue(invoke(op) instanceof EvalError); 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, Integer.valueOf(1)) instanceof EvalError);
Assert.assertTrue(invoke(op, "foo") instanceof EvalError); Assert.assertTrue(invoke(op, "foo") instanceof EvalError);
} }
@Test @Test
public void testBinary() { public void testBinary() {
for (String[] test : TRUTH_TABLE) { for (String[] test : TRUTH_TABLE) {
String operator = test[0]; String operator = test[0];
Boolean op1 = Boolean.valueOf(test[1]); Boolean op1 = Boolean.valueOf(test[1]);
Boolean op2 = Boolean.valueOf(test[2]); Boolean op2 = Boolean.valueOf(test[2]);
Boolean result = Boolean.valueOf(test[3]); Boolean op3 = Boolean.valueOf(test[3]);
Assert.assertEquals(invoke(operator, op1, op2),result); 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.TRUE),Boolean.FALSE);
Assert.assertEquals(invoke("not", Boolean.FALSE),Boolean.TRUE); Assert.assertEquals(invoke("not", Boolean.FALSE),Boolean.TRUE);