#[track_caller] pub(crate) fn assert_parse_ok_eq<T: PartialEq + Debug + Display>(
input: &str,
result: Result<T, ParseError>,
expected: T,
parse_method: &str,
) { match result {
Ok(actual) if actual == expected => { if actual.to_string() != input {
panic!( "formatting does not yield original input `{}`: {:?}",
input,
actual,
);
}
}
Ok(actual) => {
panic!( "unexpected parsing result (with `{}`) for `{}`:\nactual: {:?}\nexpected: {:?}",
parse_method,
input,
actual,
expected,
);
}
Err(e) => {
panic!( "expected `{}` to be parsed (with `{}`) successfully, but it failed: {:?}",
input,
parse_method,
e,
);
}
}
}
// This is not ideal, but to perform this check we need `proc-macro2`. So we // just don't do anything if that feature is not enabled. #[cfg(not(feature = "proc-macro2"))] pub(crate) fn assert_roundtrip<T>(_: T, _: &str) {}
#[cfg(feature = "proc-macro2")] #[track_caller] pub(crate) fn assert_roundtrip<T>(ours: T, input: &str) where
T: std::convert::TryFrom<proc_macro2::Literal> + fmt::Debug + PartialEq + Clone,
proc_macro2::Literal: From<T>,
<T as std::convert::TryFrom<proc_macro2::Literal>>::Error: std::fmt::Display,
{ let pm_lit = input.parse::<proc_macro2::Literal>()
.expect("failed to parse input as proc_macro2::Literal"); let t_name = std::any::type_name::<T>();
// Unfortunately, `proc_macro2::Literal` does not implement `PartialEq`, so // this is the next best thing. if proc_macro2::Literal::from(ours.clone()).to_string() != pm_lit.to_string() {
panic!( "Converting {} to proc_macro2::Literal has unexpected result:\
\nconverted: {:?}\nexpected: {:?}",
t_name,
proc_macro2::Literal::from(ours),
pm_lit,
);
}
match T::try_from(pm_lit) {
Err(e) => {
panic!("Trying to convert proc_macro2::Literal to {} results in error: {}", t_name, e);
}
Ok(res) => { if res != ours {
panic!( "Converting proc_macro2::Literal to {} has unexpected result:\
\nactual: {:?}\nexpected: {:?}",
t_name,
res,
ours,
);
}
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.