]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/mut_visit/tests.rs
New upstream version 1.39.0+dfsg1
[rustc.git] / src / libsyntax / mut_visit / tests.rs
CommitLineData
416331ca
XL
1use super::*;
2
3use crate::ast::{self, Ident};
4use crate::tests::{string_to_crate, matches_codepattern};
5use crate::print::pprust;
6use crate::mut_visit;
7use crate::with_default_globals;
8
e1599b0c 9// This version doesn't care about getting comments or doc-strings in.
416331ca
XL
10fn fake_print_crate(s: &mut pprust::State<'_>,
11 krate: &ast::Crate) {
12 s.print_mod(&krate.module, &krate.attrs)
13}
14
e1599b0c 15// Change every identifier to "zz".
416331ca
XL
16struct ToZzIdentMutVisitor;
17
18impl 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
e1599b0c 27// Maybe add to `expand.rs`.
416331ca
XL
28macro_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
e1599b0c 42// Make sure idents get transformed everywhere.
416331ca
XL
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
e1599b0c 57// Make sure idents get transformed even inside macro defs.
416331ca
XL
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}