]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/test.rs
Rollup merge of #66297 - vakaras:edit-queries, r=oli-obk
[rust.git] / src / libsyntax_ext / test.rs
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
4 use crate::util::check_builtin_macro_attribute;
5
6 use syntax::ast;
7 use syntax::attr;
8 use syntax_expand::base::*;
9 use syntax::print::pprust;
10 use syntax::source_map::respan;
11 use syntax::symbol::{Symbol, sym};
12 use syntax_pos::Span;
13
14 use std::iter;
15
16 // #[test_case] is used by custom test authors to mark tests
17 // When building for test, it needs to make the item public and gensym the name
18 // Otherwise, we'll omit the item. This behavior means that any item annotated
19 // with #[test_case] is never addressable.
20 //
21 // We mark item with an inert attribute "rustc_test_marker" which the test generation
22 // logic will pick up on.
23 pub fn expand_test_case(
24     ecx: &mut ExtCtxt<'_>,
25     attr_sp: Span,
26     meta_item: &ast::MetaItem,
27     anno_item: Annotatable
28 ) -> Vec<Annotatable> {
29     check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
30
31     if !ecx.ecfg.should_test { return vec![]; }
32
33     let sp = ecx.with_def_site_ctxt(attr_sp);
34     let mut item = anno_item.expect_item();
35     item = item.map(|mut item| {
36         item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
37         item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
38         item.attrs.push(
39             ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker))
40         );
41         item
42     });
43
44     return vec![Annotatable::Item(item)]
45 }
46
47 pub fn expand_test(
48     cx: &mut ExtCtxt<'_>,
49     attr_sp: Span,
50     meta_item: &ast::MetaItem,
51     item: Annotatable,
52 ) -> Vec<Annotatable> {
53     check_builtin_macro_attribute(cx, meta_item, sym::test);
54     expand_test_or_bench(cx, attr_sp, item, false)
55 }
56
57 pub fn expand_bench(
58     cx: &mut ExtCtxt<'_>,
59     attr_sp: Span,
60     meta_item: &ast::MetaItem,
61     item: Annotatable,
62 ) -> Vec<Annotatable> {
63     check_builtin_macro_attribute(cx, meta_item, sym::bench);
64     expand_test_or_bench(cx, attr_sp, item, true)
65 }
66
67 pub fn expand_test_or_bench(
68     cx: &mut ExtCtxt<'_>,
69     attr_sp: Span,
70     item: Annotatable,
71     is_bench: bool
72 ) -> Vec<Annotatable> {
73     // If we're not in test configuration, remove the annotated item
74     if !cx.ecfg.should_test { return vec![]; }
75
76     let item =
77         if let Annotatable::Item(i) = item { i }
78         else {
79             cx.parse_sess.span_diagnostic.span_fatal(item.span(),
80                 "`#[test]` attribute is only allowed on non associated functions").raise();
81         };
82
83     if let ast::ItemKind::Mac(_) = item.kind {
84         cx.parse_sess.span_diagnostic.span_warn(item.span,
85             "`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.");
86         return vec![Annotatable::Item(item)];
87     }
88
89     // has_*_signature will report any errors in the type so compilation
90     // will fail. We shouldn't try to expand in this case because the errors
91     // would be spurious.
92     if (!is_bench && !has_test_signature(cx, &item)) ||
93         (is_bench && !has_bench_signature(cx, &item)) {
94         return vec![Annotatable::Item(item)];
95     }
96
97     let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
98
99     let test_id = ast::Ident::new(sym::test, attr_sp);
100
101     // creates test::$name
102     let test_path = |name| {
103         cx.path(sp, vec![test_id, cx.ident_of(name, sp)])
104     };
105
106     // creates test::ShouldPanic::$name
107     let should_panic_path = |name| {
108         cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)])
109     };
110
111     // creates test::TestType::$name
112     let test_type_path = |name| {
113         cx.path(sp, vec![test_id, cx.ident_of("TestType", sp), cx.ident_of(name, sp)])
114     };
115
116     // creates $name: $expr
117     let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr);
118
119     let test_fn = if is_bench {
120         // A simple ident for a lambda
121         let b = cx.ident_of("b", attr_sp);
122
123         cx.expr_call(sp, cx.expr_path(test_path("StaticBenchFn")), vec![
124             // |b| self::test::assert_test_result(
125             cx.lambda1(sp,
126                 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
127                     // super::$test_fn(b)
128                     cx.expr_call(sp,
129                         cx.expr_path(cx.path(sp, vec![item.ident])),
130                         vec![cx.expr_ident(sp, b)])
131                 ]),
132                 b
133             )
134             // )
135         ])
136     } else {
137         cx.expr_call(sp, cx.expr_path(test_path("StaticTestFn")), vec![
138             // || {
139             cx.lambda0(sp,
140                 // test::assert_test_result(
141                 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
142                     // $test_fn()
143                     cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![])
144                 // )
145                 ])
146             // }
147             )
148         // )
149         ])
150     };
151
152     let mut test_const = cx.item(sp, ast::Ident::new(item.ident.name, sp),
153         vec![
154             // #[cfg(test)]
155             cx.attribute(attr::mk_list_item(ast::Ident::new(sym::cfg, attr_sp), vec![
156                 attr::mk_nested_word_item(ast::Ident::new(sym::test, attr_sp))
157             ])),
158             // #[rustc_test_marker]
159             cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
160         ],
161         // const $ident: test::TestDescAndFn =
162         ast::ItemKind::Const(cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
163             // test::TestDescAndFn {
164             cx.expr_struct(sp, test_path("TestDescAndFn"), vec![
165                 // desc: test::TestDesc {
166                 field("desc", cx.expr_struct(sp, test_path("TestDesc"), vec![
167                     // name: "path::to::test"
168                     field("name", cx.expr_call(sp, cx.expr_path(test_path("StaticTestName")),
169                         vec![
170                             cx.expr_str(sp, Symbol::intern(&item_path(
171                                 // skip the name of the root module
172                                 &cx.current_expansion.module.mod_path[1..],
173                                 &item.ident
174                             )))
175                         ])),
176                     // ignore: true | false
177                     field("ignore", cx.expr_bool(sp, should_ignore(&item))),
178                     // allow_fail: true | false
179                     field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
180                     // should_panic: ...
181                     field("should_panic", match should_panic(cx, &item) {
182                         // test::ShouldPanic::No
183                         ShouldPanic::No => cx.expr_path(should_panic_path("No")),
184                         // test::ShouldPanic::Yes
185                         ShouldPanic::Yes(None) => cx.expr_path(should_panic_path("Yes")),
186                         // test::ShouldPanic::YesWithMessage("...")
187                         ShouldPanic::Yes(Some(sym)) => cx.expr_call(sp,
188                             cx.expr_path(should_panic_path("YesWithMessage")),
189                             vec![cx.expr_str(sp, sym)]),
190                     }),
191                     // test_type: ...
192                     field("test_type", match test_type(cx) {
193                         // test::TestType::UnitTest
194                         TestType::UnitTest => cx.expr_path(test_type_path("UnitTest")),
195                         // test::TestType::IntegrationTest
196                         TestType::IntegrationTest => cx.expr_path(
197                             test_type_path("IntegrationTest")
198                         ),
199                         // test::TestPath::Unknown
200                         TestType::Unknown => cx.expr_path(test_type_path("Unknown")),
201                     }),
202                 // },
203                 ])),
204                 // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
205                 field("testfn", test_fn)
206             // }
207             ])
208         // }
209         ));
210     test_const = test_const.map(|mut tc| { tc.vis.node = ast::VisibilityKind::Public; tc});
211
212     // extern crate test
213     let test_extern = cx.item(sp,
214         test_id,
215         vec![],
216         ast::ItemKind::ExternCrate(None)
217     );
218
219     log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
220
221     vec![
222         // Access to libtest under a hygienic name
223         Annotatable::Item(test_extern),
224         // The generated test case
225         Annotatable::Item(test_const),
226         // The original item
227         Annotatable::Item(item)
228     ]
229 }
230
231 fn item_path(mod_path: &[ast::Ident], item_ident: &ast::Ident) -> String {
232     mod_path.iter().chain(iter::once(item_ident))
233         .map(|x| x.to_string()).collect::<Vec<String>>().join("::")
234 }
235
236 enum ShouldPanic {
237     No,
238     Yes(Option<Symbol>),
239 }
240
241 fn should_ignore(i: &ast::Item) -> bool {
242     attr::contains_name(&i.attrs, sym::ignore)
243 }
244
245 fn should_fail(i: &ast::Item) -> bool {
246     attr::contains_name(&i.attrs, sym::allow_fail)
247 }
248
249 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
250     match attr::find_by_name(&i.attrs, sym::should_panic) {
251         Some(attr) => {
252             let ref sd = cx.parse_sess.span_diagnostic;
253
254             match attr.meta_item_list() {
255                 // Handle #[should_panic(expected = "foo")]
256                 Some(list) => {
257                     let msg = list.iter()
258                         .find(|mi| mi.check_name(sym::expected))
259                         .and_then(|mi| mi.meta_item())
260                         .and_then(|mi| mi.value_str());
261                     if list.len() != 1 || msg.is_none() {
262                         sd.struct_span_warn(
263                             attr.span,
264                             "argument must be of the form: \
265                              `expected = \"error message\"`"
266                         ).note("Errors in this attribute were erroneously \
267                                 allowed and will become a hard error in a \
268                                 future release.").emit();
269                         ShouldPanic::Yes(None)
270                     } else {
271                         ShouldPanic::Yes(msg)
272                     }
273                 },
274                 // Handle #[should_panic] and #[should_panic = "expected"]
275                 None => ShouldPanic::Yes(attr.value_str())
276             }
277         }
278         None => ShouldPanic::No,
279     }
280 }
281
282 enum TestType {
283     UnitTest,
284     IntegrationTest,
285     Unknown,
286 }
287
288 /// Attempts to determine the type of test.
289 /// Since doctests are created without macro expanding, only possible variants here
290 /// are `UnitTest`, `IntegrationTest` or `Unknown`.
291 fn test_type(cx: &ExtCtxt<'_>) -> TestType {
292     // Root path from context contains the topmost sources directory of the crate.
293     // I.e., for `project` with sources in `src` and tests in `tests` folders
294     // (no matter how many nested folders lie inside),
295     // there will be two different root paths: `/project/src` and `/project/tests`.
296     let crate_path = cx.root_path.as_path();
297
298     if crate_path.ends_with("src") {
299         // `/src` folder contains unit-tests.
300         TestType::UnitTest
301     } else if crate_path.ends_with("tests") {
302         // `/tests` folder contains integration tests.
303         TestType::IntegrationTest
304     } else {
305         // Crate layout doesn't match expected one, test type is unknown.
306         TestType::Unknown
307     }
308 }
309
310 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
311     let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
312     let ref sd = cx.parse_sess.span_diagnostic;
313     if let ast::ItemKind::Fn(ref sig, ref generics, _) = i.kind {
314         if sig.header.unsafety == ast::Unsafety::Unsafe {
315             sd.span_err(
316                 i.span,
317                 "unsafe functions cannot be used for tests"
318             );
319             return false
320         }
321         if sig.header.asyncness.node.is_async() {
322             sd.span_err(
323                 i.span,
324                 "async functions cannot be used for tests"
325             );
326             return false
327         }
328
329
330         // If the termination trait is active, the compiler will check that the output
331         // type implements the `Termination` trait as `libtest` enforces that.
332         let has_output = match sig.decl.output {
333             ast::FunctionRetTy::Default(..) => false,
334             ast::FunctionRetTy::Ty(ref t) if t.kind.is_unit() => false,
335             _ => true
336         };
337
338         if !sig.decl.inputs.is_empty() {
339             sd.span_err(i.span, "functions used as tests can not have any arguments");
340             return false;
341         }
342
343         match (has_output, has_should_panic_attr) {
344             (true, true) => {
345                 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
346                 false
347             },
348             (true, false) => if !generics.params.is_empty() {
349                 sd.span_err(i.span,
350                                 "functions used as tests must have signature fn() -> ()");
351                 false
352             } else {
353                 true
354             },
355             (false, _) => true
356         }
357     } else {
358         sd.span_err(i.span, "only functions may be used as tests");
359         false
360     }
361 }
362
363 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
364     let has_sig = if let ast::ItemKind::Fn(ref sig, _, _) = i.kind {
365         // N.B., inadequate check, but we're running
366         // well before resolve, can't get too deep.
367         sig.decl.inputs.len() == 1
368     } else {
369         false
370     };
371
372     if !has_sig {
373         cx.parse_sess.span_diagnostic.span_err(i.span, "functions used as benches must have \
374             signature `fn(&mut Bencher) -> impl Termination`");
375     }
376
377     has_sig
378 }