]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/test_case.rs
syntax_ext: Reuse built-in attribute template checking for macro attributes
[rust.git] / src / libsyntax_ext / test_case.rs
1 // http://rust-lang.org/COPYRIGHT.
2 //
3
4 // #[test_case] is used by custom test authors to mark tests
5 // When building for test, it needs to make the item public and gensym the name
6 // Otherwise, we'll omit the item. This behavior means that any item annotated
7 // with #[test_case] is never addressable.
8 //
9 // We mark item with an inert attribute "rustc_test_marker" which the test generation
10 // logic will pick up on.
11
12 use syntax::ast;
13 use syntax::attr::check_builtin_macro_attribute;
14 use syntax::ext::base::*;
15 use syntax::ext::build::AstBuilder;
16 use syntax::ext::hygiene::SyntaxContext;
17 use syntax::source_map::respan;
18 use syntax::symbol::sym;
19 use syntax_pos::Span;
20
21 pub fn expand(
22     ecx: &mut ExtCtxt<'_>,
23     attr_sp: Span,
24     meta_item: &ast::MetaItem,
25     anno_item: Annotatable
26 ) -> Vec<Annotatable> {
27     check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
28
29     if !ecx.ecfg.should_test { return vec![]; }
30
31     let sp = attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.id));
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 = item.ident.gensym();
36         item.attrs.push(
37             ecx.attribute(sp,
38                 ecx.meta_word(sp, sym::rustc_test_marker))
39         );
40         item
41     });
42
43     return vec![Annotatable::Item(item)]
44 }