Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parse_quote macro tests #1547

Merged
merged 2 commits into from Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion tests/debug/mod.rs
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
@@ -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,
),
]
"###);
}