Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit 57245da

Browse files
committed
Support more than one output assertion
BREAKING CHANGE: Setting a new output assertion will not overwrite the previous one.
1 parent 0618fd2 commit 57245da

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/assert.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::default;
22
use std::process::Command;
33
use std::path::PathBuf;
4+
use std::vec::Vec;
45

56
use errors::*;
67
use output::{OutputAssertion, StdErr, StdOut};
@@ -12,8 +13,8 @@ pub struct Assert {
1213
current_dir: Option<PathBuf>,
1314
expect_success: Option<bool>,
1415
expect_exit_code: Option<i32>,
15-
expect_stdout: Option<OutputAssertion<StdOut>>,
16-
expect_stderr: Option<OutputAssertion<StdErr>>,
16+
expect_stdout: Vec<OutputAssertion<StdOut>>,
17+
expect_stderr: Vec<OutputAssertion<StdErr>>,
1718
}
1819

1920
impl default::Default for Assert {
@@ -27,8 +28,8 @@ impl default::Default for Assert {
2728
current_dir: None,
2829
expect_success: Some(true),
2930
expect_exit_code: None,
30-
expect_stdout: None,
31-
expect_stderr: None,
31+
expect_stdout: vec![],
32+
expect_stderr: vec![],
3233
}
3334
}
3435
}
@@ -189,7 +190,7 @@ impl Assert {
189190
/// .unwrap();
190191
/// ```
191192
pub fn prints<O: Into<String>>(mut self, output: O) -> Self {
192-
self.expect_stdout = Some(OutputAssertion {
193+
self.expect_stdout.push(OutputAssertion {
193194
expect: output.into(),
194195
fuzzy: true,
195196
expected_result: true,
@@ -210,7 +211,7 @@ impl Assert {
210211
/// .unwrap();
211212
/// ```
212213
pub fn prints_exactly<O: Into<String>>(mut self, output: O) -> Self {
213-
self.expect_stdout = Some(OutputAssertion {
214+
self.expect_stdout.push(OutputAssertion {
214215
expect: output.into(),
215216
fuzzy: false,
216217
expected_result: true,
@@ -233,7 +234,7 @@ impl Assert {
233234
/// .unwrap();
234235
/// ```
235236
pub fn prints_error<O: Into<String>>(mut self, output: O) -> Self {
236-
self.expect_stderr = Some(OutputAssertion {
237+
self.expect_stderr.push(OutputAssertion {
237238
expect: output.into(),
238239
fuzzy: true,
239240
expected_result: true,
@@ -256,7 +257,7 @@ impl Assert {
256257
/// .unwrap();
257258
/// ```
258259
pub fn prints_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
259-
self.expect_stderr = Some(OutputAssertion {
260+
self.expect_stderr.push(OutputAssertion {
260261
expect: output.into(),
261262
fuzzy: false,
262263
expected_result: true,
@@ -278,7 +279,7 @@ impl Assert {
278279
/// .unwrap();
279280
/// ```
280281
pub fn doesnt_print<O: Into<String>>(mut self, output: O) -> Self {
281-
self.expect_stdout = Some(OutputAssertion {
282+
self.expect_stdout.push(OutputAssertion {
282283
expect: output.into(),
283284
fuzzy: true,
284285
expected_result: false,
@@ -300,7 +301,7 @@ impl Assert {
300301
/// .unwrap();
301302
/// ```
302303
pub fn doesnt_print_exactly<O: Into<String>>(mut self, output: O) -> Self {
303-
self.expect_stdout = Some(OutputAssertion {
304+
self.expect_stdout.push(OutputAssertion {
304305
expect: output.into(),
305306
fuzzy: false,
306307
expected_result: false,
@@ -324,7 +325,7 @@ impl Assert {
324325
/// .unwrap();
325326
/// ```
326327
pub fn doesnt_print_error<O: Into<String>>(mut self, output: O) -> Self {
327-
self.expect_stderr = Some(OutputAssertion {
328+
self.expect_stderr.push(OutputAssertion {
328329
expect: output.into(),
329330
fuzzy: true,
330331
expected_result: false,
@@ -348,7 +349,7 @@ impl Assert {
348349
/// .unwrap();
349350
/// ```
350351
pub fn doesnt_print_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
351-
self.expect_stderr = Some(OutputAssertion {
352+
self.expect_stderr.push(OutputAssertion {
352353
expect: output.into(),
353354
fuzzy: false,
354355
expected_result: false,
@@ -380,7 +381,6 @@ impl Assert {
380381
};
381382
let output = command.output()?;
382383

383-
384384
if let Some(expect_success) = self.expect_success {
385385
if expect_success != output.status.success() {
386386
bail!(ErrorKind::StatusMismatch(
@@ -399,15 +399,14 @@ impl Assert {
399399
));
400400
}
401401

402-
if let Some(ref ouput_assertion) = self.expect_stdout {
403-
ouput_assertion.execute(&output)
404-
.map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e))?;
405-
}
406-
407-
if let Some(ref ouput_assertion) = self.expect_stderr {
408-
ouput_assertion.execute(&output)
409-
.map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e))?;
410-
}
402+
self.expect_stdout
403+
.iter()
404+
.map(|a| a.execute(&output).map_err(|e| ErrorKind::StdoutMismatch(self.cmd.clone(), e).into()))
405+
.collect::<Result<Vec<()>>>()?;
406+
self.expect_stderr
407+
.iter()
408+
.map(|a| a.execute(&output).map_err(|e| ErrorKind::StderrMismatch(self.cmd.clone(), e).into()))
409+
.collect::<Result<Vec<()>>>()?;
411410

412411
Ok(())
413412
}

0 commit comments

Comments
 (0)