Skip to content

Commit afd8e5d

Browse files
committed
Inject std when --test. rust-lang#1127, rust-lang#3241
1 parent fd095f8 commit afd8e5d

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/libcore/core.rc

-8
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,6 @@ pub mod core {
246246
}
247247

248248

249-
// Similar to above. Some magic to make core testable.
250-
#[cfg(test)]
251-
mod std {
252-
extern mod std(vers = "0.6");
253-
pub use std::std::test;
254-
}
255-
256-
257249
// Local Variables:
258250
// mode: rust;
259251
// fill-column: 78;

src/librustc/front/test.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,16 @@ mod __test {
208208
*/
209209

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

251+
fn mk_std(cx: test_ctxt) -> @ast::view_item {
252+
let vers = ast::lit_str(@~"0.6");
253+
let vers = nospan(vers);
254+
let mi = ast::meta_name_value(~"vers", vers);
255+
let mi = nospan(mi);
256+
let vi = ast::view_item_use(cx.sess.ident_of(~"std"),
257+
~[@mi],
258+
cx.sess.next_node_id());
259+
let vi = {
260+
node: vi,
261+
attrs: ~[],
262+
vis: ast::private,
263+
span: dummy_sp()
264+
};
265+
266+
return @vi;
267+
}
268+
248269
fn mk_tests(cx: test_ctxt) -> @ast::item {
249270
let ret_ty = mk_test_desc_vec_ty(cx);
250271

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

274-
fn mk_path(cx: test_ctxt, path: ~[ast::ident]) -> ~[ast::ident] {
275-
// For tests that are inside of std we don't want to prefix
276-
// the paths with std::
295+
fn is_std(cx: test_ctxt) -> bool {
277296
let is_std = {
278297
let items = attr::find_linkage_metas(cx.crate.node.attrs);
279298
match attr::last_meta_item_value_str_by_name(items, ~"name") {
280299
Some(~"std") => true,
281300
_ => false
282301
}
283302
};
284-
if is_std { path }
285-
else { vec::append(~[cx.sess.ident_of(~"std")], path) }
303+
return is_std;
304+
}
305+
306+
fn mk_path(cx: test_ctxt, path: ~[ast::ident]) -> @ast::path {
307+
// For tests that are inside of std we don't want to prefix
308+
// the paths with std::
309+
if is_std(cx) { path_node_global(path) }
310+
else {
311+
path_node(
312+
~[cx.sess.ident_of(~"self"),
313+
cx.sess.ident_of(~"std")]
314+
+ path)
315+
}
286316
}
287317

288318
// The ast::Ty of ~[std::test::test_desc]
289319
fn mk_test_desc_vec_ty(cx: test_ctxt) -> @ast::Ty {
290320
let test_desc_ty_path =
291-
path_node_global(mk_path(cx, ~[cx.sess.ident_of(~"test"),
292-
cx.sess.ident_of(~"TestDesc")]));
321+
mk_path(cx, ~[cx.sess.ident_of(~"test"),
322+
cx.sess.ident_of(~"TestDesc")]);
293323

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

503533
// Call std::test::test_main
504-
let test_main_path = path_node_global(
534+
let test_main_path =
505535
mk_path(cx, ~[cx.sess.ident_of(~"test"),
506-
cx.sess.ident_of(~"test_main")]));
536+
cx.sess.ident_of(~"test_main")]);
507537

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

0 commit comments

Comments
 (0)