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.
3 use crate::util::check_builtin_macro_attribute;
7 use rustc_ast_pretty::pprust;
8 use rustc_expand::base::*;
9 use rustc_span::source_map::respan;
10 use rustc_span::symbol::{sym, Ident, Symbol};
15 // #[test_case] is used by custom test authors to mark tests
16 // When building for test, it needs to make the item public and gensym the name
17 // Otherwise, we'll omit the item. This behavior means that any item annotated
18 // with #[test_case] is never addressable.
20 // We mark item with an inert attribute "rustc_test_marker" which the test generation
21 // logic will pick up on.
22 pub fn expand_test_case(
23 ecx: &mut ExtCtxt<'_>,
25 meta_item: &ast::MetaItem,
26 anno_item: Annotatable,
27 ) -> Vec<Annotatable> {
28 check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
30 if !ecx.ecfg.should_test {
34 let sp = ecx.with_def_site_ctxt(attr_sp);
35 let mut item = anno_item.expect_item();
36 item = item.map(|mut item| {
37 item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
38 item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
39 item.attrs.push(ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker)));
43 return vec![Annotatable::Item(item)];
49 meta_item: &ast::MetaItem,
51 ) -> Vec<Annotatable> {
52 check_builtin_macro_attribute(cx, meta_item, sym::test);
53 expand_test_or_bench(cx, attr_sp, item, false)
59 meta_item: &ast::MetaItem,
61 ) -> Vec<Annotatable> {
62 check_builtin_macro_attribute(cx, meta_item, sym::bench);
63 expand_test_or_bench(cx, attr_sp, item, true)
66 pub fn expand_test_or_bench(
71 ) -> Vec<Annotatable> {
72 // If we're not in test configuration, remove the annotated item
73 if !cx.ecfg.should_test {
77 let item = match item {
78 Annotatable::Item(i) => i,
82 "`#[test]` attribute is only allowed on non associated functions",
89 if let ast::ItemKind::MacCall(_) = item.kind {
90 cx.parse_sess.span_diagnostic.span_warn(
92 "`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.",
94 return vec![Annotatable::Item(item)];
97 // has_*_signature will report any errors in the type so compilation
98 // will fail. We shouldn't try to expand in this case because the errors
100 if (!is_bench && !has_test_signature(cx, &item))
101 || (is_bench && !has_bench_signature(cx, &item))
103 return vec![Annotatable::Item(item)];
106 let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
108 let test_id = Ident::new(sym::test, attr_sp);
110 // creates test::$name
111 let test_path = |name| cx.path(sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
113 // creates test::ShouldPanic::$name
114 let should_panic_path = |name| {
119 Ident::from_str_and_span("ShouldPanic", sp),
120 Ident::from_str_and_span(name, sp),
125 // creates test::TestType::$name
126 let test_type_path = |name| {
131 Ident::from_str_and_span("TestType", sp),
132 Ident::from_str_and_span(name, sp),
137 // creates $name: $expr
138 let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
140 let test_fn = if is_bench {
141 // A simple ident for a lambda
142 let b = Ident::from_str_and_span("b", attr_sp);
146 cx.expr_path(test_path("StaticBenchFn")),
148 // |b| self::test::assert_test_result(
153 cx.expr_path(test_path("assert_test_result")),
155 // super::$test_fn(b)
158 cx.expr_path(cx.path(sp, vec![item.ident])),
159 vec![cx.expr_ident(sp, b)],
170 cx.expr_path(test_path("StaticTestFn")),
175 // test::assert_test_result(
178 cx.expr_path(test_path("assert_test_result")),
181 cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]), // )
189 let mut test_const = cx.item(
191 Ident::new(item.ident.name, sp),
194 cx.attribute(attr::mk_list_item(
195 Ident::new(sym::cfg, attr_sp),
196 vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))],
198 // #[rustc_test_marker]
199 cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
201 // const $ident: test::TestDescAndFn =
202 ast::ItemKind::Const(
203 ast::Defaultness::Final,
204 cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
205 // test::TestDescAndFn {
209 test_path("TestDescAndFn"),
211 // desc: test::TestDesc {
216 test_path("TestDesc"),
218 // name: "path::to::test"
223 cx.expr_path(test_path("StaticTestName")),
226 Symbol::intern(&item_path(
227 // skip the name of the root module
228 &cx.current_expansion.module.mod_path[1..],
234 // ignore: true | false
235 field("ignore", cx.expr_bool(sp, should_ignore(&item))),
236 // allow_fail: true | false
237 field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
241 match should_panic(cx, &item) {
242 // test::ShouldPanic::No
244 cx.expr_path(should_panic_path("No"))
246 // test::ShouldPanic::Yes
247 ShouldPanic::Yes(None) => {
248 cx.expr_path(should_panic_path("Yes"))
250 // test::ShouldPanic::YesWithMessage("...")
251 ShouldPanic::Yes(Some(sym)) => cx.expr_call(
253 cx.expr_path(should_panic_path("YesWithMessage")),
254 vec![cx.expr_str(sp, sym)],
261 match test_type(cx) {
262 // test::TestType::UnitTest
263 TestType::UnitTest => {
264 cx.expr_path(test_type_path("UnitTest"))
266 // test::TestType::IntegrationTest
267 TestType::IntegrationTest => {
268 cx.expr_path(test_type_path("IntegrationTest"))
270 // test::TestPath::Unknown
271 TestType::Unknown => {
272 cx.expr_path(test_type_path("Unknown"))
280 // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
281 field("testfn", test_fn), // }
287 test_const = test_const.map(|mut tc| {
288 tc.vis.node = ast::VisibilityKind::Public;
293 let test_extern = cx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None));
295 log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
298 // Access to libtest under a hygienic name
299 Annotatable::Item(test_extern),
300 // The generated test case
301 Annotatable::Item(test_const),
303 Annotatable::Item(item),
307 fn item_path(mod_path: &[Ident], item_ident: &Ident) -> String {
310 .chain(iter::once(item_ident))
311 .map(|x| x.to_string())
312 .collect::<Vec<String>>()
321 fn should_ignore(i: &ast::Item) -> bool {
322 attr::contains_name(&i.attrs, sym::ignore)
325 fn should_fail(i: &ast::Item) -> bool {
326 attr::contains_name(&i.attrs, sym::allow_fail)
329 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
330 match attr::find_by_name(&i.attrs, sym::should_panic) {
332 let sd = &cx.parse_sess.span_diagnostic;
334 match attr.meta_item_list() {
335 // Handle #[should_panic(expected = "foo")]
339 .find(|mi| mi.check_name(sym::expected))
340 .and_then(|mi| mi.meta_item())
341 .and_then(|mi| mi.value_str());
342 if list.len() != 1 || msg.is_none() {
345 "argument must be of the form: \
346 `expected = \"error message\"`",
349 "errors in this attribute were erroneously \
350 allowed and will become a hard error in a \
354 ShouldPanic::Yes(None)
356 ShouldPanic::Yes(msg)
359 // Handle #[should_panic] and #[should_panic = "expected"]
360 None => ShouldPanic::Yes(attr.value_str()),
363 None => ShouldPanic::No,
373 /// Attempts to determine the type of test.
374 /// Since doctests are created without macro expanding, only possible variants here
375 /// are `UnitTest`, `IntegrationTest` or `Unknown`.
376 fn test_type(cx: &ExtCtxt<'_>) -> TestType {
377 // Root path from context contains the topmost sources directory of the crate.
378 // I.e., for `project` with sources in `src` and tests in `tests` folders
379 // (no matter how many nested folders lie inside),
380 // there will be two different root paths: `/project/src` and `/project/tests`.
381 let crate_path = cx.root_path.as_path();
383 if crate_path.ends_with("src") {
384 // `/src` folder contains unit-tests.
386 } else if crate_path.ends_with("tests") {
387 // `/tests` folder contains integration tests.
388 TestType::IntegrationTest
390 // Crate layout doesn't match expected one, test type is unknown.
395 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
396 let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
397 let sd = &cx.parse_sess.span_diagnostic;
398 if let ast::ItemKind::Fn(_, ref sig, ref generics, _) = i.kind {
399 if let ast::Unsafe::Yes(span) = sig.header.unsafety {
400 sd.struct_span_err(i.span, "unsafe functions cannot be used for tests")
401 .span_label(span, "`unsafe` because of this")
405 if let ast::Async::Yes { span, .. } = sig.header.asyncness {
406 sd.struct_span_err(i.span, "async functions cannot be used for tests")
407 .span_label(span, "`async` because of this")
412 // If the termination trait is active, the compiler will check that the output
413 // type implements the `Termination` trait as `libtest` enforces that.
414 let has_output = match sig.decl.output {
415 ast::FnRetTy::Default(..) => false,
416 ast::FnRetTy::Ty(ref t) if t.kind.is_unit() => false,
420 if !sig.decl.inputs.is_empty() {
421 sd.span_err(i.span, "functions used as tests can not have any arguments");
425 match (has_output, has_should_panic_attr) {
427 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
431 if !generics.params.is_empty() {
432 sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
441 sd.span_err(i.span, "only functions may be used as tests");
446 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
447 let has_sig = if let ast::ItemKind::Fn(_, ref sig, _, _) = i.kind {
448 // N.B., inadequate check, but we're running
449 // well before resolve, can't get too deep.
450 sig.decl.inputs.len() == 1
456 cx.parse_sess.span_diagnostic.span_err(
458 "functions used as benches must have \
459 signature `fn(&mut Bencher) -> impl Termination`",