Skip to content

Commit e16e8cd

Browse files
committed
Tests for enum error types
1 parent cac1b42 commit e16e8cd

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

tests/src/enums.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use error_stack_macros2::Error;
4+
5+
#[test]
6+
fn empty_enum_works_without_display_attr() {
7+
#[derive(Debug, Error)]
8+
enum _EmptyEnumType {}
9+
}
10+
11+
#[test]
12+
fn empty_enum_works_with_display_attr() {
13+
#[derive(Debug, Error)]
14+
#[display("this display attr is unnecessary")]
15+
enum _EmptyEnumType {}
16+
}
17+
18+
#[test]
19+
#[allow(dead_code)]
20+
#[allow(clippy::enum_variant_names)]
21+
fn enum_works_with_display_attr_default() {
22+
#[derive(Debug, Error)]
23+
#[display("enum type")]
24+
enum EnumType {
25+
UnitVariant,
26+
27+
NamedFieldVariant {
28+
_length: usize,
29+
_is_ascii: bool,
30+
_inner: String,
31+
},
32+
33+
TupleVariant(isize, isize, isize),
34+
}
35+
36+
let unit = EnumType::UnitVariant;
37+
assert_eq!(unit.to_string(), "enum type");
38+
39+
let named_field = EnumType::NamedFieldVariant {
40+
_length: 5,
41+
_is_ascii: true,
42+
_inner: String::from("hello"),
43+
};
44+
assert_eq!(named_field.to_string(), "enum type");
45+
46+
let tuple = EnumType::TupleVariant(5, 10, 15);
47+
assert_eq!(tuple.to_string(), "enum type");
48+
}
49+
50+
#[test]
51+
#[allow(clippy::enum_variant_names)]
52+
fn enum_works_with_display_attr_default_and_some_variants() {
53+
#[derive(Debug, Error)]
54+
#[display("enum type")]
55+
enum EnumType {
56+
UnitVariant,
57+
58+
#[display(
59+
"named field variant: {inner:?} has {length} characters and is ascii={is_ascii}"
60+
)]
61+
NamedFieldVariant {
62+
length: usize,
63+
is_ascii: bool,
64+
inner: String,
65+
},
66+
67+
#[display(
68+
"tuple variant: point {2} units in front of the origin, and with x and y coords ({0}, {1})"
69+
)]
70+
TupleVariant(isize, isize, isize),
71+
}
72+
73+
let unit = EnumType::UnitVariant;
74+
assert_eq!(unit.to_string(), "enum type");
75+
76+
let named_field = EnumType::NamedFieldVariant {
77+
length: 5,
78+
is_ascii: true,
79+
inner: String::from("hello"),
80+
};
81+
assert_eq!(
82+
named_field.to_string(),
83+
"named field variant: \"hello\" has 5 characters and is ascii=true"
84+
);
85+
86+
let tuple = EnumType::TupleVariant(5, 10, 15);
87+
assert_eq!(
88+
tuple.to_string(),
89+
"tuple variant: point 15 units in front of the origin, and with x and y coords (5, 10)"
90+
);
91+
}
92+
93+
#[test]
94+
#[allow(clippy::enum_variant_names)]
95+
fn enum_works_with_display_attr_default_and_all_variants() {
96+
#[derive(Debug, Error)]
97+
#[display("enum type")]
98+
enum EnumType {
99+
#[display("unit variant")]
100+
UnitVariant,
101+
102+
#[display(
103+
"named field variant: {inner:?} has {length} characters and is ascii={is_ascii}"
104+
)]
105+
NamedFieldVariant {
106+
length: usize,
107+
is_ascii: bool,
108+
inner: String,
109+
},
110+
111+
#[display(
112+
"tuple variant: point {2} units in front of the origin, and with x and y coords ({0}, {1})"
113+
)]
114+
TupleVariant(isize, isize, isize),
115+
}
116+
117+
let unit = EnumType::UnitVariant;
118+
assert_eq!(unit.to_string(), "unit variant");
119+
120+
let named_field = EnumType::NamedFieldVariant {
121+
length: 5,
122+
is_ascii: true,
123+
inner: String::from("hello"),
124+
};
125+
assert_eq!(
126+
named_field.to_string(),
127+
"named field variant: \"hello\" has 5 characters and is ascii=true"
128+
);
129+
130+
let tuple = EnumType::TupleVariant(5, 10, 15);
131+
assert_eq!(
132+
tuple.to_string(),
133+
"tuple variant: point 15 units in front of the origin, and with x and y coords (5, 10)"
134+
);
135+
}
136+
137+
#[test]
138+
#[allow(clippy::enum_variant_names)]
139+
fn enum_works_with_display_attr_all_variants() {
140+
#[derive(Debug, Error)]
141+
enum EnumType {
142+
#[display("unit variant")]
143+
UnitVariant,
144+
145+
#[display(
146+
"named field variant: {inner:?} has {length} characters and is ascii={is_ascii}"
147+
)]
148+
NamedFieldVariant {
149+
length: usize,
150+
is_ascii: bool,
151+
inner: String,
152+
},
153+
154+
#[display(
155+
"tuple variant: point {2} units in front of the origin, and with x and y coords ({0}, {1})"
156+
)]
157+
TupleVariant(isize, isize, isize),
158+
}
159+
160+
let unit = EnumType::UnitVariant;
161+
assert_eq!(unit.to_string(), "unit variant");
162+
163+
let named_field = EnumType::NamedFieldVariant {
164+
length: 5,
165+
is_ascii: true,
166+
inner: String::from("hello"),
167+
};
168+
assert_eq!(
169+
named_field.to_string(),
170+
"named field variant: \"hello\" has 5 characters and is ascii=true"
171+
);
172+
173+
let tuple = EnumType::TupleVariant(5, 10, 15);
174+
assert_eq!(
175+
tuple.to_string(),
176+
"tuple variant: point 15 units in front of the origin, and with x and y coords (5, 10)"
177+
);
178+
}
179+
}

tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod enum_variants;
2+
mod enums;
23
mod structs;

0 commit comments

Comments
 (0)