1 | #![allow (clippy::uninlined_format_args)] |
2 | |
3 | #[macro_use ] |
4 | mod macros; |
5 | |
6 | use proc_macro2::{Delimiter, Group, TokenStream, TokenTree}; |
7 | use quote::quote; |
8 | use syn::parse::Parser; |
9 | use syn::{Item, Pat, Stmt}; |
10 | |
11 | #[test] |
12 | fn test_pat_ident() { |
13 | match Pat::parse_single.parse2(quote!(self)).unwrap() { |
14 | Pat::Ident(_) => (), |
15 | value => panic!("expected PatIdent, got {:?}" , value), |
16 | } |
17 | } |
18 | |
19 | #[test] |
20 | fn test_pat_path() { |
21 | match Pat::parse_single.parse2(quote!(self::CONST)).unwrap() { |
22 | Pat::Path(_) => (), |
23 | value => panic!("expected PatPath, got {:?}" , value), |
24 | } |
25 | } |
26 | |
27 | #[test] |
28 | fn test_leading_vert() { |
29 | // https://github.com/rust-lang/rust/blob/1.43.0/src/test/ui/or-patterns/remove-leading-vert.rs |
30 | |
31 | syn::parse_str::<Item>("fn f() {}" ).unwrap(); |
32 | syn::parse_str::<Item>("fn fun1(| A: E) {}" ).unwrap_err(); |
33 | syn::parse_str::<Item>("fn fun2(|| A: E) {}" ).unwrap_err(); |
34 | |
35 | syn::parse_str::<Stmt>("let | () = ();" ).unwrap_err(); |
36 | syn::parse_str::<Stmt>("let (| A): E;" ).unwrap(); |
37 | syn::parse_str::<Stmt>("let (|| A): (E);" ).unwrap_err(); |
38 | syn::parse_str::<Stmt>("let (| A,): (E,);" ).unwrap(); |
39 | syn::parse_str::<Stmt>("let [| A]: [E; 1];" ).unwrap(); |
40 | syn::parse_str::<Stmt>("let [|| A]: [E; 1];" ).unwrap_err(); |
41 | syn::parse_str::<Stmt>("let TS(| A): TS;" ).unwrap(); |
42 | syn::parse_str::<Stmt>("let TS(|| A): TS;" ).unwrap_err(); |
43 | syn::parse_str::<Stmt>("let NS { f: | A }: NS;" ).unwrap(); |
44 | syn::parse_str::<Stmt>("let NS { f: || A }: NS;" ).unwrap_err(); |
45 | } |
46 | |
47 | #[test] |
48 | fn test_group() { |
49 | let group = Group::new(Delimiter::None, quote!(Some(_))); |
50 | let tokens = TokenStream::from_iter(vec![TokenTree::Group(group)]); |
51 | let pat = Pat::parse_single.parse2(tokens).unwrap(); |
52 | |
53 | snapshot!(pat, @r###" |
54 | Pat::TupleStruct { |
55 | path: Path { |
56 | segments: [ |
57 | PathSegment { |
58 | ident: "Some", |
59 | }, |
60 | ], |
61 | }, |
62 | elems: [ |
63 | Pat::Wild, |
64 | ], |
65 | } |
66 | "### ); |
67 | } |
68 | |
69 | #[test] |
70 | fn test_ranges() { |
71 | Pat::parse_single.parse_str(".." ).unwrap(); |
72 | Pat::parse_single.parse_str("..hi" ).unwrap(); |
73 | Pat::parse_single.parse_str("lo.." ).unwrap(); |
74 | Pat::parse_single.parse_str("lo..hi" ).unwrap(); |
75 | |
76 | Pat::parse_single.parse_str("..=" ).unwrap_err(); |
77 | Pat::parse_single.parse_str("..=hi" ).unwrap(); |
78 | Pat::parse_single.parse_str("lo..=" ).unwrap_err(); |
79 | Pat::parse_single.parse_str("lo..=hi" ).unwrap(); |
80 | |
81 | Pat::parse_single.parse_str("..." ).unwrap_err(); |
82 | Pat::parse_single.parse_str("...hi" ).unwrap_err(); |
83 | Pat::parse_single.parse_str("lo..." ).unwrap_err(); |
84 | Pat::parse_single.parse_str("lo...hi" ).unwrap(); |
85 | |
86 | Pat::parse_single.parse_str("[lo..]" ).unwrap_err(); |
87 | Pat::parse_single.parse_str("[..=hi]" ).unwrap_err(); |
88 | Pat::parse_single.parse_str("[(lo..)]" ).unwrap(); |
89 | Pat::parse_single.parse_str("[(..=hi)]" ).unwrap(); |
90 | Pat::parse_single.parse_str("[lo..=hi]" ).unwrap(); |
91 | |
92 | Pat::parse_single.parse_str("[_, lo.., _]" ).unwrap_err(); |
93 | Pat::parse_single.parse_str("[_, ..=hi, _]" ).unwrap_err(); |
94 | Pat::parse_single.parse_str("[_, (lo..), _]" ).unwrap(); |
95 | Pat::parse_single.parse_str("[_, (..=hi), _]" ).unwrap(); |
96 | Pat::parse_single.parse_str("[_, lo..=hi, _]" ).unwrap(); |
97 | } |
98 | |