Skip to content

Commit

Permalink
Add tests of binop associativity
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 11, 2024
1 parent da509d5 commit c463a74
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,48 @@ fn test_tuple_comma() {
"###);
}

#[test]
fn test_binop_associativity() {
// Left to right.
snapshot!("() + () + ()" as Expr, @r###"
Expr::Binary {
left: Expr::Binary {
left: Expr::Tuple,
op: BinOp::Add,
right: Expr::Tuple,
},
op: BinOp::Add,
right: Expr::Tuple,
}
"###);

// Right to left.
snapshot!("() += () += ()" as Expr, @r###"
Expr::Binary {
left: Expr::Tuple,
op: BinOp::AddAssign,
right: Expr::Binary {
left: Expr::Tuple,
op: BinOp::AddAssign,
right: Expr::Tuple,
},
}
"###);

// FIXME: this should fail to parse. Parenthesization is required.
snapshot!("() == () == ()" as Expr, @r###"
Expr::Binary {
left: Expr::Binary {
left: Expr::Tuple,
op: BinOp::Eq,
right: Expr::Tuple,
},
op: BinOp::Eq,
right: Expr::Tuple,
}
"###);
}

#[test]
fn test_assign_range_precedence() {
// Range has higher precedence as the right-hand of an assignment, but
Expand Down

0 comments on commit c463a74

Please sign in to comment.