Skip to content

Commit d79e3d0

Browse files
authored
Merge pull request #980 from nathanross/tests-boilerplate-reduction
tests: remove helper function boilerplate via macros
2 parents e8338ca + b667fce commit d79e3d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+452
-713
lines changed

tests/common/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ macro_rules! path_concat {
4646
pb.to_str().unwrap().to_owned()
4747
}};
4848
}
49+
50+
#[macro_export]
51+
macro_rules! util_name {
52+
() => ( module_path!().split("_").nth(1).expect("no test name") )
53+
}
54+
55+
#[macro_export]
56+
macro_rules! new_ucmd {
57+
() => ( TestScenario::new(util_name!()).ucmd() )
58+
}
59+
60+
#[macro_export]
61+
macro_rules! at_and_ucmd {
62+
() => ({
63+
let ts = TestScenario::new(util_name!());
64+
(ts.fixtures.clone(), ts.ucmd())
65+
})
66+
}

tests/common/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub fn get_root_path() -> &'static str {
181181

182182
/// Object-oriented path struct that represents and operates on
183183
/// paths relative to the directory it was constructed for.
184+
#[derive(Clone)]
184185
pub struct AtPath {
185186
pub subdir: PathBuf,
186187
}

tests/test_base32.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88

99
use common::util::*;
1010

11-
static UTIL_NAME: &'static str = "base32";
12-
fn new_ucmd() -> UCommand {
13-
TestScenario::new(UTIL_NAME).ucmd()
14-
}
1511

