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

Commit b812378

Browse files
committed
Add negative versions of string matches
- `.doesnt_print`, `.doesnt_print_exactly` - `.doesnt_print_error`, `.doesnt_print_error_exactly` Fixes #31
1 parent 49c56a8 commit b812378

File tree

2 files changed

+123
-8
lines changed

2 files changed

+123
-8
lines changed

src/lib.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl Assert {
309309
self.expect_stdout = Some(OutputAssertion {
310310
expect: output.into(),
311311
fuzzy: true,
312+
expected_result: true,
312313
kind: StdOut,
313314
});
314315
self
@@ -329,6 +330,7 @@ impl Assert {
329330
self.expect_stdout = Some(OutputAssertion {
330331
expect: output.into(),
331332
fuzzy: false,
333+
expected_result: true,
332334
kind: StdOut,
333335
});
334336
self
@@ -351,6 +353,7 @@ impl Assert {
351353
self.expect_stderr = Some(OutputAssertion {
352354
expect: output.into(),
353355
fuzzy: true,
356+
expected_result: true,
354357
kind: StdErr,
355358
});
356359
self
@@ -373,6 +376,99 @@ impl Assert {
373376
self.expect_stderr = Some(OutputAssertion {
374377
expect: output.into(),
375378
fuzzy: false,
379+
expected_result: true,
380+
kind: StdErr,
381+
});
382+
self
383+
}
384+
385+
/// Expect the command's output to not **contain** `output`.
386+
///
387+
/// # Examples
388+
///
389+
/// ```rust
390+
/// extern crate assert_cli;
391+
///
392+
/// assert_cli::Assert::command(&["echo", "42"])
393+
/// .doesnt_print("73")
394+
/// .execute()
395+
/// .unwrap();
396+
/// ```
397+
pub fn doesnt_print<O: Into<String>>(mut self, output: O) -> Self {
398+
self.expect_stdout = Some(OutputAssertion {
399+
expect: output.into(),
400+
fuzzy: true,
401+
expected_result: false,
402+
kind: StdOut,
403+
});
404+
self
405+
}
406+
407+
/// Expect the command to output to not be **exactly** this `output`.
408+
///
409+
/// # Examples
410+
///
411+
/// ```rust
412+
/// extern crate assert_cli;
413+
///
414+
/// assert_cli::Assert::command(&["echo", "42"])
415+
/// .doesnt_print_exactly("73")
416+
/// .execute()
417+
/// .unwrap();
418+
/// ```
419+
pub fn doesnt_print_exactly<O: Into<String>>(mut self, output: O) -> Self {
420+
self.expect_stdout = Some(OutputAssertion {
421+
expect: output.into(),
422+
fuzzy: false,
423+
expected_result: false,
424+
kind: StdOut,
425+
});
426+
self
427+
}
428+
429+
/// Expect the command's stderr output to not **contain** `output`.
430+
///
431+
/// # Examples
432+
///
433+
/// ```rust
434+
/// extern crate assert_cli;
435+
///
436+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
437+
/// .fails()
438+
/// .and()
439+
/// .doesnt_print_error("content")
440+
/// .execute()
441+
/// .unwrap();
442+
/// ```
443+
pub fn doesnt_print_error<O: Into<String>>(mut self, output: O) -> Self {
444+
self.expect_stderr = Some(OutputAssertion {
445+
expect: output.into(),
446+
fuzzy: true,
447+
expected_result: false,
448+
kind: StdErr,
449+
});
450+
self
451+
}
452+
453+
/// Expect the command to output to not be **exactly** this `output` to stderr.
454+
///
455+
/// # Examples
456+
///
457+
/// ```rust
458+
/// extern crate assert_cli;
459+
///
460+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
461+
/// .fails_with(1)
462+
/// .and()
463+
/// .doesnt_print_error_exactly("content")
464+
/// .execute()
465+
/// .unwrap();
466+
/// ```
467+
pub fn doesnt_print_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
468+
self.expect_stderr = Some(OutputAssertion {
469+
expect: output.into(),
470+
fuzzy: false,
471+
expected_result: false,
376472
kind: StdErr,
377473
});
378474
self

src/output.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,35 @@ use diff;
1111
pub struct OutputAssertion<T> {
1212
pub expect: String,
1313
pub fuzzy: bool,
14+
pub expected_result: bool,
1415
pub kind: T,
1516
}
1617

1718
impl<T: OutputType> OutputAssertion<T> {
1819
fn matches_fuzzy(&self, got: &str) -> Result<()> {
19-
if !got.contains(&self.expect) {
20-
bail!(ErrorKind::OutputMismatch(self.expect.clone(), got.into()));
20+
let result = got.contains(&self.expect);
21+
if result != self.expected_result {
22+
if self.expected_result {
23+
bail!(ErrorKind::OutputDoesntContain(self.expect.clone(), got.into()));
24+
} else {
25+
bail!(ErrorKind::OutputContains(self.expect.clone(), got.into()));
26+
}
2127
}
2228

2329
Ok(())
2430
}
2531

2632
fn matches_exact(&self, got: &str) -> Result<()> {
2733
let differences = Changeset::new(self.expect.trim(), got.trim(), "\n");
28-
29-
if differences.distance > 0 {
30-
let nice_diff = diff::render(&differences)?;
31-
bail!(ErrorKind::ExactOutputMismatch(nice_diff));
34+
let result = differences.distance == 0;
35+
36+
if result != self.expected_result {
37+
if self.expected_result {
38+
let nice_diff = diff::render(&differences)?;
39+
bail!(ErrorKind::OutputDoesntMatch(nice_diff));
40+
} else {
41+
bail!(ErrorKind::OutputMatches(got.to_owned()));
42+
}
3243
}
3344

3445
Ok(())
@@ -88,14 +99,22 @@ mod errors {
8899
Fmt(::std::fmt::Error);
89100
}
90101
errors {
91-
OutputMismatch(expected: String, got: String) {
102+
OutputDoesntContain(expected: String, got: String) {
92103
description("Output was not as expected")
93104
display("expected to contain {:?}, got {:?}", expected, got)
94105
}
95-
ExactOutputMismatch(diff: String) {
106+
OutputContains(expected: String, got: String) {
107+
description("Output was not as expected")
108+
display("expected to not contain {:?}, got {:?}", expected, got)
109+
}
110+
OutputDoesntMatch(diff: String) {
96111
description("Output was not as expected")
97112
display("{}", diff)
98113
}
114+
OutputMatches(got: String) {
115+
description("Output was not as expected")
116+
display("{}", got)
117+
}
99118
}
100119
}
101120
}

0 commit comments

Comments
 (0)