Skip to content

Commit

Permalink
Add more auto-parenthesization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 16, 2024
1 parent 532d3a3 commit afa1c72
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,17 +655,38 @@ fn test_fixup() {
}
}

let tokens = quote!(2 * (1 + 1));
let original: Expr = syn::parse2(tokens).unwrap();

let mut flat = original.clone();
FlattenParens.visit_expr_mut(&mut flat);
let reconstructed: Expr = syn::parse2(flat.to_token_stream()).unwrap();

assert!(
original == reconstructed,
"original: {}\nreconstructed: {}",
original.to_token_stream(),
reconstructed.to_token_stream(),
);
for tokens in [
quote! { 2 * (1 + 1) },
quote! { 0 + (0 + 0) },
quote! { (a = b) = c },
quote! { (x as i32) < 0 },
quote! { (1 + x as i32) < 0 },
quote! { (1 + 1).abs() },
quote! { (lo..hi)[..] },
quote! { (a..b)..(c..d) },
quote! { (&mut fut).await },
quote! { &mut (x as i32) },
quote! { -(x as i32) },
quote! { if (S {} == 1) {} },
quote! { { (m! {}) - 1 } },
quote! { match m { _ => ({}) - 1 } },
quote! { if let _ = (a && b) && c {} },
quote! { if let _ = (S {}) {} },
] {
let original: Expr = syn::parse2(tokens).unwrap();

let mut flat = original.clone();
FlattenParens.visit_expr_mut(&mut flat);
let reconstructed: Expr = match syn::parse2(flat.to_token_stream()) {
Ok(reconstructed) => reconstructed,
Err(err) => panic!("failed to parse `{}`: {}", flat.to_token_stream(), err),
};

assert!(
original == reconstructed,
"original: {}\nreconstructed: {}",
original.to_token_stream(),
reconstructed.to_token_stream(),
);
}
}

0 comments on commit afa1c72

Please sign in to comment.