1612
#[test]
1713
fn test_encode() {
1814
let input = "Hello, World!";
19-
new_ucmd()
15+
new_ucmd!()
2016
.pipe_in(input)
2117
.succeeds()
2218
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n");
@@ -26,7 +22,7 @@ fn test_encode() {
2622
fn test_decode() {
2723
for decode_param in vec!["-d", "--decode"] {
2824
let input = "JBSWY3DPFQQFO33SNRSCC===\n";
29-
new_ucmd()
25+
new_ucmd!()
3026
.arg(decode_param)
3127
.pipe_in(input)
3228
.succeeds()
@@ -37,7 +33,7 @@ fn test_decode() {
3733
#[test]
3834
fn test_garbage() {
3935
let input = "aGVsbG8sIHdvcmxkIQ==\0";
40-
new_ucmd()
36+
new_ucmd!()
4137
.arg("-d")
4238
.pipe_in(input)
4339
.fails()
@@ -48,7 +44,7 @@ fn test_garbage() {
4844
fn test_ignore_garbage() {
4945
for ignore_garbage_param in vec!["-i", "--ignore-garbage"] {
5046
let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n";
51-
new_ucmd()
47+
new_ucmd!()
5248
.arg("-d")
5349
.arg(ignore_garbage_param)
5450
.pipe_in(input)
@@ -61,7 +57,7 @@ fn test_ignore_garbage() {
6157
fn test_wrap() {
6258
for wrap_param in vec!["-w", "--wrap"] {
6359
let input = "The quick brown fox jumps over the lazy dog.";
64-
new_ucmd()
60+
new_ucmd!()
6561
.arg(wrap_param)
6662
.arg("20")
6763
.pipe_in(input)
@@ -73,7 +69,7 @@ fn test_wrap() {
7369
#[test]
7470
fn test_wrap_no_arg() {
7571
for wrap_param in vec!["-w", "--wrap"] {
76-
new_ucmd()
72+
new_ucmd!()
7773
.arg(wrap_param)
7874
.fails()
7975
.stderr_only(format!("base32: error: Argument to option '{}' missing.\n",
@@ -84,7 +80,7 @@ fn test_wrap_no_arg() {
8480
#[test]
8581
fn test_wrap_bad_arg() {
8682
for wrap_param in vec!["-w", "--wrap"] {
87-
new_ucmd()
83+
new_ucmd!()
8884
.arg(wrap_param).arg("b")
8985
.fails()
9086
.stderr_only("base32: error: invalid wrap size: ‘b’: invalid digit found in string\n");

tests/test_base64.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use common::util::*;
22

3-
static UTIL_NAME: &'static str = "base64";
4-
fn new_ucmd() -> UCommand {
5-
TestScenario::new(UTIL_NAME).ucmd()
6-
}
73

84
#[test]
95
fn test_encode() {
106
let input = "hello, world!";
11-
new_ucmd()
7+
new_ucmd!()
128
.pipe_in(input)
139
.succeeds()
1410
.stdout_only("aGVsbG8sIHdvcmxkIQ==\n");
@@ -18,7 +14,7 @@ fn test_encode() {
1814
fn test_decode() {
1915
for decode_param in vec!["-d", "--decode"] {
2016
let input = "aGVsbG8sIHdvcmxkIQ==";
21-
new_ucmd()
17+
new_ucmd!()
2218
.arg(decode_param)
2319
.pipe_in(input)
2420
.succeeds()
@@ -29,7 +25,7 @@ fn test_decode() {
2925
#[test]
3026
fn test_garbage() {
3127
let input = "aGVsbG8sIHdvcmxkIQ==\0";
32-
new_ucmd()
28+
new_ucmd!()
3329
.arg("-d")
3430
.pipe_in(input)
3531
.fails()
@@ -40,7 +36,7 @@ fn test_garbage() {
4036
fn test_ignore_garbage() {
4137
for ignore_garbage_param in vec!["-i", "--ignore-garbage"] {
4238
let input = "aGVsbG8sIHdvcmxkIQ==\0";
43-
new_ucmd()
39+
new_ucmd!()
4440
.arg("-d")
4541
.arg(ignore_garbage_param)
4642
.pipe_in(input)
@@ -53,7 +49,7 @@ fn test_ignore_garbage() {
5349
fn test_wrap() {
5450
for wrap_param in vec!["-w", "--wrap"] {
5551
let input = "The quick brown fox jumps over the lazy dog.";
56-
new_ucmd()
52+
new_ucmd!()
5753
.arg(wrap_param)
5854
.arg("20")
5955
.pipe_in(input)
@@ -65,7 +61,7 @@ fn test_wrap() {
6561
#[test]
6662
fn test_wrap_no_arg() {
6763
for wrap_param in vec!["-w", "--wrap"] {
68-
new_ucmd()
64+
new_ucmd!()
6965
.arg(wrap_param)
7066
.fails()
7167
.stderr_only(format!("base64: error: Argument to option '{}' missing.\n",
@@ -76,7 +72,7 @@ fn test_wrap_no_arg() {
7672
#[test]
7773
fn test_wrap_bad_arg() {
7874
for wrap_param in vec!["-w", "--wrap"] {
79-
new_ucmd()
75+
new_ucmd!()
8076
.arg(wrap_param)
8177
.arg("b")
8278
.fails()

tests/test_basename.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
use common::util::*;
22

3-
static UTIL_NAME: &'static str = "basename";
4-
fn new_ucmd() -> UCommand {
5-
TestScenario::new(UTIL_NAME).ucmd()
6-
}
73

84
#[test]
95
fn test_directory() {
10-
new_ucmd().args(&["/root/alpha/beta/gamma/delta/epsilon/omega/"])
6+
new_ucmd!().args(&["/root/alpha/beta/gamma/delta/epsilon/omega/"])
117
.succeeds().stdout_only("omega");
128
}
139

1410
#[test]
1511
fn test_file() {
16-
new_ucmd().args(&["/etc/passwd"]).succeeds().stdout_only("passwd");
12+
new_ucmd!().args(&["/etc/passwd"]).succeeds().stdout_only("passwd");
1713
}
1814

1915
#[test]
2016
fn test_remove_suffix() {
21-
new_ucmd().args(&["/usr/local/bin/reallylongexecutable.exe", ".exe"])
17+
new_ucmd!().args(&["/usr/local/bin/reallylongexecutable.exe", ".exe"])
2218
.succeeds().stdout_only("reallylongexecutable");
2319
}
2420

2521
#[test]
2622
fn test_dont_remove_suffix() {
27-
new_ucmd().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz");
23+
new_ucmd!().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz");
2824
}
2925

3026
#[cfg_attr(not(feature="test_unimplemented"),ignore)]
3127
#[test]
3228
fn test_multiple_param() {
3329
for multiple_param in vec!["-a", "--multiple"] {
3430
let path = "/foo/bar/baz";
35-
new_ucmd().args(&[multiple_param, path, path])
31+
new_ucmd!().args(&[multiple_param, path, path])
3632
.succeeds().stdout_only("baz\nbaz");
3733
}
3834
}
@@ -42,7 +38,7 @@ fn test_multiple_param() {
4238
fn test_suffix_param() {
4339
for suffix_param in vec!["-s", "--suffix"] {
4440
let path = "/foo/bar/baz.exe";
45-
new_ucmd()
41+
new_ucmd!()
4642
.args(&[suffix_param, ".exe", path, path])
4743
.succeeds().stdout_only("baz\nbaz");
4844
}
@@ -53,14 +49,14 @@ fn test_suffix_param() {
5349
fn test_zero_param() {
5450
for zero_param in vec!["-z", "--zero"] {
5551
let path = "/foo/bar/baz";
56-
new_ucmd().args(&[zero_param, "-a", path, path])
52+
new_ucmd!().args(&[zero_param, "-a", path, path])
5753
.succeeds().stdout_only("baz\0baz\0");
5854
}
5955
}
6056

6157

6258
fn expect_error(input: Vec<&str>) {
63-
assert!(new_ucmd().args(&input)
59+
assert!(new_ucmd!().args(&input)
6460
.fails().no_stdout().stderr.len() > 0);
6561
}
6662

tests/test_cat.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use common::util::*;
22

3-
static UTIL_NAME: &'static str = "cat";
4-
fn new_ucmd() -> UCommand {
5-
TestScenario::new(UTIL_NAME).ucmd()
6-
}
73

84
#[test]
95
fn test_output_multi_files_print_all_chars() {
10-
new_ucmd()
6+
new_ucmd!()
117
.args(&["alpha.txt", "256.txt", "-A", "-n"])
128
.succeeds()
139
.stdout_only(" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n \
@@ -26,7 +22,7 @@ fn test_output_multi_files_print_all_chars() {
2622
#[test]
2723
fn test_stdin_show_nonprinting() {
2824
for same_param in vec!["-v", "--show-nonprinting"] {
29-
new_ucmd()
25+
new_ucmd!()
3026
.args(&vec![same_param])
3127
.pipe_in("\t\0\n")
3228
.succeeds()
@@ -37,7 +33,7 @@ fn test_stdin_show_nonprinting() {
3733
#[test]
3834
fn test_stdin_show_tabs() {
3935
for same_param in vec!["-T", "--show-tabs"] {
40-
new_ucmd()
36+
new_ucmd!()
4137
.args(&[same_param])
4238
.pipe_in("\t\0\n")
4339
.succeeds()
@@ -49,7 +45,7 @@ fn test_stdin_show_tabs() {
4945
#[test]
5046
fn test_stdin_show_ends() {
5147
for same_param in vec!["-E", "--show-ends"] {
52-
new_ucmd()
48+
new_ucmd!()
5349
.args(&[same_param,"-"])
5450
.pipe_in("\t\0\n")
5551
.succeeds()
@@ -60,7 +56,7 @@ fn test_stdin_show_ends() {
6056
#[test]
6157
fn test_stdin_show_all() {
6258
for same_param in vec!["-A", "--show-all"] {
63-
new_ucmd()
59+
new_ucmd!()
6460
.args(&[same_param])
6561
.pipe_in("\t\0\n")
6662
.succeeds()
@@ -70,7 +66,7 @@ fn test_stdin_show_all() {
7066

7167
#[test]
7268
fn test_stdin_nonprinting_and_endofline() {
73-
new_ucmd()
69+
new_ucmd!()
7470
.args(&["-e"])
7571
.pipe_in("\t\0\n")
7672
.succeeds()
@@ -79,7 +75,7 @@ fn test_stdin_nonprinting_and_endofline() {
7975

8076
#[test]
8177
fn test_stdin_nonprinting_and_tabs() {
82-
new_ucmd()
78+
new_ucmd!()
8379
.args(&["-t"])
8480
.pipe_in("\t\0\n")
8581
.succeeds()
@@ -89,7 +85,7 @@ fn test_stdin_nonprinting_and_tabs() {
8985
#[test]
9086
fn test_stdin_squeeze_blank() {
9187
for same_param in vec!["-s", "--squeeze-blank"] {
92-
new_ucmd()
88+
new_ucmd!()
9389
.arg(same_param)
9490
.pipe_in("\n\na\n\n\n\n\nb\n\n\n")
9591
.succeeds()
@@ -100,7 +96,7 @@ fn test_stdin_squeeze_blank() {
10096
#[test]
10197
fn test_stdin_number_non_blank() {
10298
for same_param in vec!["-b", "--number-nonblank"] {
103-
new_ucmd()
99+
new_ucmd!()
104100
.arg(same_param)
105101
.arg("-")
106102
.pipe_in("\na\nb\n\n\nc")
@@ -112,7 +108,7 @@ fn test_stdin_number_non_blank() {
112108
#[test]
113109
fn test_non_blank_overrides_number() {
114110
for same_param in vec!["-b", "--number-nonblank"] {
115-
new_ucmd()
111+
new_ucmd!()
116112
.args(&[same_param, "-"])
117113
.pipe_in("\na\nb\n\n\nc")
118114
.succeeds()
@@ -123,7 +119,7 @@ fn test_non_blank_overrides_number() {
123119
#[test]
124120
fn test_squeeze_blank_before_numbering() {
125121
for same_param in vec!["-s", "--squeeze-blank"] {
126-
new_ucmd()
122+
new_ucmd!()
127123
.args(&[same_param, "-n", "-"])
128124
.pipe_in("a\n\n\nb")
129125
.succeeds()

0 commit comments

Comments
 (0)