Skip to content

Commit 3c7a602

Browse files
committed
Tests for struct error types
1 parent 972882d commit 3c7a602

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
mod structs;

tests/src/structs.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use error_stack_macros2::Error;
4+
5+
#[test]
6+
fn unit_struct_works() {
7+
#[derive(Debug, Error)]
8+
#[display("unit struct")]
9+
struct UnitStructType;
10+
11+
assert_eq!(UnitStructType.to_string(), "unit struct");
12+
}
13+
14+
#[test]
15+
fn named_field_struct_works_without_interpolation() {
16+
#[derive(Debug, Error)]
17+
#[display("named field struct")]
18+
struct NamedFieldStructType {
19+
_length: usize,
20+
_is_ascii: bool,
21+
_inner: String,
22+
}
23+
24+
let test_val = NamedFieldStructType {
25+
_length: 5,
26+
_is_ascii: true,
27+
_inner: String::from("hello"),
28+
};
29+
assert_eq!(test_val.to_string(), "named field struct");
30+
}
31+
32+
#[test]
33+
fn named_field_struct_works_with_interpolation_of_some_fields() {
34+
#[derive(Debug, Error)]
35+
#[display("named field struct: {inner:?} has {length} characters")]
36+
struct NamedFieldStructType {
37+
length: usize,
38+
_is_ascii: bool,
39+
inner: String,
40+
}
41+
42+
let test_val = NamedFieldStructType {
43+
length: 5,
44+
_is_ascii: true,
45+
inner: String::from("hello"),
46+
};
47+
assert_eq!(
48+
test_val.to_string(),
49+
"named field struct: \"hello\" has 5 characters"
50+
);
51+
}
52+
53+
#[test]
54+
fn named_field_struct_works_with_interpolation_of_all_fields() {
55+
#[derive(Debug, Error)]
56+
#[display(
57+
"named field struct: {inner:?} has {length} characters and is ascii={is_ascii}"
58+
)]
59+
struct NamedFieldStructType {
60+
length: usize,
61+
is_ascii: bool,
62+
inner: String,
63+
}
64+
65+
let test_val = NamedFieldStructType {
66+
length: 5,
67+
is_ascii: true,
68+
inner: String::from("hello"),
69+
};
70+
assert_eq!(
71+
test_val.to_string(),
72+
"named field struct: \"hello\" has 5 characters and is ascii=true"
73+
);
74+
}
75+
76+
#[test]
77+
#[allow(dead_code)]
78+
fn tuple_struct_works_without_interpolation() {
79+
#[derive(Debug, Error)]
80+
#[display("tuple struct")]
81+
struct TupleStructType(isize, isize, isize);
82+
83+
let test_val = TupleStructType(5, 10, 15);
84+
assert_eq!(test_val.to_string(), "tuple struct");
85+
}
86+
87+
#[test]
88+
#[allow(dead_code)]
89+
fn tuple_struct_works_with_interpolation_of_some_fields() {
90+
#[derive(Debug, Error)]
91+
#[display("tuple struct: point with y value {1}")]
92+
struct TupleStructType(isize, isize, isize);
93+
94+
let test_val = TupleStructType(5, 10, 15);
95+
assert_eq!(test_val.to_string(), "tuple struct: point with y value 10");
96+
}
97+
98+
#[test]
99+
#[allow(dead_code)]
100+
fn tuple_struct_works_with_interpolation_of_all_fields() {
101+
#[derive(Debug, Error)]
102+
#[display(
103+
"tuple struct: point {2} units in front of the origin, and with x and y coords ({0}, {1})"
104+
)]
105+
struct TupleStructType(isize, isize, isize);
106+
107+
let test_val = TupleStructType(5, 10, 15);
108+
assert_eq!(
109+
test_val.to_string(),
110+
"tuple struct: point 15 units in front of the origin, and with x and y coords (5, 10)"
111+
);
112+
}
113+
}

0 commit comments

Comments
 (0)