Skip to content

Commit b06ccb4

Browse files
committed
Begin adding unit testing infrastructure to the compiler
Add a --test flag and a pass for transforming the AST to generate a test harness. Issue #428
1 parent 0eac640 commit b06ccb4

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

src/comp/driver/rustc.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ fn compile_input(session::session sess, ast::crate_cfg cfg, str input,
106106
if (sess.get_opts().output_type == link::output_type_none) { ret; }
107107
crate = time(time_passes, "configuration",
108108
bind front::config::strip_unconfigured_items(crate));
109+
if (sess.get_opts().test) {
110+
crate = time(time_passes, "building test harness",
111+
bind front::test::modify_for_testing(sess, crate));
112+
}
109113
auto ast_map = time(time_passes, "ast indexing",
110114
bind middle::ast_map::map_crate(*crate));
111115
auto d =
@@ -229,7 +233,8 @@ options:
229233
--time-passes time the individual phases of the compiler
230234
--time-llvm-passes time the individual phases of the LLVM backend
231235
--sysroot <path> override the system root (default: rustc's directory)
232-
--no-typestate don't run the typestate pass (unsafe!)\n\n");
236+
--no-typestate don't run the typestate pass (unsafe!)
237+
--test build test harness\n\n");
233238
}
234239

235240
fn get_os(str triple) -> session::os {
@@ -324,6 +329,7 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
324329
case (some(?s)) { s }
325330
};
326331
auto cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
332+
auto test = opt_present(match, "test");
327333
let @session::options sopts =
328334
@rec(shared=shared,
329335
optimize=opt_level,
@@ -337,7 +343,8 @@ fn build_session_options(str binary, getopts::match match, str binary_dir) ->
337343
output_type=output_type,
338344
library_search_paths=library_search_paths,
339345
sysroot=sysroot,
340-
cfg=cfg);
346+
cfg=cfg,
347+
test=test);
341348
ret sopts;
342349
}
343350

@@ -367,7 +374,7 @@ fn main(vec[str] args) {
367374
optflag("c"), optopt("o"), optflag("g"), optflag("save-temps"),
368375
optopt("sysroot"), optflag("stats"), optflag("time-passes"),
369376
optflag("time-llvm-passes"), optflag("no-typestate"),
370-
optflag("noverify"), optmulti("cfg")];
377+
optflag("noverify"), optmulti("cfg"), optflag("test")];
371378
auto binary = vec::shift[str](args);
372379
auto binary_dir = fs::dirname(binary);
373380
auto match =

src/comp/driver/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type options =
3737
str sysroot,
3838
// The crate config requested for the session, which may be combined
3939
// with additional crate configurations during the compile process
40-
ast::crate_cfg cfg);
40+
ast::crate_cfg cfg,
41+
bool test);
4142

4243
type crate_metadata = rec(str name, vec[u8] data);
4344

src/comp/front/test.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import driver::session;
2+
import syntax::ast;
3+
import syntax::fold;
4+
5+
export modify_for_testing;
6+
7+
type test_ctxt = rec(@session::session sess);
8+
9+
// Traverse the crate, collecting all the test functions, eliding any
10+
// existing main functions, and synthesizing a main test harness
11+
fn modify_for_testing(&session::session sess,
12+
@ast::crate crate) -> @ast::crate {
13+
14+
auto cx = rec(sess = @sess);
15+
16+
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _)
17+
with *fold::default_ast_fold());
18+
19+
auto fold = fold::make_fold(precursor);
20+
auto res = @fold.fold_crate(*crate);
21+
// FIXME: This is necessary to break a circular reference
22+
fold::dummy_out(fold);
23+
ret res;
24+
}
25+
26+
fn fold_crate(&test_ctxt cx, &ast::crate_ c,
27+
fold::ast_fold fld) -> ast::crate_ {
28+
auto folded = fold::noop_fold_crate(c, fld);
29+
ret rec(module = add_test_module(folded.module)
30+
with folded);
31+
}
32+
33+
fn add_test_module(&ast::_mod m) -> ast::_mod {
34+
ret m;
35+
}
36+
37+
// Local Variables:
38+
// mode: rust
39+
// fill-column: 78;
40+
// indent-tabs-mode: nil
41+
// c-basic-offset: 4
42+
// buffer-file-coding-system: utf-8-unix
43+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
44+
// End:

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ mod syntax {
6666
mod front {
6767
mod attr;
6868
mod config;
69+
mod test;
6970
}
7071

7172
mod back {

src/comp/syntax/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export ast_fold;
1010
export default_ast_fold;
1111
export make_fold;
1212
export dummy_out;
13+
export noop_fold_crate;
1314

1415
type ast_fold = @mutable a_f;
1516

0 commit comments

Comments
 (0)