Skip to content

Commit

Permalink
Merge pull request #1547 from dtolnay/testparsequote
Browse files Browse the repository at this point in the history
Add parse_quote macro tests
  • Loading branch information
dtolnay authored Dec 12, 2023
2 parents 06161ba + 46172a4 commit 2ab0f6a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use proc_macro2::{Ident, Literal, TokenStream};
use ref_cast::RefCast;
use std::fmt::{self, Debug};
use std::ops::Deref;
use syn::punctuated::Punctuated;
use syn::punctuated::{self, Punctuated};

#[derive(RefCast)]
#[repr(transparent)]
Expand Down Expand Up @@ -124,6 +124,22 @@ where
}
}

impl<'a, T, P> Debug for Lite<punctuated::Pairs<'a, T, P>>
where
Lite<T>: Debug,
P: Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut list = formatter.debug_list();
for pair in self.value.clone() {
let (node, punct) = pair.into_tuple();
list.entry(Lite(node));
list.entries(punct);
}
list.finish()
}
}

struct Present;

impl Debug for Present {
Expand Down
122 changes: 122 additions & 0 deletions tests/test_parse_quote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#[macro_use]
mod macros;

use syn::punctuated::Punctuated;
use syn::{parse_quote, Attribute, Lit, Pat, Stmt, Token};

#[test]
fn test_attribute() {
let attr: Attribute = parse_quote!(#[test]);
snapshot!(attr, @r###"
Attribute {
style: AttrStyle::Outer,
meta: Meta::Path {
segments: [
PathSegment {
ident: "test",
},
],
},
}
"###);

let attr: Attribute = parse_quote!(#![no_std]);
snapshot!(attr, @r###"
Attribute {
style: AttrStyle::Inner,
meta: Meta::Path {
segments: [
PathSegment {
ident: "no_std",
},
],
},
}
"###);
}

#[test]
fn test_pat() {
let pat: Pat = parse_quote!(Some(false) | None);
snapshot!(&pat, @r###"
Pat::Or {
cases: [
Pat::TupleStruct {
path: Path {
segments: [
PathSegment {
ident: "Some",
},
],
},
elems: [
Pat::Lit(ExprLit {
lit: Lit::Bool {
value: false,
},
}),
],
},
Pat::Ident {
ident: "None",
},
],
}
"###);

let boxed_pat: Box<Pat> = parse_quote!(Some(false) | None);
assert_eq!(*boxed_pat, pat);
}

#[test]
fn test_punctuated() {
let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true);
snapshot!(punctuated.pairs(), @r###"
[
Lit::Bool {
value: true,
},
Or,
Lit::Bool {
value: true,
},
]
"###);

let punctuated: Punctuated<Lit, Token![|]> = parse_quote!(true | true |);
snapshot!(punctuated.pairs(), @r###"
[
Lit::Bool {
value: true,
},
Or,
Lit::Bool {
value: true,
},
Or,
]
"###);
}

#[test]
fn test_vec_stmt() {
let stmts: Vec<Stmt> = parse_quote! {
let _;
true
};
snapshot!(stmts, @r###"
[
Stmt::Local {
pat: Pat::Wild,
},
Stmt::Expr(
Expr::Lit {
lit: Lit::Bool {
value: true,
},
},
None,
),
]
"###);
}

0 comments on commit 2ab0f6a

Please sign in to comment.