diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-04-30 11:40:49 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-04-30 11:40:49 +0200 |
commit | 1f592d7f705208cdb9b03066c02411dc5afc2649 (patch) | |
tree | dc9fee22a5ea41fba3170b62882bb9191416e334 /indexinglanguage/src/test | |
parent | 724ae768c90eefd08ffab79d6dafc520abeacc04 (diff) |
Support to_bool
Diffstat (limited to 'indexinglanguage/src/test')
4 files changed, 81 insertions, 0 deletions
diff --git a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java index 0edf0f645a7..49cbc08ae74 100644 --- a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java +++ b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/ScriptTestCase.java @@ -4,6 +4,7 @@ package com.yahoo.vespa.indexinglanguage; import com.yahoo.document.DataType; import com.yahoo.document.Document; import com.yahoo.document.DocumentType; +import com.yahoo.document.datatypes.BoolFieldValue; import com.yahoo.document.datatypes.StringFieldValue; import com.yahoo.vespa.indexinglanguage.expressions.*; import com.yahoo.vespa.indexinglanguage.parser.ParseException; @@ -24,6 +25,7 @@ public class ScriptTestCase { type.addField("in-2", DataType.STRING); type.addField("out-1", DataType.STRING); type.addField("out-2", DataType.STRING); + type.addField("mybool", DataType.BOOL); } @Test @@ -76,4 +78,5 @@ public class ScriptTestCase { assertNotNull(output); assertEquals(new StringFieldValue("foo"), output.getFieldValue("out-1")); } + } diff --git a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ToBoolTestCase.java b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ToBoolTestCase.java new file mode 100644 index 00000000000..5067f0c4c67 --- /dev/null +++ b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/expressions/ToBoolTestCase.java @@ -0,0 +1,76 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.indexinglanguage.expressions; + +import com.yahoo.document.DataType; +import com.yahoo.document.datatypes.BoolFieldValue; +import com.yahoo.document.datatypes.FieldValue; +import com.yahoo.document.datatypes.IntegerFieldValue; +import com.yahoo.document.datatypes.LongFieldValue; +import com.yahoo.document.datatypes.StringFieldValue; +import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter; +import org.junit.Test; + +import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerify; +import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author bratseth + */ +public class ToBoolTestCase { + + @Test + public void requireThatHashCodeAndEqualsAreImplemented() { + Expression exp = new ToBoolExpression(); + assertFalse(exp.equals(new Object())); + assertEquals(exp, new ToBoolExpression()); + assertEquals(exp.hashCode(), new ToBoolExpression().hashCode()); + } + + @Test + public void requireThatExpressionCanBeVerified() { + Expression exp = new ToBoolExpression(); + assertVerify(DataType.INT, exp, DataType.BOOL); + assertVerify(DataType.STRING, exp, DataType.BOOL); + assertVerifyThrows(null, exp, "Expected any input, got null."); + } + + @Test + public void requireThatNonEmptyStringBecomesTrue() { + ExecutionContext context = new ExecutionContext(new SimpleTestAdapter()); + context.setValue(new StringFieldValue("false")).execute(new ToBoolExpression()); + FieldValue value = context.getValue(); + assertTrue(value instanceof BoolFieldValue); + assertTrue(((BoolFieldValue)value).getBoolean()); + } + + @Test + public void requireThatEmptyStringBecomesFalse() { + ExecutionContext context = new ExecutionContext(new SimpleTestAdapter()); + context.setValue(new StringFieldValue("")).execute(new ToBoolExpression()); + FieldValue value = context.getValue(); + assertTrue(value instanceof BoolFieldValue); + assertFalse(((BoolFieldValue)value).getBoolean()); + } + + @Test + public void requireThatNonZeroBecomesTrue() { + ExecutionContext context = new ExecutionContext(new SimpleTestAdapter()); + context.setValue(new IntegerFieldValue(37)).execute(new ToBoolExpression()); + FieldValue value = context.getValue(); + assertTrue(value instanceof BoolFieldValue); + assertTrue(((BoolFieldValue)value).getBoolean()); + } + + @Test + public void requireThatZeroBecomesFalse() { + ExecutionContext context = new ExecutionContext(new SimpleTestAdapter()); + context.setValue(new IntegerFieldValue(0)).execute(new ToBoolExpression()); + FieldValue value = context.getValue(); + assertTrue(value instanceof BoolFieldValue); + assertFalse(((BoolFieldValue)value).getBoolean()); + } + +} diff --git a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/ExpressionTestCase.java b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/ExpressionTestCase.java index 095842544a6..e9a0d0253e1 100644 --- a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/ExpressionTestCase.java +++ b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/ExpressionTestCase.java @@ -73,6 +73,7 @@ public class ExpressionTestCase { assertExpression(ToPositionExpression.class, "to_pos"); assertExpression(ToStringExpression.class, "to_string"); assertExpression(ToWsetExpression.class, "to_wset"); + assertExpression(ToBoolExpression.class, "to_bool"); assertExpression(ToWsetExpression.class, "to_wset create_if_non_existent"); assertExpression(ToWsetExpression.class, "to_wset remove_if_zero"); assertExpression(ToWsetExpression.class, "to_wset create_if_non_existent remove_if_zero"); diff --git a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/IdentifierTestCase.java b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/IdentifierTestCase.java index d7f1582fd50..f087f3f315c 100644 --- a/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/IdentifierTestCase.java +++ b/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/parser/IdentifierTestCase.java @@ -63,6 +63,7 @@ public class IdentifierTestCase { "to_pos", "to_string", "to_wset", + "to_bool", "trim", "zcurve"); for (String str : tokens) { |