diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/Controller.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/Controller.java index 40192ee24..848a3c4b7 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/Controller.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/Controller.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,7 +91,7 @@ public VisitResult postVisitBlock(Block block, Context ctx) { @Override public VisitResult visitCondition(Condition condition, Context ctx) { - if (condition.expression().eval(ctx::getValue)) { + if (condition.expression().eval(ctx::getValue).asBoolean()) { return VisitResult.CONTINUE; } return VisitResult.SKIP_SUBTREE; diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java index 0b789801a..6c586bfe8 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +83,7 @@ public VisitResult accept(Visitor visitor, A arg) { */ public static boolean filter(Node node, Function resolver) { if (node instanceof Condition) { - return ((Condition) node).expression().eval(resolver); + return ((Condition) node).expression().eval(resolver).asBoolean(); } return true; } diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Expression.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Expression.java index 6f409eb6c..858c35d32 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Expression.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Expression.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,7 +92,7 @@ public List tokens() { * * @return result */ - public boolean eval() { + public Value eval() { return eval(s -> null); } @@ -102,7 +102,7 @@ public boolean eval() { * @param resolver variable resolver * @return result */ - public Value eval1(Function resolver) { + public Value eval(Function resolver) { Deque stack = new ArrayDeque<>(); for (Token token : tokens) { Value value; @@ -134,12 +134,15 @@ public Value eval1(Function resolver) { break; case CONTAINS: if (lastOperand.type() == ValueTypes.STRING_LIST) { - result = new TypedValue(new HashSet<>(operand2.asList()).containsAll(lastOperand.asList()), ValueTypes.BOOLEAN); + result = new TypedValue(new HashSet<>(operand2.asList()).containsAll(lastOperand.asList()), + ValueTypes.BOOLEAN); } else { if (operand2.type() == ValueTypes.STRING) { - result = new TypedValue(operand2.asString().contains(lastOperand.asString()), ValueTypes.BOOLEAN); + result = new TypedValue(operand2.asString().contains(lastOperand.asString()), + ValueTypes.BOOLEAN); } else { - result = new TypedValue(operand2.asList().contains(lastOperand.asString()), ValueTypes.BOOLEAN); + result = new TypedValue(operand2.asList().contains(lastOperand.asString()), + ValueTypes.BOOLEAN); } } break; @@ -163,67 +166,6 @@ public Value eval1(Function resolver) { return stack.pop(); } - /** - * Evaluate this expression. - * - * @param resolver variable resolver - * @return result - */ - public boolean eval(Function resolver) { - Deque stack = new ArrayDeque<>(); - for (Token token : tokens) { - Value value; - if (token.operator != null) { - boolean result; - Value operand1 = stack.pop(); - if (token.operator == Operator.NOT) { - result = !operand1.asBoolean(); - } else { - Value operand2 = stack.pop(); - switch (token.operator) { - case OR: - result = operand2.asBoolean() || operand1.asBoolean(); - break; - case AND: - result = operand2.asBoolean() && operand1.asBoolean(); - break; - case EQUAL: - result = Value.equals(operand2, operand1); - break; - case NOT_EQUAL: - result = !Value.equals(operand2, operand1); - break; - case CONTAINS: - if (operand1.type() == ValueTypes.STRING_LIST) { - result = new HashSet<>(operand2.asList()).containsAll(operand1.asList()); - } else { - if (operand2.type() == ValueTypes.STRING) { - result = operand2.asString().contains(operand1.asString()); - } else { - result = operand2.asList().contains(operand1.asString()); - } - } - break; - default: - throw new IllegalStateException("Unsupported operator: " + token.operator); - } - } - value = Value.create(result); - } else if (token.operand != null) { - value = token.operand; - } else if (token.variable != null) { - value = resolver.apply(token.variable); - if (value == null) { - throw new UnresolvedVariableException(token.variable); - } - } else { - throw new IllegalStateException("Invalid token"); - } - stack.push(value); - } - return stack.pop().asBoolean(); - } - /** * Unresolved variable error. */ diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/InputPermutations.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/InputPermutations.java index 9a2fe00ae..2d2936983 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/InputPermutations.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/InputPermutations.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -265,7 +265,7 @@ private static boolean filter(List filters, Map perm return DynamicValue.create(v); } return null; - }); + }).asBoolean(); if (!result) { return false; } diff --git a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ScriptLoaderTest.java b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ScriptLoaderTest.java index 1fcf19f7a..8e7a1c47c 100644 --- a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ScriptLoaderTest.java +++ b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ScriptLoaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -664,7 +664,7 @@ public VisitResult visitPreset(Preset preset, Void arg) { @Override public VisitResult visitCondition(Condition condition, Void arg) { - if (condition.expression().eval()) { + if (condition.expression().eval().asBoolean()) { return VisitResult.CONTINUE; } return VisitResult.SKIP_SUBTREE; diff --git a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ast/ExpressionTest.java b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ast/ExpressionTest.java index 96a62495e..0e1a56c96 100644 --- a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ast/ExpressionTest.java +++ b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/ast/ExpressionTest.java @@ -45,21 +45,21 @@ public void testTernaryExpression() { exp = Expression.parse("${shape} == 'circle' ? 'red' : 'blue'"); variables = Map.of("shape", Value.create("circle")); - assertThat(exp.eval1(variables::get).asText(), is("red")); + assertThat(exp.eval(variables::get).asText(), is("red")); exp = Expression.parse("${shape} != 'circle' ? 'red' : 'blue'"); variables = Map.of("shape", Value.create("circle")); - assertThat(exp.eval1(variables::get).asText(), is("blue")); + assertThat(exp.eval(variables::get).asText(), is("blue")); exp = Expression.parse("false ? 'red' : 'blue'"); - assertThat(exp.eval1(variables::get).asText(), is("blue")); + assertThat(exp.eval(variables::get).asText(), is("blue")); exp = Expression.parse("${shape} == 'circle' ? ${var1} contains ${var2} : false"); variables = Map.of( "shape", Value.create("circle"), "var1", Value.create(List.of("a", "b", "c")), "var2", Value.create("b")); - assertThat(exp.eval1(variables::get).asBoolean(), is(true)); + assertThat(exp.eval(variables::get).asBoolean(), is(true)); exp = Expression.parse("${shape} == 'circle' ? ${var1} contains ${var2} ? 'circle_b' : 'not_circle_b' : ${var1} " + "contains ${var3} ? 'circle_c' : 'not_circle_c'"); @@ -68,7 +68,7 @@ public void testTernaryExpression() { "var1", Value.create(List.of("a", "b", "c")), "var2", Value.create("b"), "var3", Value.create("c")); - assertThat(exp.eval1(variables::get).asText(), is("circle_b")); + assertThat(exp.eval(variables::get).asText(), is("circle_b")); exp = Expression.parse("${shape} == 'circle' ? (${var1} contains ${var2} ? 'circle_b' : 'not_circle_b') : (${var1} " + "contains ${var3} ? 'circle_c' : 'not_circle_c')"); @@ -77,14 +77,14 @@ public void testTernaryExpression() { "var1", Value.create(List.of("a", "b", "c")), "var2", Value.create("b"), "var3", Value.create("c")); - assertThat(exp.eval1(variables::get).asText(), is("circle_b")); + assertThat(exp.eval(variables::get).asText(), is("circle_b")); exp = Expression.parse("${shape} != 'circle' ? ${var1} contains ${var2} : false"); variables = Map.of( "shape", Value.create("circle"), "var1", Value.create(List.of("a", "b", "c")), "var2", Value.create("b")); - assertThat(exp.eval1(variables::get).asBoolean(), is(false)); + assertThat(exp.eval(variables::get).asBoolean(), is(false)); exp = Expression.parse("${var1} contains ${var2} == ${var3} && ${var4} || ${var5} ? ['', 'adc', 'def'] contains 'foo' " + "== false && true || !false : ['', 'adc', 'def'] contains 'foo' == true && false"); @@ -94,7 +94,7 @@ public void testTernaryExpression() { "var3", Value.TRUE, "var4", Value.TRUE, "var5", Value.FALSE); - assertThat(exp.eval1(variables::get).asBoolean(), is(true)); + assertThat(exp.eval(variables::get).asBoolean(), is(true)); } @Test @@ -106,17 +106,17 @@ void testEvaluateWithVariables() { variables = Map.of( "var1", Value.create(List.of("a", "b", "c")), "var2", Value.create("b")); - assertThat(exp.eval(variables::get), is(true)); + assertThat(exp.eval(variables::get).asBoolean(), is(true)); exp = Expression.parse("!(${array} contains 'basic-auth' == false && ${var})"); variables = Map.of( "var", Value.TRUE, "array", Value.create(List.of("a", "b", "c"))); - assertThat(exp.eval(variables::get), is(false)); + assertThat(exp.eval(variables::get).asBoolean(), is(false)); exp = Expression.parse("!${var}"); variables = Map.of("var", Value.TRUE); - assertThat(exp.eval(variables::get), is(false)); + assertThat(exp.eval(variables::get).asBoolean(), is(false)); exp = Expression.parse("['', 'adc', 'def'] contains ${var1} == ${var4} && ${var2} || !${var3}"); variables = Map.of( @@ -124,7 +124,7 @@ void testEvaluateWithVariables() { "var2", Value.FALSE, "var3", Value.TRUE, "var4", Value.TRUE); - assertThat(exp.eval(variables::get), is(false)); + assertThat(exp.eval(variables::get).asBoolean(), is(false)); exp = Expression.parse("${var1} contains ${var2} == ${var3} && ${var4} || ${var5}"); variables = Map.of( @@ -133,13 +133,13 @@ void testEvaluateWithVariables() { "var3", Value.TRUE, "var4", Value.TRUE, "var5", Value.FALSE); - assertThat(exp.eval(variables::get), is(true)); + assertThat(exp.eval(variables::get).asBoolean(), is(true)); exp = Expression.parse(" ${var1} == ${var1} && ${var2} contains ''"); variables = Map.of( "var1", Value.create("foo"), "var2", Value.create(List.of("d", ""))); - assertThat(exp.eval(variables::get), is(true)); + assertThat(exp.eval(variables::get).asBoolean(), is(true)); } @Test @@ -152,27 +152,27 @@ void testVariable() { @Test void testEvaluate() { - assertThat(parse("['', 'adc', 'def'] contains 'foo'").eval(), is(false)); - assertThat(parse("!(['', 'adc', 'def'] contains 'foo' == false && false)").eval(), is(true)); - assertThat(parse("!false").eval(), is(true)); - assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true || !false").eval(), is(true)); - assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true || !true").eval(), is(true)); - assertThat(parse("['', 'adc', 'def'] contains 'def'").eval(), is(true)); - assertThat(parse("['', 'adc', 'def'] contains 'foo' == true && false").eval(), is(false)); - assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true").eval(), is(true)); - assertThat(parse(" 'aaa' == 'aaa' && ['', 'adc', 'def'] contains ''").eval(), is(true)); - assertThat(parse("true && \"bar\" == 'foo1' || true").eval(), is(true)); - assertThat(parse("true && \"bar\" == 'foo1' || false").eval(), is(false)); - assertThat(parse("('def' != 'def1') && false == true").eval(), is(false)); - assertThat(parse("('def' != 'def1') && false").eval(), is(false)); - assertThat(parse("('def' != 'def1') && true").eval(), is(true)); - assertThat(parse("'def' != 'def1'").eval(), is(true)); - assertThat(parse("'def' == 'def'").eval(), is(true)); - assertThat(parse("'def' != 'def'").eval(), is(false)); - assertThat(parse("true==((true|| false)&&true)").eval(), is(true)); - assertThat(parse("false==((true|| false)&&true)").eval(), is(false)); - assertThat(parse("false==((true|| false)&&false)").eval(), is(true)); - assertThat(parse("true == 'def'").eval(), is(false)); + assertThat(parse("['', 'adc', 'def'] contains 'foo'").eval().asBoolean(), is(false)); + assertThat(parse("!(['', 'adc', 'def'] contains 'foo' == false && false)").eval().asBoolean(), is(true)); + assertThat(parse("!false").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true || !false").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true || !true").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc', 'def'] contains 'def'").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc', 'def'] contains 'foo' == true && false").eval().asBoolean(), is(false)); + assertThat(parse("['', 'adc', 'def'] contains 'foo' == false && true").eval().asBoolean(), is(true)); + assertThat(parse(" 'aaa' == 'aaa' && ['', 'adc', 'def'] contains ''").eval().asBoolean(), is(true)); + assertThat(parse("true && \"bar\" == 'foo1' || true").eval().asBoolean(), is(true)); + assertThat(parse("true && \"bar\" == 'foo1' || false").eval().asBoolean(), is(false)); + assertThat(parse("('def' != 'def1') && false == true").eval().asBoolean(), is(false)); + assertThat(parse("('def' != 'def1') && false").eval().asBoolean(), is(false)); + assertThat(parse("('def' != 'def1') && true").eval().asBoolean(), is(true)); + assertThat(parse("'def' != 'def1'").eval().asBoolean(), is(true)); + assertThat(parse("'def' == 'def'").eval().asBoolean(), is(true)); + assertThat(parse("'def' != 'def'").eval().asBoolean(), is(false)); + assertThat(parse("true==((true|| false)&&true)").eval().asBoolean(), is(true)); + assertThat(parse("false==((true|| false)&&true)").eval().asBoolean(), is(false)); + assertThat(parse("false==((true|| false)&&false)").eval().asBoolean(), is(true)); + assertThat(parse("true == 'def'").eval().asBoolean(), is(false)); Throwable e; @@ -187,16 +187,16 @@ void testEvaluate() { @Test void testContainsOperator() { - assertThat(parse("['', 'adc', 'def'] contains 'foo'").eval(), is(false)); - assertThat(parse("['', 'adc', 'def'] contains ['', 'adc']").eval(), is(true)); - assertThat(parse("['', 'adc', 'def'] contains ['', 'adc', 'def']").eval(), is(true)); - assertThat(parse("['', 'adc'] contains ['', 'adc', 'def']").eval(), is(false)); - assertThat(parse("['', 'adc'] contains ['', 'adc', 'def']").eval(), is(false)); + assertThat(parse("['', 'adc', 'def'] contains 'foo'").eval().asBoolean(), is(false)); + assertThat(parse("['', 'adc', 'def'] contains ['', 'adc']").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc', 'def'] contains ['', 'adc', 'def']").eval().asBoolean(), is(true)); + assertThat(parse("['', 'adc'] contains ['', 'adc', 'def']").eval().asBoolean(), is(false)); + assertThat(parse("['', 'adc'] contains ['', 'adc', 'def']").eval().asBoolean(), is(false)); FormatException e = assertThrows(FormatException.class, () -> parse("['', 'adc', 'def'] contains != 'foo'").eval()); assertThat(e.getMessage(), startsWith("Missing operand")); - assertThat(parse("!(['', 'adc', 'def'] contains 'foo')").eval(), is(true)); + assertThat(parse("!(['', 'adc', 'def'] contains 'foo')").eval().asBoolean(), is(true)); e = assertThrows(FormatException.class, () -> parse("!['', 'adc', 'def'] contains 'basic-auth'")); assertThat(e.getMessage(), containsString("Invalid operand")); @@ -204,10 +204,10 @@ void testContainsOperator() { @Test void testUnaryLogicalExpression() { - assertThat(parse("!true").eval(), is(false)); - assertThat(parse("!false").eval(), is(true)); - assertThat(parse("!('foo' != 'bar')").eval(), is(false)); - assertThat(parse("'foo1' == 'bar' && !('foo' != 'bar')").eval(), is(false)); + assertThat(parse("!true").eval().asBoolean(), is(false)); + assertThat(parse("!false").eval().asBoolean(), is(true)); + assertThat(parse("!('foo' != 'bar')").eval().asBoolean(), is(false)); + assertThat(parse("'foo1' == 'bar' && !('foo' != 'bar')").eval().asBoolean(), is(false)); FormatException e = assertThrows(FormatException.class, () -> parse("!'string type'").eval()); assertThat(e.getMessage(), containsString("Invalid operand")); @@ -215,15 +215,15 @@ void testUnaryLogicalExpression() { @Test void testExpressionWithParenthesis() { - assertThat(parse("(\"foo\") != \"bar\"").eval(), is(true)); - assertThat(parse("((\"foo\")) != \"bar\"").eval(), is(true)); + assertThat(parse("(\"foo\") != \"bar\"").eval().asBoolean(), is(true)); + assertThat(parse("((\"foo\")) != \"bar\"").eval().asBoolean(), is(true)); - assertThat(parse("((\"foo\") != \"bar\")").eval(), is(true)); - assertThat(parse("\"foo\" != (\"bar\")").eval(), is(true)); - assertThat(parse("(\"foo\"==\"bar\")|| ${foo1}").eval(s -> Value.TRUE), is(true)); - assertThat(parse("(\"foo\"==\"bar\"|| true)").eval(), is(true)); - assertThat(parse("${foo}==(true|| false)").eval(s -> Value.TRUE), is(true)); - assertThat(parse("true==((${var}|| false)&&true)").eval(s -> Value.TRUE), is(true)); + assertThat(parse("((\"foo\") != \"bar\")").eval().asBoolean(), is(true)); + assertThat(parse("\"foo\" != (\"bar\")").eval().asBoolean(), is(true)); + assertThat(parse("(\"foo\"==\"bar\")|| ${foo1}").eval(s -> Value.TRUE).asBoolean(), is(true)); + assertThat(parse("(\"foo\"==\"bar\"|| true)").eval().asBoolean(), is(true)); + assertThat(parse("${foo}==(true|| false)").eval(s -> Value.TRUE).asBoolean(), is(true)); + assertThat(parse("true==((${var}|| false)&&true)").eval(s -> Value.TRUE).asBoolean(), is(true)); FormatException e; @@ -242,12 +242,12 @@ void testExpressionWithParenthesis() { @Test void testLiteralWithParenthesis() { - assertThat(parse("(true)").eval(), is(true)); - assertThat(parse("((true))").eval(), is(true)); - assertThat(parse("((${var}))").eval(s -> Value.TRUE), is(true)); - assertThat(parse("(\"value\") == (\"value\")").eval(), is(true)); - assertThat(parse("((\"value\")) == ((\"value\"))").eval(), is(true)); - assertThat(parse("\"(value)\" == \"(value)\"").eval(), is(true)); + assertThat(parse("(true)").eval().asBoolean(), is(true)); + assertThat(parse("((true))").eval().asBoolean(), is(true)); + assertThat(parse("((${var}))").eval(s -> Value.TRUE).asBoolean(), is(true)); + assertThat(parse("(\"value\") == (\"value\")").eval().asBoolean(), is(true)); + assertThat(parse("((\"value\")) == ((\"value\"))").eval().asBoolean(), is(true)); + assertThat(parse("\"(value)\" == \"(value)\"").eval().asBoolean(), is(true)); FormatException e; @@ -260,12 +260,12 @@ void testLiteralWithParenthesis() { e = assertThrows(FormatException.class, () -> parse("(\"value\"()").eval()); assertThat(e.getMessage(), startsWith("Unmatched parenthesis")); - assertThat(parse("([]) == []").eval(), is(true)); - assertThat(parse("(([])) == []").eval(), is(true)); - assertThat(parse("(['']) == ['']").eval(), is(true)); - assertThat(parse("(([''])) == ['']").eval(), is(true)); - assertThat(parse("(['', 'adc', 'def']) contains 'def'").eval(), is(true)); - assertThat(parse("((['', 'adc', 'def'])) contains 'def'").eval(), is(true)); + assertThat(parse("([]) == []").eval().asBoolean(), is(true)); + assertThat(parse("(([])) == []").eval().asBoolean(), is(true)); + assertThat(parse("(['']) == ['']").eval().asBoolean(), is(true)); + assertThat(parse("(([''])) == ['']").eval().asBoolean(), is(true)); + assertThat(parse("(['', 'adc', 'def']) contains 'def'").eval().asBoolean(), is(true)); + assertThat(parse("((['', 'adc', 'def'])) contains 'def'").eval().asBoolean(), is(true)); e = assertThrows(FormatException.class, () -> parse("((['', 'adc', 'def'])))").eval()); assertThat(e.getMessage(), startsWith("Unmatched parenthesis")); @@ -276,16 +276,16 @@ void testLiteralWithParenthesis() { @Test void testPrecedence() { - assertThat(parse("\"foo\"==\"bar\"|| true").eval(), is(true)); - assertThat(parse("\"foo\"==\"bar\" && true || false").eval(), is(false)); - assertThat(parse("true && \"bar\" != 'foo1'").eval(), is(true)); - assertThat(parse("true && ${bar} == 'foo1' || false").eval(s -> Value.create("foo1")), is(true)); + assertThat(parse("\"foo\"==\"bar\"|| true").eval().asBoolean(), is(true)); + assertThat(parse("\"foo\"==\"bar\" && true || false").eval().asBoolean(), is(false)); + assertThat(parse("true && \"bar\" != 'foo1'").eval().asBoolean(), is(true)); + assertThat(parse("true && ${bar} == 'foo1' || false").eval(s -> Value.create("foo1")).asBoolean(), is(true)); } @Test void testEqualPrecedence() { - assertThat(parse("\"foo\"!=\"bar\"==true").eval(), is(true)); - assertThat(parse("'foo'!=${var}==true").eval(s -> Value.create("bar")), is(true)); + assertThat(parse("\"foo\"!=\"bar\"==true").eval().asBoolean(), is(true)); + assertThat(parse("'foo'!=${var}==true").eval(s -> Value.create("bar")).asBoolean(), is(true)); } @Test @@ -296,47 +296,47 @@ void testIncorrectOperator() { @Test public void simpleNotEqualStringLiterals() { - assertThat(parse("'foo' != 'bar'").eval(), is(true)); + assertThat(parse("'foo' != 'bar'").eval().asBoolean(), is(true)); } @Test void testStringLiteralWithDoubleQuotes() { - assertThat(parse("[\"value\"] == \"value\"").eval(), is(false)); + assertThat(parse("[\"value\"] == \"value\"").eval().asBoolean(), is(false)); } @Test void testStringLiteralWithSingleQuotes() { - assertThat(parse("['value'] == 'value'").eval(), is(false)); + assertThat(parse("['value'] == 'value'").eval().asBoolean(), is(false)); } @Test void testStringLiteralWithWhitespaces() { - assertThat(parse("[' value '] != 'value'").eval(), is(true)); + assertThat(parse("[' value '] != 'value'").eval().asBoolean(), is(true)); } @Test void testBooleanLiteral() { - assertThat(parse("true").eval(), is(true)); - assertThat(parse("false").eval(), is(false)); - assertThat(parse("true == true").eval(), is(true)); - assertThat(parse("false == false").eval(), is(true)); - assertThat(parse("true != false").eval(), is(true)); + assertThat(parse("true").eval().asBoolean(), is(true)); + assertThat(parse("false").eval().asBoolean(), is(false)); + assertThat(parse("true == true").eval().asBoolean(), is(true)); + assertThat(parse("false == false").eval().asBoolean(), is(true)); + assertThat(parse("true != false").eval().asBoolean(), is(true)); } @Test void testEmptyStringArrayLiteral() { - assertThat(parse("[] == []").eval(), is(true)); + assertThat(parse("[] == []").eval().asBoolean(), is(true)); } @Test void testArrayWithEmptyLiteral() { - assertThat(parse("[''] contains ''").eval(), is(true)); + assertThat(parse("[''] contains ''").eval().asBoolean(), is(true)); } @Test void testArrayWithStringLiterals() { - assertThat(parse("['foo'] contains 'foo'").eval(), is(true)); - assertThat(parse("['foo'] contains 'bar'").eval(), is(false)); + assertThat(parse("['foo'] contains 'foo'").eval().asBoolean(), is(true)); + assertThat(parse("['foo'] contains 'bar'").eval().asBoolean(), is(false)); } @Test @@ -345,36 +345,36 @@ void testComplex() { assertThat(expr.eval(mapValue(Map.of( "metrics", "true", "tracing", "true", - "health", "true"), DynamicValue::create)::get), is(true)); + "health", "true"), DynamicValue::create)::get).asBoolean(), is(true)); assertThat(expr.eval(mapValue(Map.of( "metrics", "false", "tracing", "true", - "health", "true"), DynamicValue::create)::get), is(false)); + "health", "true"), DynamicValue::create)::get).asBoolean(), is(false)); assertThat(expr.eval(mapValue(Map.of( "metrics", "false", "tracing", "false", - "health", "false"), DynamicValue::create)::get), is(true)); + "health", "false"), DynamicValue::create)::get).asBoolean(), is(true)); } @Test void testUnaryPrecededWithLeftParenthesis() { - assertThat(parse("(!true)").eval(), is(false)); + assertThat(parse("(!true)").eval().asBoolean(), is(false)); } @Test void testMultilineExpression() { - assertThat(parse("true && \ntrue").eval(), is(true)); + assertThat(parse("true && \ntrue").eval().asBoolean(), is(true)); } @Test void testMultilineStringLiteral() { Expression expr = parse("${str} == \"f\no\no\no\""); - assertThat(expr.eval(mapValue(Map.of("str", "f\no\no\no"), DynamicValue::create)::get), is(true)); + assertThat(expr.eval(mapValue(Map.of("str", "f\no\no\no"), DynamicValue::create)::get).asBoolean(), is(true)); } @Test void testStringContains() { - assertThat(parse("'foo' contains 'oo'").eval(), is(true)); + assertThat(parse("'foo' contains 'oo'").eval().asBoolean(), is(true)); } @Test @@ -384,7 +384,7 @@ void testComments() { + " true && # inline comment\n" + " !false\n" + " && ${char} == \"#\""); - assertThat(expr.eval(mapValue(Map.of("char", "#"), DynamicValue::create)::get), is(true)); + assertThat(expr.eval(mapValue(Map.of("char", "#"), DynamicValue::create)::get).asBoolean(), is(true)); } @Test @@ -394,21 +394,21 @@ void testCommentWithStringLiteral() { + "\n" + "# 'foo'\n" + "true"); - assertThat(expr.eval(), is(true)); + assertThat(expr.eval().asBoolean(), is(true)); } @Test void testNoneList1() { Expression expr = parse("${list} == [] || (${list} contains 'foo' || ${list} contains 'bar')"); - assertThat(expr.eval(mapValue(Map.of("list", "none"), DynamicValue::create)::get), is(true)); - assertThat(expr.eval(mapValue(Map.of("list", "foo,bar"), DynamicValue::create)::get), is(true)); - assertThat(expr.eval(mapValue(Map.of("list", "bob"), DynamicValue::create)::get), is(false)); + assertThat(expr.eval(mapValue(Map.of("list", "none"), DynamicValue::create)::get).asBoolean(), is(true)); + assertThat(expr.eval(mapValue(Map.of("list", "foo,bar"), DynamicValue::create)::get).asBoolean(), is(true)); + assertThat(expr.eval(mapValue(Map.of("list", "bob"), DynamicValue::create)::get).asBoolean(), is(false)); } @Test void testNoneList2() { Expression expr = parse("${list1} == [] || (${list1} == ['foo'] && ${list2} == ['bar'])"); - assertThat(expr.eval(mapValue(Map.of("list1", "none", "list2", "bar"), DynamicValue::create)::get), is(true)); - assertThat(expr.eval(mapValue(Map.of("list1", "foo", "list2", "bar"), DynamicValue::create)::get), is(true)); + assertThat(expr.eval(mapValue(Map.of("list1", "none", "list2", "bar"), DynamicValue::create)::get).asBoolean(), is(true)); + assertThat(expr.eval(mapValue(Map.of("list1", "foo", "list2", "bar"), DynamicValue::create)::get).asBoolean(), is(true)); } }