1 // Copyright 2012 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
16 use front::std_inject::with_version;
17 use metadata::creader::Loader;
19 use std::cell::RefCell;
21 use syntax::ast_util::*;
22 use syntax::attr::AttrMetaMethods;
24 use syntax::codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
26 use syntax::ext::base::ExtCtxt;
27 use syntax::fold::Folder;
30 use syntax::parse::token::InternedString;
31 use syntax::parse::token;
32 use syntax::print::pprust;
33 use syntax::{ast, ast_util};
34 use syntax::util::small_vector::SmallVector;
45 sess: session::Session,
46 path: RefCell<~[ast::Ident]>,
48 testfns: RefCell<~[Test]>,
50 config: ast::CrateConfig,
53 // Traverse the crate, collecting all the test functions, eliding any
54 // existing main functions, and synthesizing a main test harness
55 pub fn modify_for_testing(sess: session::Session,
56 crate: ast::Crate) -> ast::Crate {
57 // We generate the test harness when building in the 'test'
58 // configuration, either with the '--test' or '--cfg test'
59 // command line options.
60 let should_test = attr::contains_name(crate.config, "test");
63 generate_test_harness(sess, crate)
65 strip_test_functions(crate)
69 struct TestHarnessGenerator<'a> {
73 impl<'a> fold::Folder for TestHarnessGenerator<'a> {
74 fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
75 let folded = fold::noop_fold_crate(c, self);
77 // Add a special __test module to the crate that will contain code
78 // generated for the test harness
80 module: add_test_module(&self.cx, &folded.module),
85 fn fold_item(&mut self, i: @ast::Item) -> SmallVector<@ast::Item> {
87 let mut path = self.cx.path.borrow_mut();
88 path.get().push(i.ident);
90 debug!("current path: {}",
91 ast_util::path_name_i(self.cx.path.get()));
93 if is_test_fn(&self.cx, i) || is_bench_fn(i) {
95 ast::ItemFn(_, purity, _, _, _)
96 if purity == ast::UnsafeFn => {
97 let sess = self.cx.sess;
98 sess.span_fatal(i.span,
99 "unsafe functions cannot be used for \
103 debug!("this is a test function");
106 path: self.cx.path.get(),
107 bench: is_bench_fn(i),
108 ignore: is_ignored(&self.cx, i),
109 should_fail: should_fail(i)
112 let mut testfns = self.cx.testfns.borrow_mut();
113 testfns.get().push(test);
115 // debug!("have {} test/bench functions",
116 // cx.testfns.len());
121 let res = fold::noop_fold_item(i, self);
123 let mut path = self.cx.path.borrow_mut();
129 fn fold_mod(&mut self, m: &ast::Mod) -> ast::Mod {
130 // Remove any #[main] from the AST so it doesn't clash with
131 // the one we're going to add. Only if compiling an executable.
133 fn nomain(cx: &TestCtxt, item: @ast::Item) -> @ast::Item {
134 if !cx.sess.building_library.get() {
136 attrs: item.attrs.iter().filter_map(|attr| {
137 if !attr.name().equiv(&("main")) {
150 let mod_nomain = ast::Mod {
151 view_items: m.view_items.clone(),
152 items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(),
155 fold::noop_fold_mod(&mod_nomain, self)
159 fn generate_test_harness(sess: session::Session, crate: ast::Crate)
161 let loader = &mut Loader::new(sess);
162 let mut cx: TestCtxt = TestCtxt {
164 ext_cx: ExtCtxt::new(sess.parse_sess, sess.opts.cfg.clone(), loader),
165 path: RefCell::new(~[]),
166 testfns: RefCell::new(~[]),
167 is_extra: is_extra(&crate),
168 config: crate.config.clone(),
171 cx.ext_cx.bt_push(ExpnInfo {
173 callee: NameAndSpan {
175 format: MacroAttribute,
180 let mut fold = TestHarnessGenerator {
183 let res = fold.fold_crate(crate);
184 fold.cx.ext_cx.bt_pop();
188 fn strip_test_functions(crate: ast::Crate) -> ast::Crate {
189 // When not compiling with --test we should not compile the
191 config::strip_items(crate, |attrs| {
192 !attr::contains_name(attrs, "test") &&
193 !attr::contains_name(attrs, "bench")
197 fn is_test_fn(cx: &TestCtxt, i: @ast::Item) -> bool {
198 let has_test_attr = attr::contains_name(i.attrs, "test");
200 fn has_test_signature(i: @ast::Item) -> bool {
202 &ast::ItemFn(ref decl, _, _, ref generics, _) => {
203 let no_output = match decl.output.node {
207 decl.inputs.is_empty()
209 && !generics.is_parameterized()
215 if has_test_attr && !has_test_signature(i) {
219 "functions used as tests must have signature fn() -> ()."
223 return has_test_attr && has_test_signature(i);
226 fn is_bench_fn(i: @ast::Item) -> bool {
227 let has_bench_attr = attr::contains_name(i.attrs, "bench");
229 fn has_test_signature(i: @ast::Item) -> bool {
231 ast::ItemFn(ref decl, _, _, ref generics, _) => {
232 let input_cnt = decl.inputs.len();
233 let no_output = match decl.output.node {
237 let tparm_cnt = generics.ty_params.len();
238 // NB: inadequate check, but we're running
239 // well before resolve, can't get too deep.
241 && no_output && tparm_cnt == 0u
247 return has_bench_attr && has_test_signature(i);
250 fn is_ignored(cx: &TestCtxt, i: @ast::Item) -> bool {
251 i.attrs.iter().any(|attr| {
252 // check ignore(cfg(foo, bar))
253 attr.name().equiv(&("ignore")) && match attr.meta_item_list() {
254 Some(ref cfgs) => attr::test_cfg(cx.config, cfgs.iter().map(|x| *x)),
260 fn should_fail(i: @ast::Item) -> bool {
261 attr::contains_name(i.attrs, "should_fail")
264 fn add_test_module(cx: &TestCtxt, m: &ast::Mod) -> ast::Mod {
265 let testmod = mk_test_module(cx);
267 items: vec::append_one(m.items.clone(), testmod),
274 We're going to be building a module that looks more or less like:
277 #[!resolve_unexported]
278 extern mod extra (name = "extra", vers = "...");
281 extra::test::test_main_static(::os::args(), tests)
284 static tests : &'static [extra::test::TestDescAndFn] = &[
285 ... the list of tests in the crate ...
291 fn mk_std(cx: &TestCtxt) -> ast::ViewItem {
292 let id_extra = cx.sess.ident_of("extra");
293 let vi = if cx.is_extra {
295 ~[@nospan(ast::ViewPathSimple(id_extra,
296 path_node(~[id_extra]),
297 ast::DUMMY_NODE_ID))])
299 ast::ViewItemExternMod(id_extra,
300 with_version("extra"),
311 fn mk_test_module(cx: &TestCtxt) -> @ast::Item {
314 let view_items = ~[mk_std(cx)];
316 // A constant vector of test descriptors.
317 let tests = mk_tests(cx);
319 // The synthesized main function which will call the console test runner
320 // with our list of tests
321 let mainfn = (quote_item!(&cx.ext_cx,
324 extra::test::test_main_static(::std::os::args(), TESTS);
328 let testmod = ast::Mod {
329 view_items: view_items,
330 items: ~[mainfn, tests],
332 let item_ = ast::ItemMod(testmod);
334 // This attribute tells resolve to let us call unexported functions
335 let resolve_unexported_str = InternedString::new("!resolve_unexported");
336 let resolve_unexported_attr =
337 attr::mk_attr(attr::mk_word_item(resolve_unexported_str));
339 let item = ast::Item {
340 ident: cx.sess.ident_of("__test"),
341 attrs: ~[resolve_unexported_attr],
342 id: ast::DUMMY_NODE_ID,
348 debug!("Synthetic test module:\n{}\n",
349 pprust::item_to_str(&item, cx.sess.intr()));
354 fn nospan<T>(t: T) -> codemap::Spanned<T> {
355 codemap::Spanned { node: t, span: DUMMY_SP }
358 fn path_node(ids: ~[ast::Ident]) -> ast::Path {
362 segments: ids.move_iter().map(|identifier| ast::PathSegment {
363 identifier: identifier,
364 lifetimes: opt_vec::Empty,
365 types: opt_vec::Empty,
370 fn path_node_global(ids: ~[ast::Ident]) -> ast::Path {
374 segments: ids.move_iter().map(|identifier| ast::PathSegment {
375 identifier: identifier,
376 lifetimes: opt_vec::Empty,
377 types: opt_vec::Empty,
382 fn mk_tests(cx: &TestCtxt) -> @ast::Item {
383 // The vector of test_descs for this crate
384 let test_descs = mk_test_descs(cx);
386 (quote_item!(&cx.ext_cx,
387 pub static TESTS : &'static [self::extra::test::TestDescAndFn] =
393 fn is_extra(crate: &ast::Crate) -> bool {
394 match attr::find_crateid(crate.attrs) {
395 Some(ref s) if "extra" == s.name => true,
400 fn mk_test_descs(cx: &TestCtxt) -> @ast::Expr {
403 let testfns = cx.testfns.borrow();
404 debug!("building test vector from {} tests", testfns.get().len());
405 for test in testfns.get().iter() {
406 descs.push(mk_test_desc_and_fn_rec(cx, test));
410 let inner_expr = @ast::Expr {
411 id: ast::DUMMY_NODE_ID,
412 node: ast::ExprVec(descs, ast::MutImmutable),
417 id: ast::DUMMY_NODE_ID,
418 node: ast::ExprVstore(inner_expr, ast::ExprVstoreSlice),
423 fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::Expr {
424 let span = test.span;
425 let path = test.path.clone();
427 debug!("encoding {}", ast_util::path_name_i(path));
429 let name_lit: ast::Lit =
430 nospan(ast::LitStr(token::intern_and_get_ident(
431 ast_util::path_name_i(path)), ast::CookedStr));
433 let name_expr = @ast::Expr {
434 id: ast::DUMMY_NODE_ID,
435 node: ast::ExprLit(@name_lit),
439 let fn_path = path_node_global(path);
441 let fn_expr = @ast::Expr {
442 id: ast::DUMMY_NODE_ID,
443 node: ast::ExprPath(fn_path),
447 let t_expr = if test.bench {
448 quote_expr!(&cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) )
450 quote_expr!(&cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) )
453 let ignore_expr = if test.ignore {
454 quote_expr!(&cx.ext_cx, true )
456 quote_expr!(&cx.ext_cx, false )
459 let fail_expr = if test.should_fail {
460 quote_expr!(&cx.ext_cx, true )
462 quote_expr!(&cx.ext_cx, false )
465 let e = quote_expr!(&cx.ext_cx,
466 self::extra::test::TestDescAndFn {
467 desc: self::extra::test::TestDesc {
468 name: self::extra::test::StaticTestName($name_expr),
469 ignore: $ignore_expr,
470 should_fail: $fail_expr