Skip to content

Commit

Permalink
* fixes equals and not equals comparison to ignore decimal scale (#421)
Browse files Browse the repository at this point in the history
* fixes documentation layout for date / time
* adds note of minimum Java version to README and documentation
  • Loading branch information
uklimaschewski authored Jan 10, 2024
1 parent f9b78b9 commit 3d8fb02
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/concepts/date_time_duration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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. |
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -190,4 +191,20 @@ void testDecimalPlacesResultAndAuto() throws EvaluationException, ParseException

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();
}
}
18 changes: 14 additions & 4 deletions src/test/java/com/ezylang/evalex/ExpressionEvaluatorNullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@
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
Expand Down

0 comments on commit 3d8fb02

Please sign in to comment.