1fad3a1d3Sopenharmony_ci#![allow(clippy::uninlined_format_args)]
2fad3a1d3Sopenharmony_ci
3fad3a1d3Sopenharmony_ci#[macro_use]
4fad3a1d3Sopenharmony_cimod macros;
5fad3a1d3Sopenharmony_ci
6fad3a1d3Sopenharmony_ciuse proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
7fad3a1d3Sopenharmony_ciuse quote::{quote, ToTokens as _};
8fad3a1d3Sopenharmony_ciuse syn::parse::Parser;
9fad3a1d3Sopenharmony_ciuse syn::punctuated::Punctuated;
10fad3a1d3Sopenharmony_ciuse syn::{parse_quote, token, Item, Pat, PatTuple, Stmt, Token};
11fad3a1d3Sopenharmony_ci
12fad3a1d3Sopenharmony_ci#[test]
13fad3a1d3Sopenharmony_cifn test_pat_ident() {
14fad3a1d3Sopenharmony_ci    match Pat::parse_single.parse2(quote!(self)).unwrap() {
15fad3a1d3Sopenharmony_ci        Pat::Ident(_) => (),
16fad3a1d3Sopenharmony_ci        value => panic!("expected PatIdent, got {:?}", value),
17fad3a1d3Sopenharmony_ci    }
18fad3a1d3Sopenharmony_ci}
19fad3a1d3Sopenharmony_ci
20fad3a1d3Sopenharmony_ci#[test]
21fad3a1d3Sopenharmony_cifn test_pat_path() {
22fad3a1d3Sopenharmony_ci    match Pat::parse_single.parse2(quote!(self::CONST)).unwrap() {
23fad3a1d3Sopenharmony_ci        Pat::Path(_) => (),
24fad3a1d3Sopenharmony_ci        value => panic!("expected PatPath, got {:?}", value),
25fad3a1d3Sopenharmony_ci    }
26fad3a1d3Sopenharmony_ci}
27fad3a1d3Sopenharmony_ci
28fad3a1d3Sopenharmony_ci#[test]
29fad3a1d3Sopenharmony_cifn test_leading_vert() {
30fad3a1d3Sopenharmony_ci    // https://github.com/rust-lang/rust/blob/1.43.0/src/test/ui/or-patterns/remove-leading-vert.rs
31fad3a1d3Sopenharmony_ci
32fad3a1d3Sopenharmony_ci    syn::parse_str::<Item>("fn f() {}").unwrap();
33fad3a1d3Sopenharmony_ci    syn::parse_str::<Item>("fn fun1(| A: E) {}").unwrap_err();
34fad3a1d3Sopenharmony_ci    syn::parse_str::<Item>("fn fun2(|| A: E) {}").unwrap_err();
35fad3a1d3Sopenharmony_ci
36fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let | () = ();").unwrap_err();
37fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let (| A): E;").unwrap();
38fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let (|| A): (E);").unwrap_err();
39fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let (| A,): (E,);").unwrap();
40fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let [| A]: [E; 1];").unwrap();
41fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let [|| A]: [E; 1];").unwrap_err();
42fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let TS(| A): TS;").unwrap();
43fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let TS(|| A): TS;").unwrap_err();
44fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let NS { f: | A }: NS;").unwrap();
45fad3a1d3Sopenharmony_ci    syn::parse_str::<Stmt>("let NS { f: || A }: NS;").unwrap_err();
46fad3a1d3Sopenharmony_ci}
47fad3a1d3Sopenharmony_ci
48fad3a1d3Sopenharmony_ci#[test]
49fad3a1d3Sopenharmony_cifn test_group() {
50fad3a1d3Sopenharmony_ci    let group = Group::new(Delimiter::None, quote!(Some(_)));
51fad3a1d3Sopenharmony_ci    let tokens = TokenStream::from_iter(vec![TokenTree::Group(group)]);
52fad3a1d3Sopenharmony_ci    let pat = Pat::parse_single.parse2(tokens).unwrap();
53fad3a1d3Sopenharmony_ci
54fad3a1d3Sopenharmony_ci    snapshot!(pat, @r###"
55fad3a1d3Sopenharmony_ci    Pat::TupleStruct {
56fad3a1d3Sopenharmony_ci        path: Path {
57fad3a1d3Sopenharmony_ci            segments: [
58fad3a1d3Sopenharmony_ci                PathSegment {
59fad3a1d3Sopenharmony_ci                    ident: "Some",
60fad3a1d3Sopenharmony_ci                },
61fad3a1d3Sopenharmony_ci            ],
62fad3a1d3Sopenharmony_ci        },
63fad3a1d3Sopenharmony_ci        elems: [
64fad3a1d3Sopenharmony_ci            Pat::Wild,
65fad3a1d3Sopenharmony_ci        ],
66fad3a1d3Sopenharmony_ci    }
67fad3a1d3Sopenharmony_ci    "###);
68fad3a1d3Sopenharmony_ci}
69fad3a1d3Sopenharmony_ci
70fad3a1d3Sopenharmony_ci#[test]
71fad3a1d3Sopenharmony_cifn test_ranges() {
72fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("..").unwrap();
73fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("..hi").unwrap();
74fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo..").unwrap();
75fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo..hi").unwrap();
76fad3a1d3Sopenharmony_ci
77fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("..=").unwrap_err();
78fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("..=hi").unwrap();
79fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo..=").unwrap_err();
80fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo..=hi").unwrap();
81fad3a1d3Sopenharmony_ci
82fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("...").unwrap_err();
83fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("...hi").unwrap_err();
84fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo...").unwrap_err();
85fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("lo...hi").unwrap();
86fad3a1d3Sopenharmony_ci
87fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[lo..]").unwrap_err();
88fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[..=hi]").unwrap_err();
89fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[(lo..)]").unwrap();
90fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[(..=hi)]").unwrap();
91fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[lo..=hi]").unwrap();
92fad3a1d3Sopenharmony_ci
93fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[_, lo.., _]").unwrap_err();
94fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[_, ..=hi, _]").unwrap_err();
95fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[_, (lo..), _]").unwrap();
96fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[_, (..=hi), _]").unwrap();
97fad3a1d3Sopenharmony_ci    Pat::parse_single.parse_str("[_, lo..=hi, _]").unwrap();
98fad3a1d3Sopenharmony_ci}
99fad3a1d3Sopenharmony_ci
100fad3a1d3Sopenharmony_ci#[test]
101fad3a1d3Sopenharmony_cifn test_tuple_comma() {
102fad3a1d3Sopenharmony_ci    let mut expr = PatTuple {
103fad3a1d3Sopenharmony_ci        attrs: Vec::new(),
104fad3a1d3Sopenharmony_ci        paren_token: token::Paren::default(),
105fad3a1d3Sopenharmony_ci        elems: Punctuated::new(),
106fad3a1d3Sopenharmony_ci    };
107fad3a1d3Sopenharmony_ci    snapshot!(expr.to_token_stream() as Pat, @"Pat::Tuple");
108fad3a1d3Sopenharmony_ci
109fad3a1d3Sopenharmony_ci    expr.elems.push_value(parse_quote!(_));
110fad3a1d3Sopenharmony_ci    // Must not parse to Pat::Paren
111fad3a1d3Sopenharmony_ci    snapshot!(expr.to_token_stream() as Pat, @r###"
112fad3a1d3Sopenharmony_ci    Pat::Tuple {
113fad3a1d3Sopenharmony_ci        elems: [
114fad3a1d3Sopenharmony_ci            Pat::Wild,
115fad3a1d3Sopenharmony_ci            Token![,],
116fad3a1d3Sopenharmony_ci        ],
117fad3a1d3Sopenharmony_ci    }
118fad3a1d3Sopenharmony_ci    "###);
119fad3a1d3Sopenharmony_ci
120fad3a1d3Sopenharmony_ci    expr.elems.push_punct(<Token![,]>::default());
121fad3a1d3Sopenharmony_ci    snapshot!(expr.to_token_stream() as Pat, @r###"
122fad3a1d3Sopenharmony_ci    Pat::Tuple {
123fad3a1d3Sopenharmony_ci        elems: [
124fad3a1d3Sopenharmony_ci            Pat::Wild,
125fad3a1d3Sopenharmony_ci            Token![,],
126fad3a1d3Sopenharmony_ci        ],
127fad3a1d3Sopenharmony_ci    }
128fad3a1d3Sopenharmony_ci    "###);
129fad3a1d3Sopenharmony_ci
130fad3a1d3Sopenharmony_ci    expr.elems.push_value(parse_quote!(_));
131fad3a1d3Sopenharmony_ci    snapshot!(expr.to_token_stream() as Pat, @r###"
132fad3a1d3Sopenharmony_ci    Pat::Tuple {
133fad3a1d3Sopenharmony_ci        elems: [
134fad3a1d3Sopenharmony_ci            Pat::Wild,
135fad3a1d3Sopenharmony_ci            Token![,],
136fad3a1d3Sopenharmony_ci            Pat::Wild,
137fad3a1d3Sopenharmony_ci        ],
138fad3a1d3Sopenharmony_ci    }
139fad3a1d3Sopenharmony_ci    "###);
140fad3a1d3Sopenharmony_ci
141fad3a1d3Sopenharmony_ci    expr.elems.push_punct(<Token![,]>::default());
142fad3a1d3Sopenharmony_ci    snapshot!(expr.to_token_stream() as Pat, @r###"
143fad3a1d3Sopenharmony_ci    Pat::Tuple {
144fad3a1d3Sopenharmony_ci        elems: [
145fad3a1d3Sopenharmony_ci            Pat::Wild,
146fad3a1d3Sopenharmony_ci            Token![,],
147fad3a1d3Sopenharmony_ci            Pat::Wild,
148fad3a1d3Sopenharmony_ci            Token![,],
149fad3a1d3Sopenharmony_ci        ],
150fad3a1d3Sopenharmony_ci    }
151fad3a1d3Sopenharmony_ci    "###);
152fad3a1d3Sopenharmony_ci}
153