1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Code that generates a test runner to run all the tests in a crate
14 #![allow(unused_imports)]
16 use self::HasTestSignature::*;
22 use attr::{self, HasAttrs};
23 use syntax_pos::{self, DUMMY_SP, NO_EXPANSION, Span, FileMap, BytePos};
26 use codemap::{self, CodeMap, ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned};
28 use errors::snippet::{SnippetData};
30 use entry::{self, EntryPointType};
31 use ext::base::{ExtCtxt, Resolver};
32 use ext::build::AstBuilder;
33 use ext::expand::ExpansionConfig;
35 use util::move_map::MoveMap;
37 use parse::{token, ParseSess};
39 use ast::{self, Ident};
41 use symbol::{self, Symbol, keywords};
42 use util::small_vector::SmallVector;
54 should_panic: ShouldPanic
59 span_diagnostic: &'a errors::Handler,
63 reexport_test_harness_main: Option<Symbol>,
66 // top-level re-export submodule, filled out after folding is finished
67 toplevel_reexport: Option<Ident>,
70 // Traverse the crate, collecting all the test functions, eliding any
71 // existing main functions, and synthesizing a main test harness
72 pub fn modify_for_testing(sess: &ParseSess,
73 resolver: &mut Resolver,
76 span_diagnostic: &errors::Handler) -> ast::Crate {
77 // Check for #[reexport_test_harness_main = "some_name"] which
78 // creates a `use some_name = __test::main;`. This needs to be
79 // unconditional, so that the attribute is still marked as used in
81 let reexport_test_harness_main =
82 attr::first_attr_value_str_by_name(&krate.attrs,
83 "reexport_test_harness_main");
86 generate_test_harness(sess, resolver, reexport_test_harness_main, krate, span_diagnostic)
92 struct TestHarnessGenerator<'a> {
96 // submodule name, gensym'd identifier for re-exports
97 tested_submods: Vec<(Ident, Ident)>,
100 impl<'a> fold::Folder for TestHarnessGenerator<'a> {
101 fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
102 let mut folded = fold::noop_fold_crate(c, self);
104 // Add a special __test module to the crate that will contain code
105 // generated for the test harness
106 let (mod_, reexport) = mk_test_module(&mut self.cx);
108 Some(re) => folded.module.items.push(re),
111 folded.module.items.push(mod_);
115 fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
117 if ident.name != keywords::Invalid.name() {
118 self.cx.path.push(ident);
120 debug!("current path: {}", path_name_i(&self.cx.path));
122 if is_test_fn(&self.cx, &i) || is_bench_fn(&self.cx, &i) {
124 ast::ItemKind::Fn(_, ast::Unsafety::Unsafe, _, _, _, _) => {
125 let diag = self.cx.span_diagnostic;
126 panic!(diag.span_fatal(i.span, "unsafe functions cannot be used for tests"));
129 debug!("this is a test function");
132 path: self.cx.path.clone(),
133 bench: is_bench_fn(&self.cx, &i),
134 ignore: is_ignored(&i),
135 should_panic: should_panic(&i, &self.cx)
137 self.cx.testfns.push(test);
138 self.tests.push(i.ident);
143 let mut item = i.unwrap();
144 // We don't want to recurse into anything other than mods, since
145 // mods or tests inside of functions will break things
146 if let ast::ItemKind::Mod(module) = item.node {
147 let tests = mem::replace(&mut self.tests, Vec::new());
148 let tested_submods = mem::replace(&mut self.tested_submods, Vec::new());
149 let mut mod_folded = fold::noop_fold_mod(module, self);
150 let tests = mem::replace(&mut self.tests, tests);
151 let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
153 if !tests.is_empty() || !tested_submods.is_empty() {
154 let (it, sym) = mk_reexport_mod(&mut self.cx, item.id, tests, tested_submods);
155 mod_folded.items.push(it);
157 if !self.cx.path.is_empty() {
158 self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
160 debug!("pushing nothing, sym: {:?}", sym);
161 self.cx.toplevel_reexport = Some(sym);
164 item.node = ast::ItemKind::Mod(mod_folded);
166 if ident.name != keywords::Invalid.name() {
169 SmallVector::one(P(item))
172 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { mac }
175 struct EntryPointCleaner {
176 // Current depth in the ast
180 impl fold::Folder for EntryPointCleaner {
181 fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
183 let folded = fold::noop_fold_item(i, self).expect_one("noop did something");
186 // Remove any #[main] or #[start] from the AST so it doesn't
187 // clash with the one we're going to add, but mark it as
188 // #[allow(dead_code)] to avoid printing warnings.
189 let folded = match entry::entry_point_type(&folded, self.depth) {
190 EntryPointType::MainNamed |
191 EntryPointType::MainAttr |
192 EntryPointType::Start =>
193 folded.map(|ast::Item {id, ident, attrs, node, vis, span}| {
194 let allow_str = Symbol::intern("allow");
195 let dead_code_str = Symbol::intern("dead_code");
196 let word_vec = vec![attr::mk_list_word_item(dead_code_str)];
197 let allow_dead_code_item = attr::mk_list_item(allow_str, word_vec);
198 let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
199 allow_dead_code_item);
204 attrs: attrs.into_iter()
206 !attr.check_name("main") && !attr.check_name("start")
208 .chain(iter::once(allow_dead_code))
215 EntryPointType::None |
216 EntryPointType::OtherMain => folded,
219 SmallVector::one(folded)
222 fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { mac }
225 fn mk_reexport_mod(cx: &mut TestCtxt,
228 tested_submods: Vec<(Ident, Ident)>)
229 -> (P<ast::Item>, Ident) {
230 let super_ = Ident::from_str("super");
232 // Generate imports with `#[allow(private_in_public)]` to work around issue #36768.
233 let allow_private_in_public = cx.ext_cx.attribute(DUMMY_SP, cx.ext_cx.meta_list(
235 Symbol::intern("allow"),
236 vec![cx.ext_cx.meta_list_item_word(DUMMY_SP, Symbol::intern("private_in_public"))],
238 let items = tests.into_iter().map(|r| {
239 cx.ext_cx.item_use_simple(DUMMY_SP, ast::Visibility::Public,
240 cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
241 .map_attrs(|_| vec![allow_private_in_public.clone()])
242 }).chain(tested_submods.into_iter().map(|(r, sym)| {
243 let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
244 cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Visibility::Public, r, path)
245 .map_attrs(|_| vec![allow_private_in_public.clone()])
248 let reexport_mod = ast::Mod {
253 let sym = Ident::with_empty_ctxt(Symbol::gensym("__test_reexports"));
254 let parent = if parent == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { parent };
255 cx.ext_cx.current_expansion.mark = cx.ext_cx.resolver.get_module_scope(parent);
256 let it = cx.ext_cx.monotonic_expander().fold_item(P(ast::Item {
259 id: ast::DUMMY_NODE_ID,
260 node: ast::ItemKind::Mod(reexport_mod),
261 vis: ast::Visibility::Public,
268 fn generate_test_harness(sess: &ParseSess,
269 resolver: &mut Resolver,
270 reexport_test_harness_main: Option<Symbol>,
272 sd: &errors::Handler) -> ast::Crate {
273 // Remove the entry points
274 let mut cleaner = EntryPointCleaner { depth: 0 };
275 let krate = cleaner.fold_crate(krate);
277 let mut cx: TestCtxt = TestCtxt {
280 ext_cx: ExtCtxt::new(sess, ExpansionConfig::default("test".to_string()), resolver),
283 reexport_test_harness_main: reexport_test_harness_main,
284 is_test_crate: is_test_crate(&krate),
285 toplevel_reexport: None,
287 cx.ext_cx.crate_root = Some("std");
289 cx.ext_cx.bt_push(ExpnInfo {
291 callee: NameAndSpan {
292 format: MacroAttribute(Symbol::intern("test")),
294 allow_internal_unstable: false,
298 TestHarnessGenerator {
301 tested_submods: Vec::new(),
305 /// Craft a span that will be ignored by the stability lint's
306 /// call to codemap's is_internal check.
307 /// The expanded code calls some unstable functions in the test crate.
308 fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
309 let info = ExpnInfo {
311 callee: NameAndSpan {
312 format: MacroAttribute(Symbol::intern("test")),
314 allow_internal_unstable: true,
317 let expn_id = cx.sess.codemap().record_expansion(info);
319 sp.expn_id = expn_id;
324 enum HasTestSignature {
330 fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
331 let has_test_attr = attr::contains_name(&i.attrs, "test");
333 fn has_test_signature(i: &ast::Item) -> HasTestSignature {
335 ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => {
336 let no_output = match decl.output {
337 ast::FunctionRetTy::Default(..) => true,
338 ast::FunctionRetTy::Ty(ref t) if t.node == ast::TyKind::Tup(vec![]) => true,
341 if decl.inputs.is_empty()
343 && !generics.is_parameterized() {
349 _ => NotEvenAFunction,
354 let diag = cx.span_diagnostic;
355 match has_test_signature(i) {
357 No => diag.span_err(i.span, "functions used as tests must have signature fn() -> ()"),
358 NotEvenAFunction => diag.span_err(i.span,
359 "only functions may be used as tests"),
363 return has_test_attr && has_test_signature(i) == Yes;
366 fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
367 let has_bench_attr = attr::contains_name(&i.attrs, "bench");
369 fn has_test_signature(i: &ast::Item) -> bool {
371 ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => {
372 let input_cnt = decl.inputs.len();
373 let no_output = match decl.output {
374 ast::FunctionRetTy::Default(..) => true,
375 ast::FunctionRetTy::Ty(ref t) if t.node == ast::TyKind::Tup(vec![]) => true,
378 let tparm_cnt = generics.ty_params.len();
379 // NB: inadequate check, but we're running
380 // well before resolve, can't get too deep.
382 && no_output && tparm_cnt == 0
388 if has_bench_attr && !has_test_signature(i) {
389 let diag = cx.span_diagnostic;
390 diag.span_err(i.span, "functions used as benches must have signature \
391 `fn(&mut Bencher) -> ()`");
394 return has_bench_attr && has_test_signature(i);
397 fn is_ignored(i: &ast::Item) -> bool {
398 i.attrs.iter().any(|attr| attr.check_name("ignore"))
401 fn should_panic(i: &ast::Item, cx: &TestCtxt) -> ShouldPanic {
402 match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
404 let sd = cx.span_diagnostic;
405 if attr.is_value_str() {
408 "attribute must be of the form: \
409 `#[should_panic]` or \
410 `#[should_panic(expected = \"error message\")]`"
411 ).note("Errors in this attribute were erroneously allowed \
412 and will become a hard error in a future release.")
414 return ShouldPanic::Yes(None);
416 match attr.meta_item_list() {
417 // Handle #[should_panic]
418 None => ShouldPanic::Yes(None),
419 // Handle #[should_panic(expected = "foo")]
421 let msg = list.iter()
422 .find(|mi| mi.check_name("expected"))
423 .and_then(|mi| mi.meta_item())
424 .and_then(|mi| mi.value_str());
425 if list.len() != 1 || msg.is_none() {
428 "argument must be of the form: \
429 `expected = \"error message\"`"
430 ).note("Errors in this attribute were erroneously \
431 allowed and will become a hard error in a \
432 future release.").emit();
433 ShouldPanic::Yes(None)
435 ShouldPanic::Yes(msg)
440 None => ShouldPanic::No,
446 We're going to be building a module that looks more or less like:
449 extern crate test (name = "test", vers = "...");
451 test::test_main_static(&::os::args()[], tests)
454 static tests : &'static [test::TestDescAndFn] = &[
455 ... the list of tests in the crate ...
461 fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
462 let id_test = Ident::from_str("test");
463 let sp = ignored_span(cx, DUMMY_SP);
464 let (vi, vis, ident) = if cx.is_test_crate {
466 P(nospan(ast::ViewPathSimple(id_test,
467 path_node(vec![id_test]))))),
468 ast::Visibility::Public, keywords::Invalid.ident())
470 (ast::ItemKind::ExternCrate(None), ast::Visibility::Inherited, id_test)
473 id: ast::DUMMY_NODE_ID,
482 fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
483 // Writing this out by hand with 'ignored_span':
486 // use std::slice::AsSlice;
487 // test::test_main_static(::std::os::args().as_slice(), TESTS);
490 let sp = ignored_span(cx, DUMMY_SP);
491 let ecx = &cx.ext_cx;
493 // test::test_main_static
495 ecx.path(sp, vec![Ident::from_str("test"), Ident::from_str("test_main_static")]);
497 // test::test_main_static(...)
498 let test_main_path_expr = ecx.expr_path(test_main_path);
499 let tests_ident_expr = ecx.expr_ident(sp, Ident::from_str("TESTS"));
500 let call_test_main = ecx.expr_call(sp, test_main_path_expr,
501 vec![tests_ident_expr]);
502 let call_test_main = ecx.stmt_expr(call_test_main);
504 let main_meta = ecx.meta_word(sp, Symbol::intern("main"));
505 let main_attr = ecx.attribute(sp, main_meta);
506 // pub fn main() { ... }
507 let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(vec![]));
508 let main_body = ecx.block(sp, vec![call_test_main]);
509 let main = ast::ItemKind::Fn(ecx.fn_decl(vec![], main_ret_ty),
510 ast::Unsafety::Normal,
511 dummy_spanned(ast::Constness::NotConst),
512 ::abi::Abi::Rust, ast::Generics::default(), main_body);
513 let main = P(ast::Item {
514 ident: Ident::from_str("main"),
515 attrs: vec![main_attr],
516 id: ast::DUMMY_NODE_ID,
518 vis: ast::Visibility::Public,
525 fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
526 // Link to test crate
527 let import = mk_std(cx);
529 // A constant vector of test descriptors.
530 let tests = mk_tests(cx);
532 // The synthesized main function which will call the console test runner
533 // with our list of tests
534 let mainfn = mk_main(cx);
536 let testmod = ast::Mod {
538 items: vec![import, mainfn, tests],
540 let item_ = ast::ItemKind::Mod(testmod);
541 let mod_ident = Ident::with_empty_ctxt(Symbol::gensym("__test"));
543 let mut expander = cx.ext_cx.monotonic_expander();
544 let item = expander.fold_item(P(ast::Item {
545 id: ast::DUMMY_NODE_ID,
549 vis: ast::Visibility::Public,
552 let reexport = cx.reexport_test_harness_main.map(|s| {
553 // building `use <ident> = __test::main`
554 let reexport_ident = Ident::with_empty_ctxt(s);
557 nospan(ast::ViewPathSimple(reexport_ident,
558 path_node(vec![mod_ident, Ident::from_str("main")])));
560 expander.fold_item(P(ast::Item {
561 id: ast::DUMMY_NODE_ID,
562 ident: keywords::Invalid.ident(),
564 node: ast::ItemKind::Use(P(use_path)),
565 vis: ast::Visibility::Inherited,
570 debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&item));
575 fn nospan<T>(t: T) -> codemap::Spanned<T> {
576 codemap::Spanned { node: t, span: DUMMY_SP }
579 fn path_node(ids: Vec<Ident>) -> ast::Path {
582 segments: ids.into_iter().map(Into::into).collect(),
586 fn path_name_i(idents: &[Ident]) -> String {
587 // FIXME: Bad copies (#2543 -- same for everything else that says "bad")
588 idents.iter().map(|i| i.to_string()).collect::<Vec<String>>().join("::")
591 fn mk_tests(cx: &TestCtxt) -> P<ast::Item> {
592 // The vector of test_descs for this crate
593 let test_descs = mk_test_descs(cx);
595 // FIXME #15962: should be using quote_item, but that stringifies
596 // __test_reexports, causing it to be reinterned, losing the
597 // gensym information.
598 let sp = ignored_span(cx, DUMMY_SP);
599 let ecx = &cx.ext_cx;
600 let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
601 ecx.ident_of("test"),
602 ecx.ident_of("TestDescAndFn")]));
603 let static_lt = ecx.lifetime(sp, keywords::StaticLifetime.name());
604 // &'static [self::test::TestDescAndFn]
605 let static_type = ecx.ty_rptr(sp,
606 ecx.ty(sp, ast::TyKind::Slice(struct_type)),
608 ast::Mutability::Immutable);
609 // static TESTS: $static_type = &[...];
611 ecx.ident_of("TESTS"),
616 fn is_test_crate(krate: &ast::Crate) -> bool {
617 match attr::find_crate_name(&krate.attrs) {
618 Some(s) if "test" == &*s.as_str() => true,
623 fn mk_test_descs(cx: &TestCtxt) -> P<ast::Expr> {
624 debug!("building test vector from {} tests", cx.testfns.len());
627 id: ast::DUMMY_NODE_ID,
628 node: ast::ExprKind::AddrOf(ast::Mutability::Immutable,
630 id: ast::DUMMY_NODE_ID,
631 node: ast::ExprKind::Array(cx.testfns.iter().map(|test| {
632 mk_test_desc_and_fn_rec(cx, test)
635 attrs: ast::ThinVec::new(),
638 attrs: ast::ThinVec::new(),
642 fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
643 // FIXME #15962: should be using quote_expr, but that stringifies
644 // __test_reexports, causing it to be reinterned, losing the
645 // gensym information.
647 let span = ignored_span(cx, test.span);
648 let path = test.path.clone();
649 let ecx = &cx.ext_cx;
650 let self_id = ecx.ident_of("self");
651 let test_id = ecx.ident_of("test");
653 // creates self::test::$name
654 let test_path = |name| {
655 ecx.path(span, vec![self_id, test_id, ecx.ident_of(name)])
657 // creates $name: $expr
658 let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr);
660 debug!("encoding {}", path_name_i(&path[..]));
662 // path to the #[test] function: "foo::bar::baz"
663 let path_string = path_name_i(&path[..]);
664 let name_expr = ecx.expr_str(span, Symbol::intern(&path_string));
666 // self::test::StaticTestName($name_expr)
667 let name_expr = ecx.expr_call(span,
668 ecx.expr_path(test_path("StaticTestName")),
671 let ignore_expr = ecx.expr_bool(span, test.ignore);
672 let should_panic_path = |name| {
673 ecx.path(span, vec![self_id, test_id, ecx.ident_of("ShouldPanic"), ecx.ident_of(name)])
675 let fail_expr = match test.should_panic {
676 ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
677 ShouldPanic::Yes(msg) => {
680 let msg = ecx.expr_str(span, msg);
681 let path = should_panic_path("YesWithMessage");
682 ecx.expr_call(span, ecx.expr_path(path), vec![msg])
684 None => ecx.expr_path(should_panic_path("Yes")),
689 // self::test::TestDesc { ... }
690 let desc_expr = ecx.expr_struct(
692 test_path("TestDesc"),
693 vec![field("name", name_expr),
694 field("ignore", ignore_expr),
695 field("should_panic", fail_expr)]);
698 let mut visible_path = match cx.toplevel_reexport {
699 Some(id) => vec![id],
701 let diag = cx.span_diagnostic;
702 diag.bug("expected to find top-level re-export name, but found None");
705 visible_path.extend(path);
707 let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));
709 let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };
710 // self::test::$variant_name($fn_expr)
711 let testfn_expr = ecx.expr_call(span, ecx.expr_path(test_path(variant_name)), vec![fn_expr]);
713 // self::test::TestDescAndFn { ... }
714 ecx.expr_struct(span,
715 test_path("TestDescAndFn"),
716 vec![field("desc", desc_expr),
717 field("testfn", testfn_expr)])