]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/mut_visit/tests.rs
Auto merge of #65202 - pietroalbini:scriptify-ci-config, r=alexcrichton
[rust.git] / src / libsyntax / mut_visit / tests.rs
1 use super::*;
2
3 use crate::ast::{self, Ident};
4 use crate::tests::{string_to_crate, matches_codepattern};
5 use crate::print::pprust;
6 use crate::mut_visit;
7 use crate::with_default_globals;
8
9 // This version doesn't care about getting comments or doc-strings in.
10 fn fake_print_crate(s: &mut pprust::State<'_>,
11                     krate: &ast::Crate) {
12     s.print_mod(&krate.module, &krate.attrs)
13 }
14
15 // Change every identifier to "zz".
16 struct ToZzIdentMutVisitor;
17
18 impl MutVisitor for ToZzIdentMutVisitor {
19     fn visit_ident(&mut self, ident: &mut ast::Ident) {
20         *ident = Ident::from_str("zz");
21     }
22     fn visit_mac(&mut self, mac: &mut ast::Mac) {
23         mut_visit::noop_visit_mac(mac, self)
24     }
25 }
26
27 // Maybe add to `expand.rs`.
28 macro_rules! assert_pred {
29     ($pred:expr, $predname:expr, $a:expr , $b:expr) => (
30         {
31             let pred_val = $pred;
32             let a_val = $a;
33             let b_val = $b;
34             if !(pred_val(&a_val, &b_val)) {
35                 panic!("expected args satisfying {}, got {} and {}",
36                         $predname, a_val, b_val);
37             }
38         }
39     )
40 }
41
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);
49         assert_pred!(
50             matches_codepattern,
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());
54     })
55 }
56
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);
65         assert_pred!(
66             matches_codepattern,
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());
70     })
71 }