Skip to content

Commit 46172a4

Browse files
committed
Add parse_quote tests
1 parent 0fcdad0 commit 46172a4

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

tests/test_parse_quote.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#[macro_use]
2+
mod macros;
3+
4+
use syn::punctuated::Punctuated;
5+
use syn::{parse_quote, Attribute, Lit, Pat, Stmt, Token};
6+
7+
#[test]
8+
fn test_attribute() {
9+
let attr: Attribute = parse_quote!(#[test]);
10+
snapshot!(attr, @r###"
11+
Attribute {
12+
style: AttrStyle::Outer,
13+
meta: Meta::Path {
14+
segments: [
15+
PathSegment {
16+
ident: "test",
17+
},
18+
],
19+
},
20+
}
21+
"###);
22+
23+
let attr: Attribute = parse_quote!(#![no_std]);
24+
snapshot!(attr, @r###"
25+
Attribute {
26+
style: AttrStyle::Inner,
27+
meta: Meta::Path {
28+
segments: [
29+
PathSegment {
30+
ident: "no_std",
31+
},
32+
],
33+
},
34+
}
35+
"###);
36+
}
37+
38+
#[test]
39+
fn test_pat() {
40+
let pat: Pat = parse_quote!(Some(false) | None);
41+
snapshot!(&pat, @r###"
42+
Pat::Or {
43+
cases: [
44+
Pat::TupleStruct {
45+
path: Path {
46+
segments: [
47+
PathSegment {
48+
ident: "Some",
49+
},
50+
],
51+
},
52+
elems: [
53+
Pat::Lit(ExprLit {
54+
lit: Lit::Bool {
55+
value: false,
56+
},
57+
}),
58+
],
59+
},
60+
Pat::Ident {
61+
ident: "None",
62+
},
63+
],
64+
}
65+
"###);
66+
67+
let boxed_pat: Box<Pat> = parse_quote!(Some(false) | None);
68+
assert_eq!(*boxed_pat, pat);
69+
}
70+
71+
#[test]
72+
fn test_punctuated() {
73+
let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true);
74+
snapshot!(punctuated.pairs(), @r###"
75+
[
76+
Lit::Bool {
77+
value: true,
78+
},
79+
Or,
80+
Lit::Bool {
81+
value: true,
82+
},
83+
]
84+
"###);
85+
86+
let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true |);
87+
snapshot!(punctuated.pairs(), @r###"
88+
[
89+
Lit::Bool {
90+
value: true,
91+
},
92+
Or,
93+
Lit::Bool {
94+
value: true,
95+
},
96+
Or,
97+
]
98+
"###);
99+
}
100+
101+
#[test]
102+
fn test_vec_stmt() {
103+
let stmts: Vec<Stmt> = parse_quote! {
104+
let _;
105+
true
106+
};
107+
snapshot!(stmts, @r###"
108+
[
109+
Stmt::Local {
110+
pat: Pat::Wild,
111+
},
112+
Stmt::Expr(
113+
Expr::Lit {
114+
lit: Lit::Bool {
115+
value: true,
116+
},
117+
},
118+
None,
119+
),
120+
]
121+
"###);
122+
}

0 commit comments

Comments
 (0)