jinja : support noncall test statements with arg (#29443)

* support noncall test statements with arg

* add tests
This commit is contained in:
Sigbjørn Skjæret
2026-09-26 08:56:48 +02:00
committed by GitHub
parent 965f89794f
commit a1de614ba3
2 changed files with 24 additions and 5 deletions
+9 -2
View File
@@ -429,8 +429,15 @@ private:
bool negate = false;
if (is_identifier("not")) { ++current; negate = true; }
auto test_id = parse_primary_expression();
// FIXME: tests can also be expressed like this: if x is eq 3
if (is(token::open_paren)) test_id = parse_call_expression(std::move(test_id));
if (is(token::open_paren)) {
test_id = parse_call_expression(std::move(test_id));
} else if (is(token::numeric_literal) || is(token::string_literal) || is(token::open_curly_bracket) || is(token::open_square_bracket) ||
(is(token::identifier) && !is_identifier("and") && !is_identifier("or") && !is_identifier("else"))) {
size_t call_pos = current;
statements args;
args.push_back(std::move(parse_unary_expression()));
test_id = mk_stmt<call_expression>(call_pos, std::move(test_id), std::move(args));
}
operand = mk_stmt<test_expression>(start_pos, std::move(operand), negate, std::move(test_id));
}
return operand;
+15 -3
View File
@@ -1153,7 +1153,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is not equalto",
"{{ 'yes' if 3 is not equalto(4) }}",
"{{ 'yes' if 3 is not equalto 4 }}",
json::object(),
"yes"
);
@@ -1165,7 +1165,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is gt",
"{{ 'yes' if 3 is gt(2) }}",
"{{ 'yes' if 3 is gt 2 }}",
json::object(),
"yes"
);
@@ -1177,7 +1177,7 @@ static void test_tests(testing & t) {
);
test_template(t, "is lt",
"{{ 'yes' if 2 is lt(3) }}",
"{{ 'yes' if 2 is lt 3 }}",
json::object(),
"yes"
);
@@ -1194,6 +1194,12 @@ static void test_tests(testing & t) {
"yes"
);
test_template(t, "is lt and gt",
"{{ 'yes' if x is lt 3 and x is gt 1 }}",
{{"x", 2}},
"yes"
);
test_template(t, "is lower",
"{{ 'yes' if 'lowercase' is lower }}",
json::object(),
@@ -1248,6 +1254,12 @@ static void test_tests(testing & t) {
"yes"
);
test_template(t, "is integer or float",
"{{ 'yes' if x.y is integer or x.y is float else 'no' }}",
{{"x", {{"y", 1.1}}}},
"yes"
);
test_template(t, "is sequence",
"{{ 'yes' if x is sequence }}",
{{"x", json::array({1, 2, 3})}},