about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-07-08 20:52:54 -0700
committerBrian Anderson <banderson@mozilla.com>2011-07-09 12:35:30 -0700
commit5543404abe2ecfe280ffd4393d4e4c9ad3d489b8 (patch)
tree6413f156d3d7744c20d644f457a4715eb877edf3
parent9af59f9d81b8d8c91e2c470e82813d75a6bcdc9e (diff)
downloadrust-5543404abe2ecfe280ffd4393d4e4c9ad3d489b8.tar.gz
rust-5543404abe2ecfe280ffd4393d4e4c9ad3d489b8.zip
Track the path as we fold over the AST looking for unit tests. Issue #428
-rw-r--r--src/comp/front/test.rs20
-rw-r--r--src/comp/syntax/ast.rs4
-rw-r--r--src/comp/syntax/fold.rs1
3 files changed, 21 insertions, 4 deletions
diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs
index 0f93e61a410..101a8e5cc89 100644
--- a/src/comp/front/test.rs
+++ b/src/comp/front/test.rs
@@ -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;
diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs
index 5438a2392b2..15cb5fc7d07 100644
--- a/src/comp/syntax/ast.rs
+++ b/src/comp/syntax/ast.rs
@@ -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;
diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs
index 01e43d9a369..4c35dd82170 100644
--- a/src/comp/syntax/fold.rs
+++ b/src/comp/syntax/fold.rs
@@ -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;