Skip to content

Commit

Permalink
Closes #882 (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremija authored May 27, 2024
1 parent 15b6609 commit 9653290
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ fn parse_logic_val(pair: Pair<Rule>) -> TeraResult<Expr> {
Rule::in_cond => expr = Some(parse_in_condition(p)?),
Rule::comparison_expr => expr = Some(parse_comparison_expression(p)?),
Rule::string_expr_filter => expr = Some(parse_string_expr_with_filters(p)?),
Rule::logic_expr => expr = Some(parse_logic_expr(p)?),
_ => unreachable!(),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tera.pest
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ comparison_expr = { (string_expr_filter | comparison_val) ~ (comparison_op ~ (st
in_cond_container = {string_expr_filter | array_filter | dotted_square_bracket_ident}
in_cond = !{ (string_expr_filter | basic_expr_filter) ~ op_not? ~ "in" ~ in_cond_container }

logic_val = !{ op_not? ~ (in_cond | comparison_expr) }
logic_val = !{ op_not? ~ (in_cond | comparison_expr) | "(" ~ logic_expr ~ ")" }
logic_expr = !{ logic_val ~ ((op_or | op_and) ~ logic_val)* }

array = !{ "[" ~ (logic_val ~ ",")* ~ logic_val? ~ "]"}
Expand Down
24 changes: 24 additions & 0 deletions src/parser/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,30 @@ fn parse_variable_tag_negated_expr() {
);
}

#[test]
fn parse_variable_tag_negated_expr_with_parentheses() {
let ast = parse("{{ (not id or not true) and not 1 + 1 }}").unwrap();
assert_eq!(
ast[0],
Node::VariableBlock(
WS::default(),
Expr::new(ExprVal::Logic(LogicExpr {
lhs: Box::new(Expr::new(ExprVal::Logic(LogicExpr {
lhs: Box::new(Expr::new_negated(ExprVal::Ident("id".to_string()))),
operator: LogicOperator::Or,
rhs: Box::new(Expr::new_negated(ExprVal::Bool(true))),
},))),
operator: LogicOperator::And,
rhs: Box::new(Expr::new_negated(ExprVal::Math(MathExpr {
lhs: Box::new(Expr::new(ExprVal::Int(1))),
operator: MathOperator::Add,
rhs: Box::new(Expr::new(ExprVal::Int(1))),
},))),
},))
)
);
}

#[test]
fn parse_variable_tag_simple_test() {
let ast = parse("{{ id is defined }}").unwrap();
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,19 @@ fn render_if_elif_else() {
("{% if 'n' in name %}Admin{% else %}Hmm{% endif %}", "Admin"),
// function in if
("{% if get_true() %}Truth{% endif %}", "Truth"),
// Parentheses around logic expressions
("{% if age >= 18 and name == 'john' %}Truth{% endif %}", "Truth"),
("{% if (age >= 18) and (name == 'john') %}Truth{% endif %}", "Truth"),
("{% if (age >= 18) or (name == 'john') %}Truth{% endif %}", "Truth"),
("{% if (age < 18) or (name == 'john') %}Truth{% endif %}", "Truth"),
("{% if (age >= 18) or (name != 'john') %}Truth{% endif %}", "Truth"),
("{% if (age < 18) and (name != 'john') %}Truth{% endif %}", ""),
("{% if (age >= 18) and (name != 'john') %}Truth{% endif %}", ""),
("{% if (age >= 18 and name == 'john') %}Truth{% endif %}", "Truth"),
("{% if (age < 18 and name == 'john') %}Truth{% endif %}", ""),
("{% if (age >= 18 and name != 'john') %}Truth{% endif %}", ""),
("{% if age >= 18 or name == 'john' and is_false %}Truth{% endif %}", "Truth"),
("{% if (age >= 18 or name == 'john') and is_false %}Truth{% endif %}", ""),
];

for (input, expected) in inputs {
Expand Down

0 comments on commit 9653290

Please sign in to comment.