Collapse Verbatim representation in snapshot tests

This commit is contained in:
David Tolnay 2020-05-26 15:34:14 -07:00
parent fb242347db
commit 535bed0e57
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 43 additions and 35 deletions

View File

@ -157,15 +157,23 @@ fn expand_impl_body(defs: &Definitions, node: &Node, name: &str) -> TokenStream
}
}
} else if fields.len() == 1 {
let ty = &fields[0];
let val = quote!(_val);
let format = format_field(&val, ty).map(|format| {
quote! {
formatter.write_str("(")?;
Debug::fmt(#format, formatter)?;
formatter.write_str(")")?;
}
});
let format = if variant == "Verbatim" {
Some(quote! {
formatter.write_str("(`")?;
Display::fmt(#val, formatter)?;
formatter.write_str("`)")?;
})
} else {
let ty = &fields[0];
format_field(&val, ty).map(|format| {
quote! {
formatter.write_str("(")?;
Debug::fmt(#format, formatter)?;
formatter.write_str(")")?;
}
})
};
quote! {
syn::#ident::#variant(_val) => {
formatter.write_str(#v)?;
@ -298,7 +306,7 @@ pub fn generate(defs: &Definitions) -> Result<()> {
DEBUG_SRC,
quote! {
use super::{Lite, RefCast};
use std::fmt::{self, Debug};
use std::fmt::{self, Debug, Display};
#impls
},

View File

@ -2,7 +2,7 @@
// It is not intended for manual editing.
use super::{Lite, RefCast};
use std::fmt::{self, Debug};
use std::fmt::{self, Debug, Display};
impl Debug for Lite<syn::Abi> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let _val = &self.value;
@ -1039,9 +1039,9 @@ impl Debug for Lite<syn::Expr> {
}
syn::Expr::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
syn::Expr::While(_val) => {
@ -2116,9 +2116,9 @@ impl Debug for Lite<syn::ForeignItem> {
}
syn::ForeignItem::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
_ => unreachable!(),
@ -2432,9 +2432,9 @@ impl Debug for Lite<syn::ImplItem> {
}
syn::ImplItem::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
_ => unreachable!(),
@ -2940,9 +2940,9 @@ impl Debug for Lite<syn::Item> {
}
syn::Item::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
_ => unreachable!(),
@ -3437,9 +3437,9 @@ impl Debug for Lite<syn::Lit> {
}
syn::Lit::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
}
@ -3878,9 +3878,9 @@ impl Debug for Lite<syn::Pat> {
}
syn::Pat::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
syn::Pat::Wild(_val) => {
@ -4674,9 +4674,9 @@ impl Debug for Lite<syn::TraitItem> {
}
syn::TraitItem::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
_ => unreachable!(),
@ -5040,9 +5040,9 @@ impl Debug for Lite<syn::Type> {
}
syn::Type::Verbatim(_val) => {
formatter.write_str("Verbatim")?;
formatter.write_str("(")?;
Debug::fmt(Lite(_val), formatter)?;
formatter.write_str(")")?;
formatter.write_str("(`")?;
Display::fmt(_val, formatter)?;
formatter.write_str("`)")?;
Ok(())
}
_ => unreachable!(),

View File

@ -10,7 +10,7 @@ fn test_raw_operator() {
snapshot!(stmt, @r###"
Local(Local {
pat: Pat::Wild,
init: Some(Verbatim(TokenStream(`& raw const x`))),
init: Some(Verbatim(`& raw const x`)),
})
"###);
}