]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/symbols/tests.rs
Rollup merge of #106200 - compiler-errors:suggest-impl-trait, r=estebank
[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         .find_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         .expect("did not find `symbols!` macro invocation.");
27
28     let body_tokens = m.mac.tokens.clone();
29
30     test_symbols_macro(body_tokens, &[]);
31 }
32
33 fn test_symbols_macro(input: TokenStream, expected_errors: &[&str]) {
34     let (output, found_errors) = symbols_with_errors(input);
35
36     // It should always parse.
37     let _parsed_file = syn::parse2::<syn::File>(output).unwrap();
38
39     assert_eq!(
40         found_errors.len(),
41         expected_errors.len(),
42         "Macro generated a different number of errors than expected"
43     );
44
45     for (found_error, &expected_error) in found_errors.iter().zip(expected_errors) {
46         let found_error_str = format!("{}", found_error);
47         assert_eq!(found_error_str, expected_error);
48     }
49 }
50
51 #[test]
52 fn check_dup_keywords() {
53     let input = quote! {
54         Keywords {
55             Crate: "crate",
56             Crate: "crate",
57         }
58         Symbols {}
59     };
60     test_symbols_macro(input, &["Symbol `crate` is duplicated", "location of previous definition"]);
61 }
62
63 #[test]
64 fn check_dup_symbol() {
65     let input = quote! {
66         Keywords {}
67         Symbols {
68             splat,
69             splat,
70         }
71     };
72     test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]);
73 }
74
75 #[test]
76 fn check_dup_symbol_and_keyword() {
77     let input = quote! {
78         Keywords {
79             Splat: "splat",
80         }
81         Symbols {
82             splat,
83         }
84     };
85     test_symbols_macro(input, &["Symbol `splat` is duplicated", "location of previous definition"]);
86 }
87
88 #[test]
89 fn check_symbol_order() {
90     let input = quote! {
91         Keywords {}
92         Symbols {
93             zebra,
94             aardvark,
95         }
96     };
97     test_symbols_macro(
98         input,
99         &["Symbol `aardvark` must precede `zebra`", "location of previous symbol `zebra`"],
100     );
101 }