Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handled nested evaluation value #376

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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