Skip to content

Commit 2d59eba

Browse files
committed
add test cases for fold traversing macros
1 parent a51fae0 commit 2d59eba

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

src/libsyntax/fold.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use core::prelude::*;
1313
use ast::*;
1414
use ast;
1515
use codemap::{span, spanned};
16+
use parse::token;
1617
use opt_vec::OptVec;
1718

1819
use core::vec;
@@ -904,3 +905,74 @@ impl AstFoldExtensions for @ast_fold {
904905
pub fn make_fold(afp: ast_fold_fns) -> @ast_fold {
905906
afp as @ast_fold
906907
}
908+
909+
#[cfg(test)]
910+
mod test {
911+
use ast;
912+
use util::parser_testing::{string_to_crate, matches_codepattern};
913+
use parse::token;
914+
use print::pprust;
915+
use super::*;
916+
917+
// taken from expand
918+
// given a function from idents to idents, produce
919+
// an ast_fold that applies that function:
920+
pub fn fun_to_ident_folder(f: @fn(ast::ident)->ast::ident) -> @ast_fold{
921+
let afp = default_ast_fold();
922+
let f_pre = @AstFoldFns{
923+
fold_ident : |id, _| f(id),
924+
.. *afp
925+
};
926+
make_fold(f_pre)
927+
}
928+
929+
// this version doesn't care about getting comments or docstrings in.
930+
fn fake_print_crate(s: @pprust::ps, crate: ast::crate) {
931+
pprust::print_mod(s, &crate.node.module, crate.node.attrs);
932+
}
933+
934+
// change every identifier to "zz"
935+
pub fn to_zz() -> @fn(ast::ident)->ast::ident {
936+
let zz_id = token::str_to_ident("zz");
937+
|id| {zz_id}
938+
}
939+
940+
// maybe add to expand.rs...
941+
macro_rules! assert_pred (
942+
($pred:expr, $predname:expr, $a:expr , $b:expr) => (
943+
{
944+
let pred_val = $pred;
945+
let a_val = $a;
946+
let b_val = $b;
947+
if !(pred_val(a_val,b_val)) {
948+
fail!("expected args satisfying %s, got %? and %?",
949+
$predname, a_val, b_val);
950+
}
951+
}
952+
)
953+
)
954+
955+
// make sure idents get transformed everywhere
956+
#[test] fn ident_transformation () {
957+
let zz_fold = fun_to_ident_folder(to_zz());
958+
let ast = string_to_crate(@~"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
959+
assert_pred!(matches_codepattern,
960+
"matches_codepattern",
961+
pprust::to_str(zz_fold.fold_crate(ast),fake_print_crate,
962+
token::get_ident_interner()),
963+
~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
964+
}
965+
966+
// even inside macro defs....
967+
#[test] fn ident_transformation_in_defs () {
968+
let zz_fold = fun_to_ident_folder(to_zz());
969+
let ast = string_to_crate(@~"macro_rules! a {(b $c:expr $(d $e:token)f+
970+
=> (g $(d $d $e)+))} ");
971+
assert_pred!(matches_codepattern,
972+
"matches_codepattern",
973+
pprust::to_str(zz_fold.fold_crate(ast),fake_print_crate,
974+
token::get_ident_interner()),
975+
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
976+
}
977+
978+
}

src/libsyntax/util/parser_testing.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,81 @@ pub fn string_to_pat(source_str : @~str) -> @ast::pat {
6969
pub fn strs_to_idents(ids: ~[&str]) -> ~[ast::ident] {
7070
ids.map(|u| token::str_to_ident(*u))
7171
}
72+
73+
// does the given string match the pattern? whitespace in the first string
74+
// may be deleted or replaced with other whitespace to match the pattern.
75+
// this function is unicode-ignorant; fortunately, the careful design of
76+
// UTF-8 mitigates this ignorance. In particular, this function only collapses
77+
// sequences of \n, \r, ' ', and \t, but it should otherwise tolerate unicode
78+
// chars. Unsurprisingly, it doesn't do NKF-normalization(?).
79+
pub fn matches_codepattern(a : &str, b : &str) -> bool {
80+
let mut idx_a = 0;
81+
let mut idx_b = 0;
82+
loop {
83+
if (idx_a == a.len() && idx_b == b.len()) {
84+
return true;
85+
}
86+
else if (idx_a == a.len()) {return false;}
87+
else if (idx_b == b.len()) {
88+
// maybe the stuff left in a is all ws?
89+
if (is_whitespace(a.char_at(idx_a))) {
90+
return (scan_for_non_ws_or_end(a,idx_a) == a.len());
91+
} else {
92+
return false;
93+
}
94+
}
95+
// ws in both given and pattern:
96+
else if (is_whitespace(a.char_at(idx_a))
97+
&& is_whitespace(b.char_at(idx_b))) {
98+
idx_a = scan_for_non_ws_or_end(a,idx_a);
99+
idx_b = scan_for_non_ws_or_end(b,idx_b);
100+
}
101+
// ws in given only:
102+
else if (is_whitespace(a.char_at(idx_a))) {
103+
idx_a = scan_for_non_ws_or_end(a,idx_a);
104+
}
105+
// *don't* silently eat ws in expected only.
106+
else if (a.char_at(idx_a) == b.char_at(idx_b)) {
107+
idx_a += 1;
108+
idx_b += 1;
109+
}
110+
else {
111+
return false;
112+
}
113+
}
114+
}
115+
116+
// given a string and an index, return the first uint >= idx
117+
// that is a non-ws-char or is outside of the legal range of
118+
// the string.
119+
fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
120+
let mut i = idx;
121+
let len = a.len();
122+
while ((i < len) && (is_whitespace(a.char_at(i)))) {
123+
i += 1;
124+
}
125+
i
126+
}
127+
128+
// copied from lexer.
129+
pub fn is_whitespace(c: char) -> bool {
130+
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
131+
}
132+
133+
#[cfg(test)]
134+
mod test {
135+
use super::*;
136+
137+
#[test] fn eqmodws() {
138+
assert_eq!(matches_codepattern("",""),true);
139+
assert_eq!(matches_codepattern("","a"),false);
140+
assert_eq!(matches_codepattern("a",""),false);
141+
assert_eq!(matches_codepattern("a","a"),true);
142+
assert_eq!(matches_codepattern("a b","a \n\t\r b"),true);
143+
assert_eq!(matches_codepattern("a b ","a \n\t\r b"),true);
144+
assert_eq!(matches_codepattern("a b","a \n\t\r b "),false);
145+
assert_eq!(matches_codepattern("a b","a b"),true);
146+
assert_eq!(matches_codepattern("ab","a b"),false);
147+
assert_eq!(matches_codepattern("a b","ab"),true);
148+
}
149+
}

0 commit comments

Comments
 (0)