diff --git a/README.md b/README.md index 4c45ab9..f99b3c9 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ EvalEx is a handy expression evaluator for Java, that allows to parse and evalua - Supports implicit multiplication, e.g. 2x or (a+b)(a-b) or 2(x-y) which equals to (a+b)\*(a-b) or 2\*( x-y) - Lazy evaluation of function parameters (see the IF function) and support of sub-expressions. +- Requires minimum Java version 11. ## Documentation diff --git a/docs/concepts/date_time_duration.md b/docs/concepts/date_time_duration.md index d5045a6..667d99a 100644 --- a/docs/concepts/date_time_duration.md +++ b/docs/concepts/date_time_duration.md @@ -31,6 +31,7 @@ The infix plus and minus operators can be used to do calculations with _DATE_TIM The outcome of the operation depends on the operator types: #### Addition + | Left Operand | Right Operand | Result | |--------------|---------------|------------------------------------------------------------------------| | _DATE_TIME_ | _DURATION_ | A new _DATE_TIME_ where the duration is added to the date. | @@ -54,6 +55,7 @@ System.out.println(result); // will print "EvaluationValue(value=2023-12-04T02:1 ``` #### Subtraction + | Left Operand | Right Operand | Result | |--------------|---------------|-----------------------------------------------------------------------------| | _DATE_TIME_ | _DATE_TIME_ | A duration which reflects the difference between the two date-times. | diff --git a/docs/index.md b/docs/index.md index 28acc4b..cf918a0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -24,6 +24,7 @@ EvalEx is a handy expression evaluator for Java, that allows to parse and evalua - Supports implicit multiplication, e.g. 2x or (a+b)(a-b) or 2(x-y) which equals to (a+b)\*(a-b) or 2\*( x-y) - Lazy evaluation of function parameters (see the IF function) and support of sub-expressions. +- Requires minimum Java version 11. ## Documentation diff --git a/src/main/java/com/ezylang/evalex/operators/booleans/InfixEqualsOperator.java b/src/main/java/com/ezylang/evalex/operators/booleans/InfixEqualsOperator.java index 6bef6d8..40df165 100644 --- a/src/main/java/com/ezylang/evalex/operators/booleans/InfixEqualsOperator.java +++ b/src/main/java/com/ezylang/evalex/operators/booleans/InfixEqualsOperator.java @@ -30,6 +30,12 @@ public class InfixEqualsOperator extends AbstractOperator { @Override public EvaluationValue evaluate( Expression expression, Token operatorToken, EvaluationValue... operands) { - return expression.convertValue(operands[0].equals(operands[1])); + if (operands[0].isNullValue() && operands[1].isNullValue()) { + return EvaluationValue.booleanValue(true); + } + if (operands[0].isNullValue() || operands[1].isNullValue()) { + return EvaluationValue.booleanValue(false); + } + return expression.convertValue(operands[0].compareTo(operands[1]) == 0); } } diff --git a/src/main/java/com/ezylang/evalex/operators/booleans/InfixNotEqualsOperator.java b/src/main/java/com/ezylang/evalex/operators/booleans/InfixNotEqualsOperator.java index 50fd130..e414436 100644 --- a/src/main/java/com/ezylang/evalex/operators/booleans/InfixNotEqualsOperator.java +++ b/src/main/java/com/ezylang/evalex/operators/booleans/InfixNotEqualsOperator.java @@ -30,6 +30,12 @@ public class InfixNotEqualsOperator extends AbstractOperator { @Override public EvaluationValue evaluate( Expression expression, Token operatorToken, EvaluationValue... operands) { - return expression.convertValue(!operands[0].equals(operands[1])); + if (operands[0].isNullValue() && operands[1].isNullValue()) { + return EvaluationValue.booleanValue(false); + } + if (operands[0].isNullValue() || operands[1].isNullValue()) { + return EvaluationValue.booleanValue(true); + } + return expression.convertValue(operands[0].compareTo(operands[1]) != 0); } } diff --git a/src/test/java/com/ezylang/evalex/ExpressionEvaluatorDecimalPlacesTest.java b/src/test/java/com/ezylang/evalex/ExpressionEvaluatorDecimalPlacesTest.java index 44101dd..2fb5116 100644 --- a/src/test/java/com/ezylang/evalex/ExpressionEvaluatorDecimalPlacesTest.java +++ b/src/test/java/com/ezylang/evalex/ExpressionEvaluatorDecimalPlacesTest.java @@ -18,6 +18,7 @@ package com.ezylang.evalex; import static org.assertj.core.api.Assertions.assertThat; import com.ezylang.evalex.config.ExpressionConfiguration; +import com.ezylang.evalex.data.EvaluationValue; import com.ezylang.evalex.parser.ParseException; import java.math.BigDecimal; import java.util.HashMap; @@ -190,4 +191,20 @@ class ExpressionEvaluatorDecimalPlacesTest extends BaseExpressionEvaluatorTest { assertThat(expression.evaluate().getStringValue()).isEqualTo("5.01"); } + + @Test + void testEqualsIgnoresScale() throws EvaluationException, ParseException { + Expression expression = new Expression("a == b"); + EvaluationValue result = expression.with("a", 70).and("b", 70.0).evaluate(); + + assertThat(result.getBooleanValue()).isTrue(); + } + + @Test + void testNotEqualsIgnoresScale() throws EvaluationException, ParseException { + Expression expression = new Expression("a != b"); + EvaluationValue result = expression.with("a", 70).and("b", 70.0).evaluate(); + + assertThat(result.getBooleanValue()).isFalse(); + } } diff --git a/src/test/java/com/ezylang/evalex/ExpressionEvaluatorNullTest.java b/src/test/java/com/ezylang/evalex/ExpressionEvaluatorNullTest.java index c44e108..110c9f6 100644 --- a/src/test/java/com/ezylang/evalex/ExpressionEvaluatorNullTest.java +++ b/src/test/java/com/ezylang/evalex/ExpressionEvaluatorNullTest.java @@ -26,17 +26,27 @@ import org.junit.jupiter.api.Test; class ExpressionEvaluatorNullTest extends BaseExpressionEvaluatorTest { @Test - void testNullEquals() throws ParseException, EvaluationException { + void testSecondNullEquals() throws ParseException, EvaluationException { Expression expression = createExpression("a == null"); assertExpressionHasExpectedResult(expression.with("a", null), "true"); - assertExpressionHasExpectedResult(expression.with("a", 99), "false"); } @Test - void testNullNotEquals() throws ParseException, EvaluationException { + void testSecondNullNotEquals() throws ParseException, EvaluationException { Expression expression = new Expression("a != null"); assertExpressionHasExpectedResult(expression.with("a", null), "false"); - assertExpressionHasExpectedResult(expression.with("a", 99), "true"); + } + + @Test + void testFirstNullEquals() throws ParseException, EvaluationException { + Expression expression = createExpression("null == a"); + assertExpressionHasExpectedResult(expression.with("a", null), "true"); + } + + @Test + void testFirstNullNotEquals() throws ParseException, EvaluationException { + Expression expression = new Expression("null != a"); + assertExpressionHasExpectedResult(expression.with("a", null), "false"); } @Test