]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/symbols/tests.rs
Auto merge of #80965 - camelid:rename-doc-spotlight, r=jyn514
[rust.git] / compiler / rustc_macros / src / symbols / tests.rs
1 use super::*;
2
3 // This test is mainly here for interactive development. Use this test while
4 // you're working on the proc-macro defined in this file.
5 #[test]
6 fn test_symbols() {
7     // We textually include the symbol.rs file, which contains the list of all
8     // symbols, keywords, and common words. Then we search for the
9     // `symbols! { ... }` call.
10
11     static SYMBOL_RS_FILE: &str = include_str!("../../../rustc_span/src/symbol.rs");
12
13     let file = syn::parse_file(SYMBOL_RS_FILE).unwrap();
14     let symbols_path: syn::Path = syn::parse_quote!(symbols);
15
16     let m: &syn::ItemMacro = file
17         .items
18         .iter()
19         .filter_map(|i| {
20             if let syn::Item::Macro(m) = i {
21                 if m.mac.path == symbols_path { Some(m) } else { None }
22             } else {
23                 None
24             }
25         })
26         .next()
27         .expect("did not find `symbols!` macro invocation.");
28
29     let body_tokens = m.mac.tokens.clone();
30
31     test_symbols_macro(body_tokens, &[]);
32 }
33
34 fn test_symbols_macro(input: TokenStream, expected_errors: &[&str]) {
35     let (output, found_errors) = symbols_with_errors(input);
36
37     // It should always parse.
38     let _parsed_file = syn::parse2::<syn::File>(output).unwrap();
39
40     assert_eq!(
41         found_errors.len(),
42         expected_errors.len(),
43         "Macro generated a different number of errors than expected"
44     );
45
46     for (found_error, &expected_error) in found_errors.iter().zip(expected_errors) {
47         let found_error_str = format!("{}", found_error);
48         assert_eq!(found_error_str, expected_error);
49     }
50 }
51
52 #[test]
53 fn check_dup_keywords() {
54     let input = quote! {
55         Keywords {
56             Crate: "crate",
57             Crate: "crate",
58         }
59         Symbols {}
60     };
61     test_symbols_macro(input, &["Symbol `crate` is duplicated", "location of previous definition"]);
62 }
63
64 #[test]
65 fn check_dup_symbol() {
66     let input = quote! {
67         Keywords {}
68         Symbols {
69             splat,
70             splat,
71         }
72     };
73     test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]);
74 }
75
76 #[test]
77 fn check_dup_symbol_and_keyword() {
78     let input = quote! {
79         Keywords {
80             Splat: "splat",
81         }
82         Symbols {
83             splat,
84         }
85     };
86     test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]);
87 }
88
89 #[test]
90 fn check_symbol_order() {
91     let input = quote! {
92         Keywords {}
93         Symbols {
94             zebra,
95             aardvark,
96         }
97     };
98     test_symbols_macro(
99         input,
100         &["Symbol `aardvark` must precede `zebra`", "location of previous symbol `zebra`"],
101     );
102 }