Skip to content

Inject std when --test. #1127, #3241 #4309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,6 @@ pub mod core {
}


// Similar to above. Some magic to make core testable.
#[cfg(test)]
mod std {
extern mod std(vers = "0.6");
pub use std::std::test;
}


// Local Variables:
// mode: rust;
// fill-column: 78;
Expand Down
50 changes: 40 additions & 10 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,16 @@ mod __test {
*/

fn mk_test_module(cx: test_ctxt) -> @ast::item {
// Link to std
let std = mk_std(cx);
let view_items = if is_std(cx) { ~[] } else { ~[std] };
// A function that generates a vector of test descriptors to feed to the
// test runner
let testsfn = mk_tests(cx);
// The synthesized main function which will call the console test runner
// with our list of tests
let mainfn = mk_main(cx);
let testmod: ast::_mod = {view_items: ~[], items: ~[mainfn, testsfn]};
let testmod: ast::_mod = {view_items: view_items, items: ~[mainfn, testsfn]};
let item_ = ast::item_mod(testmod);
// This attribute tells resolve to let us call unexported functions
let resolve_unexported_attr =
Expand Down Expand Up @@ -245,6 +248,24 @@ fn path_node_global(ids: ~[ast::ident]) -> @ast::path {
@{span: dummy_sp(), global: true, idents: ids, rp: None, types: ~[]}
}

fn mk_std(cx: test_ctxt) -> @ast::view_item {
let vers = ast::lit_str(@~"0.6");
let vers = nospan(vers);
let mi = ast::meta_name_value(~"vers", vers);
let mi = nospan(mi);
let vi = ast::view_item_use(cx.sess.ident_of(~"std"),
~[@mi],
cx.sess.next_node_id());
let vi = {
node: vi,
attrs: ~[],
vis: ast::private,
span: dummy_sp()
};

return @vi;
}

fn mk_tests(cx: test_ctxt) -> @ast::item {
let ret_ty = mk_test_desc_vec_ty(cx);

Expand All @@ -271,25 +292,34 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
return @item;
}

fn mk_path(cx: test_ctxt, path: ~[ast::ident]) -> ~[ast::ident] {
// For tests that are inside of std we don't want to prefix
// the paths with std::
fn is_std(cx: test_ctxt) -> bool {
let is_std = {
let items = attr::find_linkage_metas(cx.crate.node.attrs);
match attr::last_meta_item_value_str_by_name(items, ~"name") {
Some(~"std") => true,
_ => false
}
};
if is_std { path }
else { vec::append(~[cx.sess.ident_of(~"std")], path) }
return is_std;
}

fn mk_path(cx: test_ctxt, path: ~[ast::ident]) -> @ast::path {
// For tests that are inside of std we don't want to prefix
// the paths with std::
if is_std(cx) { path_node_global(path) }
else {
path_node(
~[cx.sess.ident_of(~"self"),
cx.sess.ident_of(~"std")]
+ path)
}
}

// The ast::Ty of ~[std::test::test_desc]
fn mk_test_desc_vec_ty(cx: test_ctxt) -> @ast::Ty {
let test_desc_ty_path =
path_node_global(mk_path(cx, ~[cx.sess.ident_of(~"test"),
cx.sess.ident_of(~"TestDesc")]));
mk_path(cx, ~[cx.sess.ident_of(~"test"),
cx.sess.ident_of(~"TestDesc")]);

let test_desc_ty: ast::Ty =
{id: cx.sess.next_node_id(),
Expand Down Expand Up @@ -501,9 +531,9 @@ fn mk_test_main_call(cx: test_ctxt) -> @ast::expr {
node: test_call_expr_, span: dummy_sp()};

// Call std::test::test_main
let test_main_path = path_node_global(
let test_main_path =
mk_path(cx, ~[cx.sess.ident_of(~"test"),
cx.sess.ident_of(~"test_main")]));
cx.sess.ident_of(~"test_main")]);

let test_main_path_expr_: ast::expr_ = ast::expr_path(test_main_path);

Expand Down