2011-07-06 23:28:07 +00:00
|
|
|
// Code that generates a test runner to run all the tests in a crate
|
|
|
|
|
|
|
|
import std::option;
|
2011-07-06 21:29:50 +00:00
|
|
|
import syntax::ast;
|
|
|
|
import syntax::fold;
|
|
|
|
|
|
|
|
export modify_for_testing;
|
|
|
|
|
2011-07-06 23:28:07 +00:00
|
|
|
type node_id_gen = @fn() -> ast::node_id;
|
|
|
|
|
|
|
|
type test_ctxt = rec(node_id_gen next_node_id);
|
2011-07-06 21:29:50 +00:00
|
|
|
|
|
|
|
// Traverse the crate, collecting all the test functions, eliding any
|
|
|
|
// existing main functions, and synthesizing a main test harness
|
2011-07-06 23:28:07 +00:00
|
|
|
fn modify_for_testing(@ast::crate crate) -> @ast::crate {
|
|
|
|
|
|
|
|
// FIXME: This hackasaurus assumes that 200000 is a safe number to start
|
|
|
|
// generating node_ids at (which is totally not the case). pauls is going
|
|
|
|
// to land a patch that puts parse_sess into session, which will give us
|
|
|
|
// access to the real next node_id.
|
|
|
|
auto next_node_id = @mutable 200000;
|
|
|
|
auto next_node_id_fn = @bind fn(@mutable ast::node_id next_node_id)
|
|
|
|
-> ast::node_id {
|
|
|
|
auto this_node_id = *next_node_id;
|
|
|
|
*next_node_id = next_node_id + 1;
|
|
|
|
ret this_node_id;
|
|
|
|
} (next_node_id);
|
2011-07-06 21:29:50 +00:00
|
|
|
|
2011-07-06 23:28:07 +00:00
|
|
|
auto cx = rec(next_node_id = next_node_id_fn);
|
2011-07-06 21:29:50 +00:00
|
|
|
|
2011-07-06 23:28:07 +00:00
|
|
|
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _)
|
|
|
|
with *fold::default_ast_fold());
|
2011-07-06 21:29:50 +00:00
|
|
|
|
2011-07-06 23:28:07 +00:00
|
|
|
auto fold = fold::make_fold(precursor);
|
|
|
|
auto res = @fold.fold_crate(*crate);
|
|
|
|
// FIXME: This is necessary to break a circular reference
|
|
|
|
fold::dummy_out(fold);
|
|
|
|
ret res;
|
2011-07-06 21:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_crate(&test_ctxt cx, &ast::crate_ c,
|
|
|
|
fold::ast_fold fld) -> ast::crate_ {
|
2011-07-06 23:28:07 +00:00
|
|
|
auto folded = fold::noop_fold_crate(c, fld);
|
|
|
|
|
|
|
|
// Add a special __test module to the crate that will contain code
|
|
|
|
// generated for the test harness
|
|
|
|
ret rec(module = add_test_module(cx, folded.module)
|
|
|
|
with folded);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_test_module(&test_ctxt cx, &ast::_mod m) -> ast::_mod {
|
|
|
|
auto testmod = mk_test_module(cx);
|
|
|
|
ret rec(items = m.items + [testmod]
|
|
|
|
with m);
|
2011-07-06 21:29:50 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 23:28:07 +00:00
|
|
|
fn mk_test_module(&test_ctxt cx) -> @ast::item {
|
|
|
|
auto mainfn = mk_main(cx);
|
|
|
|
let ast::_mod testmod = rec(view_items = [],
|
|
|
|
items = [mainfn]);
|
|
|
|
auto item_ = ast::item_mod(testmod);
|
|
|
|
let ast::item item = rec(ident = "__test",
|
|
|
|
attrs = ~[],
|
|
|
|
id = cx.next_node_id(),
|
|
|
|
node = item_,
|
|
|
|
span = rec(lo=0u, hi=0u));
|
|
|
|
ret @item;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_main(&test_ctxt cx) -> @ast::item {
|
|
|
|
auto ret_ty = @rec(node=ast::ty_nil,
|
|
|
|
span=rec(lo=0u, hi=0u));
|
|
|
|
|
2011-07-07 02:00:00 +00:00
|
|
|
let ast::fn_decl decl = rec(inputs = ~[],
|
2011-07-06 23:28:07 +00:00
|
|
|
output = ret_ty,
|
|
|
|
purity = ast::impure_fn,
|
|
|
|
cf = ast::return,
|
|
|
|
constraints = ~[]);
|
|
|
|
auto proto = ast::proto_fn;
|
|
|
|
|
2011-07-07 02:00:00 +00:00
|
|
|
let ast::block_ body_ = rec(stmts = ~[],
|
2011-07-06 23:28:07 +00:00
|
|
|
expr = option::none,
|
|
|
|
id = cx.next_node_id());
|
|
|
|
auto body = rec(node = body_, span = rec(lo=0u, hi=0u));
|
|
|
|
|
|
|
|
auto fn_ = rec(decl = decl,
|
|
|
|
proto = proto,
|
|
|
|
body = body);
|
|
|
|
|
2011-07-07 02:00:00 +00:00
|
|
|
auto item_ = ast::item_fn(fn_, ~[]);
|
2011-07-06 23:28:07 +00:00
|
|
|
let ast::item item = rec(ident = "main",
|
|
|
|
attrs = ~[],
|
|
|
|
id = cx.next_node_id(),
|
|
|
|
node = item_,
|
|
|
|
span = rec(lo=0u, hi=0u));
|
|
|
|
ret @item;
|
2011-07-06 21:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|