]>
git.proxmox.com Git - rustc.git/blob - vendor/thiserror/tests/test_display.rs
1 #![deny(clippy::all, clippy::pedantic)]
6 fn assert
<T
: Display
>(expected
: &str, value
: T
) {
7 assert_eq
!(expected
, value
.to_string());
12 #[derive(Error, Debug)]
13 #[error("braced error: {msg}")]
18 let msg
= "T".to_owned();
19 assert("braced error: T", Error { msg }
);
23 fn test_braced_unused() {
24 #[derive(Error, Debug)]
25 #[error("braced error")]
30 assert("braced error", Error { extra: 0 }
);
35 #[derive(Error, Debug)]
36 #[error("tuple error: {0}")]
39 assert("tuple error: 0", Error(0));
44 #[derive(Error, Debug)]
45 #[error("unit error")]
48 assert("unit error", Error
);
53 #[derive(Error, Debug)]
55 #[error("braced error: {id}")]
57 #[error("tuple error: {0}")]
59 #[error("unit error")]
63 assert("braced error: 0", Error
::Braced { id: 0 }
);
64 assert("tuple error: 0", Error
::Tuple(0));
65 assert("unit error", Error
::Unit
);
70 #[derive(Error, Debug)]
71 #[error("{MSG}: {id:?} (code {CODE:?})")]
76 const MSG
: &str = "failed to do";
77 const CODE
: usize = 9;
79 assert("failed to do: \"\" (code 9)", Error { id: "" }
);
84 #[derive(Error, Debug)]
88 #[error("other error")]
92 assert("some error", Error
::Some("some error"));
93 assert("other error", Error
::Other("..."));
97 fn test_brace_escape() {
98 #[derive(Error, Debug)]
99 #[error("fn main() {{}}")]
102 assert("fn main() {}", Error
);
107 #[derive(Error, Debug)]
108 #[error("1 + 1 = {}", 1 + 1)]
110 assert("1 + 1 = 2", Error
);
115 #[derive(Error, Debug)]
116 #[error("!bool = {}", not(.0))]
119 #[allow(clippy::trivially_copy_pass_by_ref)]
120 fn not(bool
: &bool
) -> bool
{
124 assert("!bool = false", Error(true));
129 #[derive(Error, Debug)]
130 #[error("{}: {0}", match .1 {
131 Some(n
) => format
!("error occurred with {}", n
),
132 None
=> "there was an empty error".to_owned(),
134 struct Error(String
, Option
<usize>);
137 "error occurred with 1: ...",
138 Error("...".to_owned(), Some(1)),
141 "there was an empty error: ...",
142 Error("...".to_owned(), None
),
148 #[allow(clippy::empty_enum)]
149 #[derive(Error, Debug)]
158 #[derive(Error, Debug)]
159 #[error("a={a} :: b={} :: c={c} :: d={d}", 1, c = 2, d = 3)]
165 assert("a=0 :: b=1 :: c=2 :: d=3", Error { a: 0, d: 0 }
);
170 #[derive(Error, Debug)]
172 #[error("error {0}")]
174 #[error("error {0}", '?')]
178 assert("error 9", Error
::Tuple(9, 0));
179 assert("error ?", Error
::Struct { v: 0 }
);
183 fn test_trailing_comma() {
184 #[derive(Error, Debug)]
191 assert("error ?", Error('?'
));
201 #[derive(Error, Debug)]
202 #[error("{}", .0.data)]
205 assert("0", Error(Inner { data: 0 }
));
209 fn test_macro_rules() {
210 // Regression test for https://github.com/dtolnay/thiserror/issues/86
212 macro_rules
! decl_error
{
213 ($variant
:ident($value
:ident
)) => {
214 #[derive(Debug, Error)]
220 #[derive(Debug, Error)]
228 decl_error
!(Repro(u8));
230 assert("0", Error0
::Repro(0));
231 assert("0", Error1
::Repro(0));