Skip to content

Commit f72dbec

Browse files
committed
Collect functions that look like unit tests. Issue #428
1 parent 5543404 commit f72dbec

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/comp/front/test.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import std::option;
44
import std::ivec;
55
import syntax::ast;
66
import syntax::fold;
7+
import front::attr;
78

89
export modify_for_testing;
910

1011
type node_id_gen = @fn() -> ast::node_id;
1112

1213
type test_ctxt = rec(node_id_gen next_node_id,
13-
mutable ast::ident[] path);
14+
mutable ast::ident[] path,
15+
mutable ast::ident[][] testfns);
1416

1517
// Traverse the crate, collecting all the test functions, eliding any
1618
// existing main functions, and synthesizing a main test harness
@@ -28,8 +30,9 @@ fn modify_for_testing(@ast::crate crate) -> @ast::crate {
2830
ret this_node_id;
2931
} (next_node_id);
3032

31-
auto cx = rec(next_node_id = next_node_id_fn,
32-
mutable path = ~[]);
33+
let test_ctxt cx = rec(next_node_id = next_node_id_fn,
34+
mutable path = ~[],
35+
mutable testfns = ~[]);
3336

3437
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _),
3538
fold_item = bind fold_item(cx, _, _)
@@ -103,11 +106,36 @@ fn fold_item(&test_ctxt cx, &@ast::item i,
103106

104107
cx.path += ~[i.ident];
105108
log #fmt("current path: %s", ast::path_name_i(cx.path));
109+
110+
if (is_test_fn(i)) {
111+
log "this is a test function";
112+
cx.testfns += ~[cx.path];
113+
}
114+
106115
auto res = fold::noop_fold_item(i, fld);
107116
ivec::pop(cx.path);
108117
ret res;
109118
}
110119

120+
fn is_test_fn(&@ast::item i) -> bool {
121+
auto has_test_attr =
122+
ivec::len(attr::find_attrs_by_name(i.attrs, "test")) > 0u;
123+
124+
fn has_test_signature(&@ast::item i) -> bool {
125+
alt (i.node) {
126+
case (ast::item_fn(?f, ?tps)) {
127+
auto input_cnt = ivec::len(f.decl.inputs);
128+
auto no_output = f.decl.output.node == ast::ty_nil;
129+
auto tparm_cnt = ivec::len(tps);
130+
input_cnt == 0u && no_output && tparm_cnt == 0u
131+
}
132+
case (_) { false }
133+
}
134+
}
135+
136+
ret has_test_attr && has_test_signature(i);
137+
}
138+
111139
// Local Variables:
112140
// mode: rust
113141
// fill-column: 78;

0 commit comments

Comments
 (0)