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
use std::process::Output;
use crate::Captured;
pub trait TestOutput {
#[track_caller]
fn assert_success(&self) -> &Self;
#[track_caller]
fn assert_failure(&self) -> &Self;
#[track_caller]
fn assert_exitcode(&self, code: i32) -> &Self;
#[track_caller]
fn assert_stdout_utf8(&self, regex: &str) -> &Self;
#[track_caller]
fn assert_stderr_utf8(&self, regex: &str) -> &Self;
#[track_caller]
fn assert_stdout_bytes(&self, regex: &str) -> &Self;
#[track_caller]
fn assert_stderr_bytes(&self, regex: &str) -> &Self;
fn stdout_captures_utf8(&self, regex: &str) -> Captured;
fn stderr_captures_utf8(&self, regex: &str) -> Captured;
}
impl TestOutput for Output {
fn assert_success(&self) -> &Self {
assert!(self.status.success(), "expected success at exit");
self
}
fn assert_failure(&self) -> &Self {
assert!(!self.status.success(), "expected failure at exit");
self
}
fn assert_exitcode(&self, code: i32) -> &Self {
assert_eq!(self.status.code(), Some(code), "unexpected exitcode");
self
}
fn assert_stdout_utf8(&self, regex: &str) -> &Self {
let (ok, utf8) = crate::regex::regex_match_utf8(&self.stdout, regex);
assert!(
ok,
"stdout does not match:\n{}\nstdout was:\n{}",
regex, utf8
);
self
}
fn assert_stderr_utf8(&self, regex: &str) -> &Self {
let (ok, utf8) = crate::regex::regex_match_utf8(&self.stderr, regex);
assert!(
ok,
"stderr does not match:\n{}\nstderr was:\n{}",
regex, utf8
);
self
}
fn assert_stdout_bytes(&self, regex: &str) -> &Self {
let (ok, bytes) = crate::regex::regex_match_bytes(&self.stdout, regex);
assert!(
ok,
"stdout does not match:\n{}\nstdout was:\n{}",
regex, bytes
);
self
}
fn assert_stderr_bytes(&self, regex: &str) -> &Self {
let (ok, bytes) = crate::regex::regex_match_bytes(&self.stderr, regex);
assert!(
ok,
"stderr does not match:\n{}\nstderr was:\n{}",
regex, bytes
);
self
}
fn stdout_captures_utf8(&self, regex: &str) -> Captured {
crate::regex::captures_utf8(&self.stdout, regex)
}
fn stderr_captures_utf8(&self, regex: &str) -> Captured {
crate::regex::captures_utf8(&self.stderr, regex)
}
}
#[cfg(test)]
#[cfg(unix)]
mod test {
use crate::*;
use std::path::Path;
#[test]
fn captures() {
let testcall = TestCall::external_command(Path::new("echo"));
let captures = testcall
.call(["Hello World!"])
.stdout_captures_utf8("(?P<first>[^ ]*) (?P<second>[^ ]*)");
assert_eq!(&captures[0], "Hello World!\n");
assert_eq!(&captures[1], "Hello");
assert_eq!(&captures[2], "World!\n");
assert_eq!(&captures["first"], "Hello");
assert_eq!(&captures["second"], "World!\n");
}
}