Skip to content

Commit

Permalink
handled nested evaluation value (#376)
Browse files Browse the repository at this point in the history
* handles nested evaluation values
  • Loading branch information
PrajwalBanakar26 authored Jun 8, 2023
1 parent 3ac17cd commit 574efc2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/ezylang/evalex/data/EvaluationValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public EvaluationValue(Object value) {
} else if (value instanceof Map) {
this.dataType = DataType.STRUCTURE;
this.value = convertMapStructure((Map<?, ?>) value);
} else if (value instanceof EvaluationValue) {
this.dataType = ((EvaluationValue) value).getDataType();
this.value = ((EvaluationValue) value).getValue();
} else {
throw new IllegalArgumentException(
"Unsupported data type '" + value.getClass().getName() + "'");
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/ezylang/evalex/data/EvaluationValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.parser.ASTNode;
import com.ezylang.evalex.parser.ParseException;
import com.ezylang.evalex.parser.Token;
import com.ezylang.evalex.parser.Token.TokenType;
import java.math.BigDecimal;
Expand Down Expand Up @@ -274,6 +277,27 @@ void testDoubleMathContext() {
.isEqualByComparingTo("3.99");
}

@Test
void nestedEvaluationValue() {
try {
EvaluationValue value1 = new EvaluationValue("Hello");
EvaluationValue value2 = new EvaluationValue("World");

Map<String, EvaluationValue> structure = new HashMap<>();
structure.put("a", value1);
structure.put("b", value2);

EvaluationValue structureMap = new EvaluationValue(structure);

Expression exp = new Expression("value.a == \"Hello\"").with("value", structureMap);

EvaluationValue result = exp.evaluate();
assertThat(result.getBooleanValue()).isTrue();
} catch (EvaluationException | ParseException e) {
e.printStackTrace();
}
}

private void assertDataIsCorrect(
EvaluationValue value,
String stringValue,
Expand Down

0 comments on commit 574efc2

Please sign in to comment.