/
tests.rs
161 lines (136 loc) · 3.61 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#[macro_use]
extern crate justerror;
use indoc::indoc;
#[Error]
enum EnumError {
Foo,
Bar { a: &'static str, b: usize },
Baz(&'static str),
Qux(#[fmt(debug)] Vec<&'static str>, usize),
}
#[Error(desc = "My enum error", fmt = debug)]
enum EnumErrorWithArgs {
#[error(desc = "Foo error")]
Foo,
#[error(desc = "Bar error", fmt = display)]
Bar {
a: &'static str,
#[fmt("05")]
b: usize,
},
#[error(desc = "Baz error")]
Baz(&'static str),
Qux(Vec<&'static str>, usize),
}
#[Error(desc = "My struct error")]
struct MultipleNamedFieldsStructError {
a: &'static str,
#[fmt(">5")]
b: usize,
}
#[Error]
struct SingleUnnamedFieldStructError(&'static str);
#[test]
fn it_formats_enum_error_without_fields() {
let actual = format!("{}", EnumError::Foo);
let expected = "EnumError::Foo";
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_named_fields() {
let actual = format!("{}", EnumError::Bar { a: "A", b: 42 });
let expected = indoc! {r#"
EnumError::Bar
=== ↴
a: A
b: 42"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_single_unnamed_field() {
let actual = format!("{}", EnumError::Baz("Oh no"));
let expected = indoc! {r#"
EnumError::Baz
=== ↴
Oh no"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_multiple_unnamed_fields() {
let actual = format!("{}", EnumError::Qux(vec!["One", "Two"], 42));
let expected = indoc! {r#"
EnumError::Qux
=== ↴
0: [
"One",
"Two",
]
1: 42"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_args_without_fields() {
let actual = format!("{}", EnumErrorWithArgs::Foo);
let expected = indoc! {r#"
EnumErrorWithArgs::Foo
EnumErrorWithArgs: My enum error
Foo: Foo error"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_args_with_field_with_custom_format() {
let actual = format!("{}", EnumErrorWithArgs::Bar { a: "A", b: 42 });
let expected = indoc! {r#"
EnumErrorWithArgs::Bar
EnumErrorWithArgs: My enum error
Bar: Bar error
=== ↴
a: A
b: 00042"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_args_with_single_unnamed_field() {
let actual = format!("{}", EnumErrorWithArgs::Baz("Oh no"));
let expected = indoc! {r#"
EnumErrorWithArgs::Baz
EnumErrorWithArgs: My enum error
Baz: Baz error
=== ↴
"Oh no""#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_enum_error_with_args_with_field_using_root_format() {
let actual = format!("{}", EnumErrorWithArgs::Qux(vec!["One", "Two"], 42));
let expected = indoc! {r#"
EnumErrorWithArgs::Qux
My enum error
=== ↴
0: [
"One",
"Two",
]
1: 42"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_multiple_named_fields_struct_error() {
let actual = format!("{}", MultipleNamedFieldsStructError { a: "A", b: 7 });
let expected = indoc! {r#"
MultipleNamedFieldsStructError
My struct error
=== ↴
a: A
b: 7"#};
assert_eq!(actual, expected);
}
#[test]
fn it_formats_single_unnamed_field_struct_error() {
let actual = format!("{}", SingleUnnamedFieldStructError("Oh no"));
let expected = indoc! {r#"
SingleUnnamedFieldStructError
=== ↴
Oh no"#};
assert_eq!(actual, expected);
}