]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/mut_visit/tests.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / compiler / rustc_expand / src / mut_visit / tests.rs
1 use crate::tests::{matches_codepattern, string_to_crate};
2
3 use rustc_ast as ast;
4 use rustc_ast::mut_visit::MutVisitor;
5 use rustc_ast_pretty::pprust;
6 use rustc_span::create_default_session_globals_then;
7 use rustc_span::symbol::Ident;
8
9 // This version doesn't care about getting comments or doc-strings in.
10 fn print_crate_items(krate: &ast::Crate) -> String {
11     krate.items.iter().map(|i| pprust::item_to_string(i)).collect::<Vec<_>>().join(" ")
12 }
13
14 // Change every identifier to "zz".
15 struct ToZzIdentMutVisitor;
16
17 impl MutVisitor for ToZzIdentMutVisitor {
18     const VISIT_TOKENS: bool = true;
19
20     fn visit_ident(&mut self, ident: &mut Ident) {
21         *ident = Ident::from_str("zz");
22     }
23 }
24
25 // Maybe add to `expand.rs`.
26 macro_rules! assert_pred {
27     ($pred:expr, $predname:expr, $a:expr , $b:expr) => {{
28         let pred_val = $pred;
29         let a_val = $a;
30         let b_val = $b;
31         if !(pred_val(&a_val, &b_val)) {
32             panic!("expected args satisfying {}, got {} and {}", $predname, a_val, b_val);
33         }
34     }};
35 }
36
37 // Make sure idents get transformed everywhere.
38 #[test]
39 fn ident_transformation() {
40     create_default_session_globals_then(|| {
41         let mut zz_visitor = ToZzIdentMutVisitor;
42         let mut krate =
43             string_to_crate("#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
44         zz_visitor.visit_crate(&mut krate);
45         assert_pred!(
46             matches_codepattern,
47             "matches_codepattern",
48             print_crate_items(&krate),
49             "#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string()
50         );
51     })
52 }
53
54 // Make sure idents get transformed even inside macro defs.
55 #[test]
56 fn ident_transformation_in_defs() {
57     create_default_session_globals_then(|| {
58         let mut zz_visitor = ToZzIdentMutVisitor;
59         let mut krate = string_to_crate(
60             "macro_rules! a {(b $c:expr $(d $e:token)f+ => \
61             (g $(d $d $e)+))} "
62                 .to_string(),
63         );
64         zz_visitor.visit_crate(&mut krate);
65         assert_pred!(
66             matches_codepattern,
67             "matches_codepattern",
68             print_crate_items(&krate),
69             "macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string()
70         );
71     })
72 }