Skip to content

Commit

Permalink
#421 fixes inconsistent equals behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
uklimaschewski committed Jan 15, 2024
1 parent 3d8fb02 commit 7636c4c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class InfixEqualsOperator extends AbstractOperator {
@Override
public EvaluationValue evaluate(
Expression expression, Token operatorToken, EvaluationValue... operands) {
if (operands[0].getDataType() != operands[1].getDataType()) {
return EvaluationValue.booleanValue(false);
}
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,12 +30,12 @@ public class InfixNotEqualsOperator extends AbstractOperator {
@Override
public EvaluationValue evaluate(
Expression expression, Token operatorToken, EvaluationValue... operands) {
if (operands[0].getDataType() != operands[1].getDataType()) {
return EvaluationValue.booleanValue(true);
}
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 @@ -54,6 +54,26 @@ void testInfixEqualsLiterals(String expression, String expectedResult)
assertExpressionHasExpectedResult(expression, expectedResult);
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"1==\"1\" : false",
"\"1\"==1 : false",
"true==\"1\" : false",
"\"1\"==true : false",
"false==\"1\" : false",
"\"1\"==false : false",
"DT_DATE_NEW(2022,10,30)==1 : false",
"1==DT_DATE_NEW(2022,10,30) : false",
"DT_DURATION_PARSE(\"PT24H\")==1 : false",
"1==DT_DURATION_PARSE(\"PT24H\") : false",
})
void testInfixEqualsTypesDiffer(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@Test
void testInfixEqualsVariables() throws EvaluationException, ParseException {
Expression expression = new Expression("a=b");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ void testInfixNotEqualsLiterals(String expression, String expectedResult)
assertExpressionHasExpectedResult(expression, expectedResult);
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"1!=\"1\" : true",
"\"1\"!=1 : true",
"true!=\"1\" : true",
"\"1\"!=true : true",
"false!=\"1\" : true",
"\"1\"!=false : true",
"DT_DATE_NEW(2022,10,30)!=1 : true",
"1!=DT_DATE_NEW(2022,10,30) : true",
"DT_DURATION_PARSE(\"PT24H\")!=1 : true",
"1!=DT_DURATION_PARSE(\"PT24H\") : true",
})
void testInfixNotEqualsTypesDiffer(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@Test
void testInfixNotEqualsVariables() throws EvaluationException, ParseException {
Expression expression = new Expression("a!=b");
Expand Down

0 comments on commit 7636c4c

Please sign in to comment.