Skip to content

Commit 8c8ede8

Browse files
committed
Add asser_error macro to test_utils module
1 parent 1fd9a10 commit 8c8ede8

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/auth_monitor_params.test.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::error::Error;
22

3+
use crate::assert_error;
34
use crate::auth_monitor_options::AuthMonitorOptions;
45
use crate::auth_monitor_params::{
56
AuthMonitorParams, MAX_FAILED_ATTEMPTS_OPTION, RESET_AFTER_SECONDS_OPTION,
@@ -16,14 +17,7 @@ fn when_parsing_no_arguments_then_return_file_not_specified_error() {
1617
}
1718

1819
fn expect_file_not_specified_error(result: AuthMonitorResult) {
19-
expect_error(result, "File path not specified");
20-
}
21-
22-
fn expect_error(result: AuthMonitorResult, expected: &str) {
23-
match result {
24-
Ok(_) => panic!("Error \"{}\" was expected", expected),
25-
Err(error) => assert_eq!(error.to_string(), expected),
26-
}
20+
assert_error!(result, "File path not specified");
2721
}
2822

2923
#[test]
@@ -61,7 +55,7 @@ fn when_parsing_filepath_passed_more_than_once_then_return_file_path_specified_m
6155
}
6256

6357
fn expect_file_path_specified_more_than_once_error(result: AuthMonitorResult) {
64-
expect_error(result, "File path specified more than once");
58+
assert_error!(result, "File path specified more than once");
6559
}
6660

6761
#[test]
@@ -123,7 +117,7 @@ fn when_parsing_option_without_value_then_return_missing_option_value_error() {
123117
for option_argument in option_arguments {
124118
let arguments = [String::from(FILEPATH), option_argument];
125119
let expected = format!("Missing value for option --{}", option);
126-
expect_error(AuthMonitorParams::from_arguments(&arguments), &expected);
120+
assert_error!(AuthMonitorParams::from_arguments(&arguments), expected);
127121
}
128122
}
129123
}
@@ -136,7 +130,7 @@ fn when_parsing_option_with_invalid_value_then_return_invalid_option_value_error
136130
let option_argument = format!("--{}={}", option, value);
137131
let arguments = [String::from(FILEPATH), option_argument];
138132
let expected = format!("\"{}\" is not a valid value for option --{}", value, option);
139-
expect_error(AuthMonitorParams::from_arguments(&arguments), &expected);
133+
assert_error!(AuthMonitorParams::from_arguments(&arguments), expected);
140134
}
141135
}
142136
}
@@ -147,7 +141,7 @@ fn when_parsing_option_with_no_value_then_return_no_value_error() {
147141
let option_argument = format!("--{}=", option);
148142
let arguments = [String::from(FILEPATH), option_argument];
149143
let expected = format!("Missing value for option --{}", option);
150-
expect_error(AuthMonitorParams::from_arguments(&arguments), &expected);
144+
assert_error!(AuthMonitorParams::from_arguments(&arguments), expected);
151145
}
152146
}
153147

@@ -159,7 +153,7 @@ fn when_parsing_option_with_value_less_than_0_then_invalid_value_error() {
159153
let option_argument = format!("--{}={}", option, value);
160154
let arguments = [String::from(FILEPATH), option_argument];
161155
let expected = format!("{} must be greater than 0", option);
162-
expect_error(AuthMonitorParams::from_arguments(&arguments), &expected);
156+
assert_error!(AuthMonitorParams::from_arguments(&arguments), expected);
163157
}
164158
}
165159
}
@@ -193,6 +187,6 @@ fn when_parsing_unknown_option_then_return_unknown_option_error() {
193187
] {
194188
let arguments = [String::from(FILEPATH), String::from(option)];
195189
let expected = format!("Unknown option {}", option);
196-
expect_error(AuthMonitorParams::from_arguments(&arguments), &expected)
190+
assert_error!(AuthMonitorParams::from_arguments(&arguments), expected)
197191
}
198192
}

src/test_utils/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[macro_export]
2+
macro_rules! assert_error {
3+
($result:expr, $expected:expr) => {
4+
match &$result {
5+
Ok(_) => panic!("Error \"{}\" was expected", $expected),
6+
Err(error) => assert_eq!(error.to_string(), $expected),
7+
}
8+
};
9+
}

src/test_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod macros;
12
pub mod test_file;

0 commit comments

Comments
 (0)