From 0fcdad044f330f4c32e2f3b1230271669d1951ea Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 12 Dec 2023 14:42:22 -0800 Subject: [PATCH 1/2] Support punctuated Pairs iterator in snapshot tests --- tests/debug/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/debug/mod.rs b/tests/debug/mod.rs index caf9eed804..707de0e2f0 100644 --- a/tests/debug/mod.rs +++ b/tests/debug/mod.rs @@ -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)] @@ -124,6 +124,22 @@ where } } +impl<'a, T, P> Debug for Lite> +where + Lite: 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 { From 46172a41a478920ff23c9e370a5b922fa984829d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 12 Dec 2023 14:21:54 -0800 Subject: [PATCH 2/2] Add parse_quote tests --- tests/test_parse_quote.rs | 122 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/test_parse_quote.rs diff --git a/tests/test_parse_quote.rs b/tests/test_parse_quote.rs new file mode 100644 index 0000000000..1c9120646b --- /dev/null +++ b/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 = parse_quote!(Some(false) | None); + assert_eq!(*boxed_pat, pat); +} + +#[test] +fn test_punctuated() { + let punctuated: Punctuated = parse_quote!(true | true); + snapshot!(punctuated.pairs(), @r###" + [ + Lit::Bool { + value: true, + }, + Or, + Lit::Bool { + value: true, + }, + ] + "###); + + let punctuated: Punctuated = 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 = parse_quote! { + let _; + true + }; + snapshot!(stmts, @r###" + [ + Stmt::Local { + pat: Pat::Wild, + }, + Stmt::Expr( + Expr::Lit { + lit: Lit::Bool { + value: true, + }, + }, + None, + ), + ] + "###); +}