diff options
-rw-r--r-- | doc/concepts/expressions.md | 12 | ||||
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 2 | ||||
-rw-r--r-- | test/buildtool/build_engine/expression/expression.test.cpp | 13 |
3 files changed, 20 insertions, 7 deletions
diff --git a/doc/concepts/expressions.md b/doc/concepts/expressions.md index a14cd60f..b3dbd82f 100644 --- a/doc/concepts/expressions.md +++ b/doc/concepts/expressions.md @@ -107,12 +107,12 @@ expression power. It is equivalent but lot shorter to multiple ###### Binary conditional: `"if"` -First the key `"cond"` is evaluated. If it evaluates to a -value that is logically true, then the key `"then"` is -evaluated and its value is the result of the evaluation. -Otherwise, the key `"else"` (if present, otherwise `[]` is -taken as default) is evaluated and the obtained value is the -result of the evaluation. +First the key `"cond"` is evaluated. If it evaluates to a value that +is logically true, then the key `"then"` (if present, otherwise `[]` +is taken as default) is evaluated and its value is the result of +the evaluation. Otherwise, the key `"else"` (if present, otherwise +`[]` is taken as default) is evaluated and the obtained value is +the result of the evaluation. ###### Sequential conditional: `"cond"` diff --git a/src/buildtool/build_engine/expression/evaluator.cpp b/src/buildtool/build_engine/expression/evaluator.cpp index 11299bb5..d53259ba 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -495,7 +495,7 @@ auto IfExpr(SubExprEvaluator&& eval, ExpressionPtr const& expr, Configuration const& env) -> ExpressionPtr { if (ValueIsTrue(EvalArgument(expr, "cond", eval, env))) { - return EvalArgument(expr, "then", eval, env); + return eval(expr->Get("then", list_t{}), env); } return eval(expr->Get("else", list_t{}), env); } diff --git a/test/buildtool/build_engine/expression/expression.test.cpp b/test/buildtool/build_engine/expression/expression.test.cpp index 83550ebd..1d7bde3e 100644 --- a/test/buildtool/build_engine/expression/expression.test.cpp +++ b/test/buildtool/build_engine/expression/expression.test.cpp @@ -479,6 +479,19 @@ TEST_CASE("Expression Evaluation", "[expression]") { // NOLINT CHECK(failure == Expression::FromJson(R"("failure")"_json)); } } + + SECTION("if defaults") { + auto expr = Expression::FromJson(R"( + { "type": "if" + , "cond": {"type": "var", "name": "x"} + } + )"_json); + CHECK(expr.Evaluate(env.Update("x", true), fcts) == + Expression::FromJson(R"([])"_json)); + CHECK(expr.Evaluate(env.Update("x", false), fcts) == + Expression::FromJson(R"([])"_json)); + } + SECTION("cond expression") { auto expr = Expression::FromJson(R"( { "type": "cond" |