Debian Codegraph @

debian/rust-vendor/litrs/src/test_util.rs

raw
1 use std::fmt::{Debug, Display};
2
3 use crate::*;
4
5
6 #[track_caller]
7 pub(crate) fn assert_parse_ok_eq<T: PartialEq + Debug + Display>(
8 input: &str,
9 result: Result<T, ParseError>,
10 expected: T,
11 parse_method: &str,
12 ) {
13 match result {
14 Ok(actual) if actual == expected => {
15 if actual.to_string() != input {
16 panic!("formatting does not yield original input `{input}`: {actual:?}");
17 }
18 }
19 Ok(actual) => {
20 panic!("unexpected parsing result (with `{parse_method}`) for `{input}`:\n\
21 actual: {actual:?}\n\
22 expected: {expected:?}");
23 }
24 Err(e) => {
25 panic!("expected `{input}` to be parsed (with `{parse_method}`) successfully, \
26 but it failed: {e:?}");
27 }
28 }
29 }
30
31 // This is not ideal, but to perform this check we need `proc-macro2`. So we
32 // just don't do anything if that feature is not enabled.
33 #[cfg(not(feature = "proc-macro2"))]
34 pub(crate) fn assert_roundtrip<T>(_: T, _: &str) {}
35
36 #[cfg(feature = "proc-macro2")]
37 #[track_caller]
38 pub(crate) fn assert_roundtrip<T>(ours: T, input: &str)
39 where
40 T: std::convert::TryFrom<proc_macro2::Literal> + fmt::Debug + PartialEq + Clone,
41 proc_macro2::Literal: From<T>,
42 <T as std::convert::TryFrom<proc_macro2::Literal>>::Error: std::fmt::Display,
43 {
44 let pm_lit = input.parse::<proc_macro2::Literal>()
45 .expect("failed to parse input as proc_macro2::Literal");
46 let t_name = std::any::type_name::<T>();
47
48 // Unfortunately, `proc_macro2::Literal` does not implement `PartialEq`, so
49 // this is the next best thing.
50 if proc_macro2::Literal::from(ours.clone()).to_string() != pm_lit.to_string() {
51 panic!(
52 "Converting {} to proc_macro2::Literal has unexpected result:\
53 \nconverted: {:?}\nexpected: {:?}",
54 t_name,
55 proc_macro2::Literal::from(ours),
56 pm_lit,
57 );
58 }
59
60 match T::try_from(pm_lit) {
61 Err(e) => {
62 panic!("Trying to convert proc_macro2::Literal to {t_name} results in error: {e}");
63 }
64 Ok(res) => {
65 if res != ours {
66 panic!("Converting proc_macro2::Literal to {t_name} has unexpected result:\n\
67 actual: {res:?}\n\
68 expected: {ours:?}");
69 }
70 }
71 }
72 }
73
74 macro_rules! assert_err {
75 ($ty:ident, $input:literal, $kind:ident, $( $span:tt )+ ) => {
76 assert_err_single!($ty::parse($input), $kind, $($span)+);
77 assert_err_single!($crate::Literal::parse($input), $kind, $($span)+);
78 };
79 }
80
81 macro_rules! assert_err_single {
82 ($expr:expr, $kind:ident, $( $span:tt )+ ) => {
83 let res = $expr;
84 let err = match res {
85 Err(e) => e,
86 Ok(v) => panic!(
87 "Expected `{}` to return an error, but it returned Ok({:?})",
88 stringify!($expr),
89 v,
90 ),
91 };
92 if err.kind != $crate::err::ParseErrorKind::$kind {
93 panic!(
94 "Expected error kind {} for `{}` but got {:?}",
95 stringify!($kind),
96 stringify!($expr),
97 err.kind,
98 )
99 }
100 let expected_span = assert_err_single!(@span $($span)+);
101 if err.span != expected_span {
102 panic!(
103 "Expected error span {:?} for `{}` but got {:?}",
104 expected_span,
105 stringify!($expr),
106 err.span,
107 )
108 }
109 };
110 (@span $start:literal .. $end:literal) => { Some($start .. $end) };
111 (@span $at:literal) => { Some($at.. $at + 1) };
112 (@span None) => { None };
113 }