3 use crate::ast::{self, Ident};
4 use crate::tests::{string_to_crate, matches_codepattern};
5 use crate::print::pprust;
7 use crate::with_default_globals;
9 // This version doesn't care about getting comments or doc-strings in.
10 fn fake_print_crate(s: &mut pprust::State<'_>,
12 s.print_mod(&krate.module, &krate.attrs)
15 // Change every identifier to "zz".
16 struct ToZzIdentMutVisitor;
18 impl MutVisitor for ToZzIdentMutVisitor {
19 fn visit_ident(&mut self, ident: &mut ast::Ident) {
20 *ident = Ident::from_str("zz");
22 fn visit_mac(&mut self, mac: &mut ast::Mac) {
23 mut_visit::noop_visit_mac(mac, self)
27 // Maybe add to `expand.rs`.
28 macro_rules! assert_pred {
29 ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
34 if !(pred_val(&a_val, &b_val)) {
35 panic!("expected args satisfying {}, got {} and {}",
36 $predname, a_val, b_val);
42 // Make sure idents get transformed everywhere.
43 #[test] fn ident_transformation () {
44 with_default_globals(|| {
45 let mut zz_visitor = ToZzIdentMutVisitor;
46 let mut krate = string_to_crate(
47 "#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
48 zz_visitor.visit_crate(&mut krate);
51 "matches_codepattern",
52 pprust::to_string(|s| fake_print_crate(s, &krate)),
53 "#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
57 // Make sure idents get transformed even inside macro defs.
58 #[test] fn ident_transformation_in_defs () {
59 with_default_globals(|| {
60 let mut zz_visitor = ToZzIdentMutVisitor;
61 let mut krate = string_to_crate(
62 "macro_rules! a {(b $c:expr $(d $e:token)f+ => \
63 (g $(d $d $e)+))} ".to_string());
64 zz_visitor.visit_crate(&mut krate);
67 "matches_codepattern",
68 pprust::to_string(|s| fake_print_crate(s, &krate)),
69 "macro_rules! zz{(zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+))}".to_string());