-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexceptions_work.rs
More file actions
218 lines (188 loc) · 5.36 KB
/
exceptions_work.rs
File metadata and controls
218 lines (188 loc) · 5.36 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
extern crate regex;
extern crate serde_json;
#[macro_use]
extern crate throw;
use throw::Result;
macro_rules! assert_matches {
($expected:expr, $actual:expr) => {{
let expected = ($expected).replace(" ", "\t");
let re = regex::Regex::new(&expected).expect("expected hardcoded regex to compile");
let actual = format!("{}", $actual);
assert!(
re.is_match(&actual),
"expected error to match regex `\n{expected}\n`, but found `\n{actual}\n`",
);
}};
}
fn throw_static_message() -> Result<(), &'static str> {
throw_new!("hi");
}
fn throw1() -> Result<(), ()> {
throw_new!(());
}
fn throw2() -> Result<(), ()> {
up!(throw1());
Ok(())
}
fn throw3() -> Result<(), ()> {
up!(throw2());
Ok(())
}
fn throw_with_context1() -> Result<(), &'static str> {
throw_new!("Error with context", "code"=>78,"application"=>"rust_core")
}
fn throw_with_context2() -> Result<(), &'static str> {
up!(throw_with_context1(), "project_secret"=>"omega");
Ok(())
}
fn throw_with_context3() -> Result<(), &'static str> {
up!(throw_with_context2(), "score"=>0.75, "height"=>948);
Ok(())
}
fn gives_ok() -> Result<&'static str, &'static str> {
Ok("ok")
}
fn throws_ok() -> Result<&'static str, &'static str> {
let ok_msg = up!(gives_ok());
Ok(ok_msg)
}
mod mod_test {
use throw::Result;
pub fn throws() -> Result<(), &'static str> {
throw_new!("ahhhh");
}
}
fn throws_into() -> Result<(), String> {
throw!(Err("some static string"));
Ok(())
}
fn throws_into_key_value() -> Result<(), String> {
throw!(Err("some static string"), "key" => "value");
Ok(())
}
fn throws_into_multiple_key_value_pairs() -> Result<(), String> {
throw!(
Err("some static string"),
"key" => "value",
"key2" => "value2",
"key3" => "value3",
"key4" => "value4",
);
Ok(())
}
#[test]
fn test_static_message() {
let error = throw_static_message().unwrap_err();
assert_eq!(*error.error(), "hi");
assert_matches!(
r#"Error: hi
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
error
);
assert_eq!("hi".to_owned(), error.into_origin());
}
#[test]
#[cfg(any(feature = "serde-1", feature = "serde-1-std"))]
fn serialize_json() {
let error = throw_with_context3().unwrap_err();
let json = serde_json::to_string(&error).unwrap();
let whitespace_trim = regex::Regex::new("\\s+").unwrap();
let expected = r#"\{
"points":\[
\{"line":[0-9]+,"column":[0-9]+,"module_path":"exceptions_work",
"file":"tests/exceptions_work.rs"\},
\{"line":[0-9]+,"column":[0-9]+,"module_path":"exceptions_work",
"file":"tests/exceptions_work.rs"\},
\{"line":[0-9]+,"column":[0-9]+,"module_path":"exceptions_work",
"file":"tests/exceptions_work.rs"\}
\],
"context":\[
\{"key":"code","value":78\},
\{"key":"application","value":"rust_core"\},
\{"key":"project_secret","value":"omega"\},
\{"key":"score","value":0.75\},
\{"key":"height","value":948\}
\],
"error":"Error with context"
\}"#;
assert_matches!(whitespace_trim.replace_all(expected, "\\s*"), json);
}
#[test]
fn test_throw_with_context() {
let error = throw_with_context1().unwrap_err();
assert_eq!(error.get_context().len(), 2);
let error2 = throw_with_context3().unwrap_err();
assert_eq!(error2.get_context().len(), 5);
assert_matches!(
r#"Error: "Error with context"
height: 948
score: 0.75
project_secret: omega
application: rust_core
code: 78
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
format!("{:?}", error2)
);
}
#[test]
#[allow(deprecated)]
fn test_multiple_throws() {
let error = throw3().unwrap_err();
assert_eq!(error.error(), &());
assert_eq!(error.error(), error.original_error());
assert_matches!(
r#"Error: \(\)
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
format!("{:?}", error)
);
}
#[test]
fn test_returns_ok() {
let ok = throws_ok().unwrap();
assert_eq!(ok, "ok");
}
#[test]
fn test_mod_throw() {
let error = mod_test::throws().unwrap_err();
assert_matches!(
r#"Error: ahhhh
at [0-9]+:[0-9] in exceptions_work::mod_test \([a-z/._-]+\)"#,
error
);
}
#[test]
fn test_throws_into() {
let error = throws_into().unwrap_err();
assert_matches!(
r#"Error: some static string
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
error
)
}
#[test]
fn test_throws_into_key_value() {
let error = throws_into_key_value().unwrap_err();
assert_matches!(
r#"Error: some static string
key: value
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
error
)
}
#[test]
fn test_throws_into_multiple_key_value_pairs() {
let error = throws_into_multiple_key_value_pairs().unwrap_err();
assert_matches!(
r#"Error: some static string
key4: value4
key3: value3
key2: value2
key: value
at [0-9]+:[0-9] in exceptions_work \([a-z/._-]+\)"#,
error
)
}