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)]
15 use self::HasTestSignature::*;
22 use attr::AttrMetaMethods;
24 use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
28 use entry::{self, EntryPointType};
29 use ext::base::ExtCtxt;
30 use ext::build::AstBuilder;
31 use ext::expand::ExpansionConfig;
32 use fold::{Folder, MoveMap};
34 use owned_slice::OwnedSlice;
35 use parse::token::{intern, InternedString};
36 use parse::{token, ParseSess};
40 use util::small_vector::SmallVector;
44 Yes(Option<InternedString>),
49 path: Vec<ast::Ident> ,
52 should_panic: ShouldPanic
57 span_diagnostic: &'a diagnostic::SpanHandler,
58 path: Vec<ast::Ident>,
61 reexport_test_harness_main: Option<InternedString>,
63 config: ast::CrateConfig,
65 // top-level re-export submodule, filled out after folding is finished
66 toplevel_reexport: Option<ast::Ident>,
69 // Traverse the crate, collecting all the test functions, eliding any
70 // existing main functions, and synthesizing a main test harness
71 pub fn modify_for_testing(sess: &ParseSess,
72 cfg: &ast::CrateConfig,
74 span_diagnostic: &diagnostic::SpanHandler) -> ast::Crate {
75 // We generate the test harness when building in the 'test'
76 // configuration, either with the '--test' or '--cfg test'
77 // command line options.
78 let should_test = attr::contains_name(&krate.config, "test");
80 // Check for #[reexport_test_harness_main = "some_name"] which
81 // creates a `use some_name = __test::main;`. This needs to be
82 // unconditional, so that the attribute is still marked as used in
84 let reexport_test_harness_main =
85 attr::first_attr_value_str_by_name(&krate.attrs,
86 "reexport_test_harness_main");
89 generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
91 strip_test_functions(krate)
95 struct TestHarnessGenerator<'a> {
97 tests: Vec<ast::Ident>,
99 // submodule name, gensym'd identifier for re-exports
100 tested_submods: Vec<(ast::Ident, ast::Ident)>,
103 impl<'a> fold::Folder for TestHarnessGenerator<'a> {
104 fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
105 let mut folded = fold::noop_fold_crate(c, self);
107 // Add a special __test module to the crate that will contain code
108 // generated for the test harness
109 let (mod_, reexport) = mk_test_module(&mut self.cx);
111 Some(re) => folded.module.items.push(re),
114 folded.module.items.push(mod_);
118 fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
120 if ident.name != token::special_idents::invalid.name {
121 self.cx.path.push(ident);
123 debug!("current path: {}",
124 ast_util::path_name_i(&self.cx.path));
126 let i = if is_test_fn(&self.cx, &*i) || is_bench_fn(&self.cx, &*i) {
128 ast::ItemFn(_, ast::Unsafety::Unsafe, _, _, _, _) => {
129 let diag = self.cx.span_diagnostic;
130 panic!(diag.span_fatal(i.span, "unsafe functions cannot be used for tests"));
133 debug!("this is a test function");
136 path: self.cx.path.clone(),
137 bench: is_bench_fn(&self.cx, &*i),
138 ignore: is_ignored(&*i),
139 should_panic: should_panic(&*i)
141 self.cx.testfns.push(test);
142 self.tests.push(i.ident);
143 // debug!("have {} test/bench functions",
144 // cx.testfns.len());
146 // Make all tests public so we can call them from outside
147 // the module (note that the tests are re-exported and must
148 // be made public themselves to avoid privacy errors).
159 // We don't want to recurse into anything other than mods, since
160 // mods or tests inside of functions will break things
161 let res = match i.node {
162 ast::ItemMod(..) => fold::noop_fold_item(i, self),
163 _ => SmallVector::one(i),
165 if ident.name != token::special_idents::invalid.name {
171 fn fold_mod(&mut self, m: ast::Mod) -> ast::Mod {
172 let tests = mem::replace(&mut self.tests, Vec::new());
173 let tested_submods = mem::replace(&mut self.tested_submods, Vec::new());
174 let mut mod_folded = fold::noop_fold_mod(m, self);
175 let tests = mem::replace(&mut self.tests, tests);
176 let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
178 if !tests.is_empty() || !tested_submods.is_empty() {
179 let (it, sym) = mk_reexport_mod(&mut self.cx, tests, tested_submods);
180 mod_folded.items.push(it);
182 if !self.cx.path.is_empty() {
183 self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
185 debug!("pushing nothing, sym: {:?}", sym);
186 self.cx.toplevel_reexport = Some(sym);
194 struct EntryPointCleaner {
195 // Current depth in the ast
199 impl fold::Folder for EntryPointCleaner {
200 fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
202 let folded = fold::noop_fold_item(i, self).expect_one("noop did something");
205 // Remove any #[main] or #[start] from the AST so it doesn't
206 // clash with the one we're going to add, but mark it as
207 // #[allow(dead_code)] to avoid printing warnings.
208 let folded = match entry::entry_point_type(&*folded, self.depth) {
209 EntryPointType::MainNamed |
210 EntryPointType::MainAttr |
211 EntryPointType::Start =>
212 folded.map(|ast::Item {id, ident, attrs, node, vis, span}| {
213 let allow_str = InternedString::new("allow");
214 let dead_code_str = InternedString::new("dead_code");
215 let allow_dead_code_item =
216 attr::mk_list_item(allow_str,
217 vec![attr::mk_word_item(dead_code_str)]);
218 let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
219 allow_dead_code_item);
224 attrs: attrs.into_iter()
226 !attr.check_name("main") && !attr.check_name("start")
228 .chain(iter::once(allow_dead_code))
235 EntryPointType::None |
236 EntryPointType::OtherMain => folded,
239 SmallVector::one(folded)
243 fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
244 tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) {
245 let super_ = token::str_to_ident("super");
247 let items = tests.into_iter().map(|r| {
248 cx.ext_cx.item_use_simple(DUMMY_SP, ast::Public,
249 cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
250 }).chain(tested_submods.into_iter().map(|(r, sym)| {
251 let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
252 cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Public, r, path)
255 let reexport_mod = ast::Mod {
257 items: items.collect(),
260 let sym = token::gensym_ident("__test_reexports");
261 let it = P(ast::Item {
264 id: ast::DUMMY_NODE_ID,
265 node: ast::ItemMod(reexport_mod),
273 fn generate_test_harness(sess: &ParseSess,
274 reexport_test_harness_main: Option<InternedString>,
276 cfg: &ast::CrateConfig,
277 sd: &diagnostic::SpanHandler) -> ast::Crate {
278 // Remove the entry points
279 let mut cleaner = EntryPointCleaner { depth: 0 };
280 let krate = cleaner.fold_crate(krate);
282 let mut feature_gated_cfgs = vec![];
283 let mut cx: TestCtxt = TestCtxt {
286 ext_cx: ExtCtxt::new(sess, cfg.clone(),
287 ExpansionConfig::default("test".to_string()),
288 &mut feature_gated_cfgs),
291 reexport_test_harness_main: reexport_test_harness_main,
292 is_test_crate: is_test_crate(&krate),
293 config: krate.config.clone(),
294 toplevel_reexport: None,
296 cx.ext_cx.crate_root = Some("std");
298 cx.ext_cx.bt_push(ExpnInfo {
300 callee: NameAndSpan {
301 format: MacroAttribute(intern("test")),
303 allow_internal_unstable: false,
307 let mut fold = TestHarnessGenerator {
310 tested_submods: Vec::new(),
312 let res = fold.fold_crate(krate);
313 fold.cx.ext_cx.bt_pop();
317 fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
318 // When not compiling with --test we should not compile the
320 config::strip_items(krate, |attrs| {
321 !attr::contains_name(&attrs[..], "test") &&
322 !attr::contains_name(&attrs[..], "bench")
326 /// Craft a span that will be ignored by the stability lint's
327 /// call to codemap's is_internal check.
328 /// The expanded code calls some unstable functions in the test crate.
329 fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
330 let info = ExpnInfo {
332 callee: NameAndSpan {
333 format: MacroAttribute(intern("test")),
335 allow_internal_unstable: true,
338 let expn_id = cx.sess.codemap().record_expansion(info);
340 sp.expn_id = expn_id;
345 enum HasTestSignature {
352 fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
353 let has_test_attr = attr::contains_name(&i.attrs, "test");
355 fn has_test_signature(i: &ast::Item) -> HasTestSignature {
357 &ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
358 let no_output = match decl.output {
359 ast::DefaultReturn(..) => true,
360 ast::Return(ref t) if t.node == ast::TyTup(vec![]) => true,
363 if decl.inputs.is_empty()
365 && !generics.is_parameterized() {
371 _ => NotEvenAFunction,
376 let diag = cx.span_diagnostic;
377 match has_test_signature(i) {
379 No => diag.span_err(i.span, "functions used as tests must have signature fn() -> ()"),
380 NotEvenAFunction => diag.span_err(i.span,
381 "only functions may be used as tests"),
385 return has_test_attr && has_test_signature(i) == Yes;
388 fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
389 let has_bench_attr = attr::contains_name(&i.attrs, "bench");
391 fn has_test_signature(i: &ast::Item) -> bool {
393 ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
394 let input_cnt = decl.inputs.len();
395 let no_output = match decl.output {
396 ast::DefaultReturn(..) => true,
397 ast::Return(ref t) if t.node == ast::TyTup(vec![]) => true,
400 let tparm_cnt = generics.ty_params.len();
401 // NB: inadequate check, but we're running
402 // well before resolve, can't get too deep.
404 && no_output && tparm_cnt == 0
410 if has_bench_attr && !has_test_signature(i) {
411 let diag = cx.span_diagnostic;
412 diag.span_err(i.span, "functions used as benches must have signature \
413 `fn(&mut Bencher) -> ()`");
416 return has_bench_attr && has_test_signature(i);
419 fn is_ignored(i: &ast::Item) -> bool {
420 i.attrs.iter().any(|attr| attr.check_name("ignore"))
423 fn should_panic(i: &ast::Item) -> ShouldPanic {
424 match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
426 let msg = attr.meta_item_list()
427 .and_then(|list| list.iter().find(|mi| mi.check_name("expected")))
428 .and_then(|mi| mi.value_str());
429 ShouldPanic::Yes(msg)
431 None => ShouldPanic::No,
437 We're going to be building a module that looks more or less like:
440 extern crate test (name = "test", vers = "...");
442 test::test_main_static(&::os::args()[], tests)
445 static tests : &'static [test::TestDescAndFn] = &[
446 ... the list of tests in the crate ...
452 fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
453 let id_test = token::str_to_ident("test");
454 let (vi, vis, ident) = if cx.is_test_crate {
456 P(nospan(ast::ViewPathSimple(id_test,
457 path_node(vec!(id_test)))))),
458 ast::Public, token::special_idents::invalid)
460 (ast::ItemExternCrate(None), ast::Inherited, id_test)
463 id: ast::DUMMY_NODE_ID,
472 fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
473 // Writing this out by hand with 'ignored_span':
476 // use std::slice::AsSlice;
477 // test::test_main_static(::std::os::args().as_slice(), TESTS);
480 let sp = ignored_span(cx, DUMMY_SP);
481 let ecx = &cx.ext_cx;
483 // test::test_main_static
484 let test_main_path = ecx.path(sp, vec![token::str_to_ident("test"),
485 token::str_to_ident("test_main_static")]);
486 // test::test_main_static(...)
487 let test_main_path_expr = ecx.expr_path(test_main_path);
488 let tests_ident_expr = ecx.expr_ident(sp, token::str_to_ident("TESTS"));
489 let call_test_main = ecx.expr_call(sp, test_main_path_expr,
490 vec![tests_ident_expr]);
491 let call_test_main = ecx.stmt_expr(call_test_main);
493 let main_meta = ecx.meta_word(sp, token::intern_and_get_ident("main"));
494 let main_attr = ecx.attribute(sp, main_meta);
495 // pub fn main() { ... }
496 let main_ret_ty = ecx.ty(sp, ast::TyTup(vec![]));
497 let main_body = ecx.block_all(sp, vec![call_test_main], None);
498 let main = ast::ItemFn(ecx.fn_decl(vec![], main_ret_ty),
499 ast::Unsafety::Normal,
500 ast::Constness::NotConst,
501 ::abi::Rust, empty_generics(), main_body);
502 let main = P(ast::Item {
503 ident: token::str_to_ident("main"),
504 attrs: vec![main_attr],
505 id: ast::DUMMY_NODE_ID,
514 fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
515 // Link to test crate
516 let import = mk_std(cx);
518 // A constant vector of test descriptors.
519 let tests = mk_tests(cx);
521 // The synthesized main function which will call the console test runner
522 // with our list of tests
523 let mainfn = mk_main(cx);
525 let testmod = ast::Mod {
527 items: vec![import, mainfn, tests],
529 let item_ = ast::ItemMod(testmod);
531 let mod_ident = token::gensym_ident("__test");
532 let item = P(ast::Item {
533 id: ast::DUMMY_NODE_ID,
540 let reexport = cx.reexport_test_harness_main.as_ref().map(|s| {
541 // building `use <ident> = __test::main`
542 let reexport_ident = token::str_to_ident(&s);
545 nospan(ast::ViewPathSimple(reexport_ident,
546 path_node(vec![mod_ident, token::str_to_ident("main")])));
549 id: ast::DUMMY_NODE_ID,
550 ident: token::special_idents::invalid,
552 node: ast::ItemUse(P(use_path)),
558 debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&*item));
563 fn nospan<T>(t: T) -> codemap::Spanned<T> {
564 codemap::Spanned { node: t, span: DUMMY_SP }
567 fn path_node(ids: Vec<ast::Ident> ) -> ast::Path {
571 segments: ids.into_iter().map(|identifier| ast::PathSegment {
572 identifier: identifier,
573 parameters: ast::PathParameters::none(),
578 fn mk_tests(cx: &TestCtxt) -> P<ast::Item> {
579 // The vector of test_descs for this crate
580 let test_descs = mk_test_descs(cx);
582 // FIXME #15962: should be using quote_item, but that stringifies
583 // __test_reexports, causing it to be reinterned, losing the
584 // gensym information.
586 let ecx = &cx.ext_cx;
587 let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
588 ecx.ident_of("test"),
589 ecx.ident_of("TestDescAndFn")]));
590 let static_lt = ecx.lifetime(sp, token::special_idents::static_lifetime.name);
591 // &'static [self::test::TestDescAndFn]
592 let static_type = ecx.ty_rptr(sp,
593 ecx.ty(sp, ast::TyVec(struct_type)),
596 // static TESTS: $static_type = &[...];
598 ecx.ident_of("TESTS"),
603 fn is_test_crate(krate: &ast::Crate) -> bool {
604 match attr::find_crate_name(&krate.attrs) {
605 Some(ref s) if "test" == &s[..] => true,
610 fn mk_test_descs(cx: &TestCtxt) -> P<ast::Expr> {
611 debug!("building test vector from {} tests", cx.testfns.len());
614 id: ast::DUMMY_NODE_ID,
615 node: ast::ExprAddrOf(ast::MutImmutable,
617 id: ast::DUMMY_NODE_ID,
618 node: ast::ExprVec(cx.testfns.iter().map(|test| {
619 mk_test_desc_and_fn_rec(cx, test)
627 fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
628 // FIXME #15962: should be using quote_expr, but that stringifies
629 // __test_reexports, causing it to be reinterned, losing the
630 // gensym information.
632 let span = ignored_span(cx, test.span);
633 let path = test.path.clone();
634 let ecx = &cx.ext_cx;
635 let self_id = ecx.ident_of("self");
636 let test_id = ecx.ident_of("test");
638 // creates self::test::$name
639 let test_path = |name| {
640 ecx.path(span, vec![self_id, test_id, ecx.ident_of(name)])
642 // creates $name: $expr
643 let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr);
645 debug!("encoding {}", ast_util::path_name_i(&path[..]));
647 // path to the #[test] function: "foo::bar::baz"
648 let path_string = ast_util::path_name_i(&path[..]);
649 let name_expr = ecx.expr_str(span, token::intern_and_get_ident(&path_string[..]));
651 // self::test::StaticTestName($name_expr)
652 let name_expr = ecx.expr_call(span,
653 ecx.expr_path(test_path("StaticTestName")),
656 let ignore_expr = ecx.expr_bool(span, test.ignore);
657 let should_panic_path = |name| {
658 ecx.path(span, vec![self_id, test_id, ecx.ident_of("ShouldPanic"), ecx.ident_of(name)])
660 let fail_expr = match test.should_panic {
661 ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
662 ShouldPanic::Yes(ref msg) => {
665 let msg = ecx.expr_str(span, msg.clone());
666 let path = should_panic_path("YesWithMessage");
667 ecx.expr_call(span, ecx.expr_path(path), vec![msg])
669 None => ecx.expr_path(should_panic_path("Yes")),
674 // self::test::TestDesc { ... }
675 let desc_expr = ecx.expr_struct(
677 test_path("TestDesc"),
678 vec![field("name", name_expr),
679 field("ignore", ignore_expr),
680 field("should_panic", fail_expr)]);
683 let mut visible_path = match cx.toplevel_reexport {
684 Some(id) => vec![id],
686 let diag = cx.span_diagnostic;
687 diag.handler.bug("expected to find top-level re-export name, but found None");
690 visible_path.extend(path);
692 let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));
694 let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };
695 // self::test::$variant_name($fn_expr)
696 let testfn_expr = ecx.expr_call(span, ecx.expr_path(test_path(variant_name)), vec![fn_expr]);
698 // self::test::TestDescAndFn { ... }
699 ecx.expr_struct(span,
700 test_path("TestDescAndFn"),
701 vec![field("desc", desc_expr),
702 field("testfn", testfn_expr)])