1 /// The expansion from a test function to the appropriate test struct for libtest
2 /// Ideally, this code would be in libtest but for efficiency and error messages it lives here.
5 use syntax::attr::{self, check_builtin_macro_attribute};
6 use syntax::ext::base::*;
7 use syntax::print::pprust;
8 use syntax::source_map::respan;
9 use syntax::symbol::{Symbol, sym};
14 // #[test_case] is used by custom test authors to mark tests
15 // When building for test, it needs to make the item public and gensym the name
16 // Otherwise, we'll omit the item. This behavior means that any item annotated
17 // with #[test_case] is never addressable.
19 // We mark item with an inert attribute "rustc_test_marker" which the test generation
20 // logic will pick up on.
21 pub fn expand_test_case(
22 ecx: &mut ExtCtxt<'_>,
24 meta_item: &ast::MetaItem,
25 anno_item: Annotatable
26 ) -> Vec<Annotatable> {
27 check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
29 if !ecx.ecfg.should_test { return vec![]; }
31 let sp = ecx.with_def_site_ctxt(attr_sp);
32 let mut item = anno_item.expect_item();
33 item = item.map(|mut item| {
34 item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
35 item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
37 ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker))
42 return vec![Annotatable::Item(item)]
48 meta_item: &ast::MetaItem,
50 ) -> Vec<Annotatable> {
51 check_builtin_macro_attribute(cx, meta_item, sym::test);
52 expand_test_or_bench(cx, attr_sp, item, false)
58 meta_item: &ast::MetaItem,
60 ) -> Vec<Annotatable> {
61 check_builtin_macro_attribute(cx, meta_item, sym::bench);
62 expand_test_or_bench(cx, attr_sp, item, true)
65 pub fn expand_test_or_bench(
70 ) -> Vec<Annotatable> {
71 // If we're not in test configuration, remove the annotated item
72 if !cx.ecfg.should_test { return vec![]; }
75 if let Annotatable::Item(i) = item { i }
77 cx.parse_sess.span_diagnostic.span_fatal(item.span(),
78 "`#[test]` attribute is only allowed on non associated functions").raise();
81 if let ast::ItemKind::Mac(_) = item.node {
82 cx.parse_sess.span_diagnostic.span_warn(item.span,
83 "`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.");
84 return vec![Annotatable::Item(item)];
87 // has_*_signature will report any errors in the type so compilation
88 // will fail. We shouldn't try to expand in this case because the errors
90 if (!is_bench && !has_test_signature(cx, &item)) ||
91 (is_bench && !has_bench_signature(cx, &item)) {
92 return vec![Annotatable::Item(item)];
95 let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
97 let test_id = ast::Ident::new(sym::test, attr_sp);
99 // creates test::$name
100 let test_path = |name| {
101 cx.path(sp, vec![test_id, cx.ident_of(name, sp)])
104 // creates test::ShouldPanic::$name
105 let should_panic_path = |name| {
106 cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)])
109 // creates $name: $expr
110 let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr);
112 let test_fn = if is_bench {
113 // A simple ident for a lambda
114 let b = cx.ident_of("b", attr_sp);
116 cx.expr_call(sp, cx.expr_path(test_path("StaticBenchFn")), vec![
117 // |b| self::test::assert_test_result(
119 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
120 // super::$test_fn(b)
122 cx.expr_path(cx.path(sp, vec![item.ident])),
123 vec![cx.expr_ident(sp, b)])
130 cx.expr_call(sp, cx.expr_path(test_path("StaticTestFn")), vec![
133 // test::assert_test_result(
134 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
136 cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![])
145 let mut test_const = cx.item(sp, ast::Ident::new(item.ident.name, sp),
148 cx.attribute(cx.meta_list(attr_sp, sym::cfg, vec![
149 cx.meta_list_item_word(attr_sp, sym::test)
151 // #[rustc_test_marker]
152 cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
154 // const $ident: test::TestDescAndFn =
155 ast::ItemKind::Const(cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
156 // test::TestDescAndFn {
157 cx.expr_struct(sp, test_path("TestDescAndFn"), vec![
158 // desc: test::TestDesc {
159 field("desc", cx.expr_struct(sp, test_path("TestDesc"), vec![
160 // name: "path::to::test"
161 field("name", cx.expr_call(sp, cx.expr_path(test_path("StaticTestName")),
163 cx.expr_str(sp, Symbol::intern(&item_path(
164 // skip the name of the root module
165 &cx.current_expansion.module.mod_path[1..],
169 // ignore: true | false
170 field("ignore", cx.expr_bool(sp, should_ignore(&item))),
171 // allow_fail: true | false
172 field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
174 field("should_panic", match should_panic(cx, &item) {
175 // test::ShouldPanic::No
176 ShouldPanic::No => cx.expr_path(should_panic_path("No")),
177 // test::ShouldPanic::Yes
178 ShouldPanic::Yes(None) => cx.expr_path(should_panic_path("Yes")),
179 // test::ShouldPanic::YesWithMessage("...")
180 ShouldPanic::Yes(Some(sym)) => cx.expr_call(sp,
181 cx.expr_path(should_panic_path("YesWithMessage")),
182 vec![cx.expr_str(sp, sym)]),
186 // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
187 field("testfn", test_fn)
192 test_const = test_const.map(|mut tc| { tc.vis.node = ast::VisibilityKind::Public; tc});
195 let test_extern = cx.item(sp,
198 ast::ItemKind::ExternCrate(None)
201 log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
204 // Access to libtest under a hygienic name
205 Annotatable::Item(test_extern),
206 // The generated test case
207 Annotatable::Item(test_const),
209 Annotatable::Item(item)
213 fn item_path(mod_path: &[ast::Ident], item_ident: &ast::Ident) -> String {
214 mod_path.iter().chain(iter::once(item_ident))
215 .map(|x| x.to_string()).collect::<Vec<String>>().join("::")
223 fn should_ignore(i: &ast::Item) -> bool {
224 attr::contains_name(&i.attrs, sym::ignore)
227 fn should_fail(i: &ast::Item) -> bool {
228 attr::contains_name(&i.attrs, sym::allow_fail)
231 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
232 match attr::find_by_name(&i.attrs, sym::should_panic) {
234 let ref sd = cx.parse_sess.span_diagnostic;
236 match attr.meta_item_list() {
237 // Handle #[should_panic(expected = "foo")]
239 let msg = list.iter()
240 .find(|mi| mi.check_name(sym::expected))
241 .and_then(|mi| mi.meta_item())
242 .and_then(|mi| mi.value_str());
243 if list.len() != 1 || msg.is_none() {
246 "argument must be of the form: \
247 `expected = \"error message\"`"
248 ).note("Errors in this attribute were erroneously \
249 allowed and will become a hard error in a \
250 future release.").emit();
251 ShouldPanic::Yes(None)
253 ShouldPanic::Yes(msg)
256 // Handle #[should_panic] and #[should_panic = "expected"]
257 None => ShouldPanic::Yes(attr.value_str())
260 None => ShouldPanic::No,
264 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
265 let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
266 let ref sd = cx.parse_sess.span_diagnostic;
267 if let ast::ItemKind::Fn(ref decl, ref header, ref generics, _) = i.node {
268 if header.unsafety == ast::Unsafety::Unsafe {
271 "unsafe functions cannot be used for tests"
275 if header.asyncness.node.is_async() {
278 "async functions cannot be used for tests"
284 // If the termination trait is active, the compiler will check that the output
285 // type implements the `Termination` trait as `libtest` enforces that.
286 let has_output = match decl.output {
287 ast::FunctionRetTy::Default(..) => false,
288 ast::FunctionRetTy::Ty(ref t) if t.node.is_unit() => false,
292 if !decl.inputs.is_empty() {
293 sd.span_err(i.span, "functions used as tests can not have any arguments");
297 match (has_output, has_should_panic_attr) {
299 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
302 (true, false) => if !generics.params.is_empty() {
304 "functions used as tests must have signature fn() -> ()");
312 sd.span_err(i.span, "only functions may be used as tests");
317 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
318 let has_sig = if let ast::ItemKind::Fn(ref decl, _, _, _) = i.node {
319 // N.B., inadequate check, but we're running
320 // well before resolve, can't get too deep.
321 decl.inputs.len() == 1
327 cx.parse_sess.span_diagnostic.span_err(i.span, "functions used as benches must have \
328 signature `fn(&mut Bencher) -> impl Termination`");