Debian Codegraph @

debian/missing-sources/derive_more/tests/boats_display_derive.rs

raw
1 // The following code is from https://github.com/withoutboats/display_derive/blob/232a32ee19e262aacbd2c93be5b4ce9e89a5fc30/tests/tests.rs
2 // Written by without boats originally
3
4 use derive_more::Display;
5
6 #[derive(Display)]
7 #[display("An error has occurred.")]
8 struct UnitError;
9
10 #[test]
11 fn unit_struct() {
12 let s = UnitError.to_string();
13 assert_eq!(s, "An error has occurred.");
14 }
15
16 #[derive(Display)]
17 #[display("Error code: {}", code)]
18 struct RecordError {
19 code: u32,
20 }
21
22 #[test]
23 fn record_struct() {
24 let s = RecordError { code: 0 }.to_string();
25 assert_eq!(s, "Error code: 0");
26 }
27
28 #[derive(Display)]
29 #[display("Error code: {}", _0)]
30 struct TupleError(i32);
31
32 #[test]
33 fn tuple_struct() {
34 let s = TupleError(2).to_string();
35 assert_eq!(s, "Error code: 2");
36 }
37
38 #[allow(clippy::enum_variant_names)] // because of the original source
39 #[derive(Display)]
40 enum EnumError {
41 #[display("Error code: {}", code)]
42 StructVariant { code: i32 },
43 #[display("Error: {}", _0)]
44 TupleVariant(&'static str),
45 #[display("An error has occurred.")]
46 UnitVariant,
47 }
48
49 #[test]
50 fn enum_error() {
51 let s = EnumError::StructVariant { code: 2 }.to_string();
52 assert_eq!(s, "Error code: 2");
53 let s = EnumError::TupleVariant("foobar").to_string();
54 assert_eq!(s, "Error: foobar");
55 let s = EnumError::UnitVariant.to_string();
56 assert_eq!(s, "An error has occurred.");
57 }