Track the path as we fold over the AST looking for unit tests. Issue #428

This commit is contained in:
Brian Anderson 2011-07-08 20:52:54 -07:00
parent 9af59f9d81
commit 5543404abe
3 changed files with 21 additions and 4 deletions

View file

@ -1,6 +1,7 @@
// Code that generates a test runner to run all the tests in a crate
import std::option;
import std::ivec;
import syntax::ast;
import syntax::fold;
@ -8,7 +9,8 @@ export modify_for_testing;
type node_id_gen = @fn() -> ast::node_id;
type test_ctxt = rec(node_id_gen next_node_id);
type test_ctxt = rec(node_id_gen next_node_id,
mutable ast::ident[] path);
// Traverse the crate, collecting all the test functions, eliding any
// existing main functions, and synthesizing a main test harness
@ -26,9 +28,11 @@ fn modify_for_testing(@ast::crate crate) -> @ast::crate {
ret this_node_id;
} (next_node_id);
auto cx = rec(next_node_id = next_node_id_fn);
auto cx = rec(next_node_id = next_node_id_fn,
mutable path = ~[]);
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _)
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _),
fold_item = bind fold_item(cx, _, _)
with *fold::default_ast_fold());
auto fold = fold::make_fold(precursor);
@ -94,6 +98,16 @@ fn mk_main(&test_ctxt cx) -> @ast::item {
ret @item;
}
fn fold_item(&test_ctxt cx, &@ast::item i,
fold::ast_fold fld) -> @ast::item {
cx.path += ~[i.ident];
log #fmt("current path: %s", ast::path_name_i(cx.path));
auto res = fold::noop_fold_item(i, fld);
ivec::pop(cx.path);
ret res;
}
// Local Variables:
// mode: rust
// fill-column: 78;

View file

@ -20,7 +20,9 @@ type path_ = rec(ident[] idents, (@ty)[] types);
type path = spanned[path_];
fn path_name(&path p) -> str { ret str::connect_ivec(p.node.idents, "::"); }
fn path_name(&path p) -> str { path_name_i(p.node.idents) }
fn path_name_i(&ident[] idents) -> str { str::connect_ivec(idents, "::") }
type crate_num = int;
type node_id = int;

View file

@ -12,6 +12,7 @@ export default_ast_fold;
export make_fold;
export dummy_out;
export noop_fold_crate;
export noop_fold_item;
type ast_fold = @mutable a_f;