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

Commit 899d353

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 aed7aa6 commit 899d353

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
@@ -288,6 +288,7 @@ impl Assert {
288288
self.expect_stdout = Some(OutputAssertion {
289289
expect: output.into(),
290290
fuzzy: true,
291+
expected_result: true,
291292
kind: StdOut,
292293
});
293294
self
@@ -308,6 +309,7 @@ impl Assert {
308309
self.expect_stdout = Some(OutputAssertion {
309310
expect: output.into(),
310311
fuzzy: false,
312+
expected_result: true,
311313
kind: StdOut,
312314
});
313315
self
@@ -330,6 +332,7 @@ impl Assert {
330332
self.expect_stderr = Some(OutputAssertion {
331333
expect: output.into(),
332334
fuzzy: true,
335+
expected_result: true,
333336
kind: StdErr,
334337
});
335338
self
@@ -352,6 +355,99 @@ impl Assert {
352355
self.expect_stderr = Some(OutputAssertion {
353356
expect: output.into(),
354357
fuzzy: false,
358+
expected_result: true,
359+
kind: StdErr,
360+
});
361+
self
362+
}
363+
364+
/// Expect the command's output to not **contain** `output`.
365+
///
366+
/// # Examples
367+
///
368+
/// ```rust
369+
/// extern crate assert_cli;
370+
///
371+
/// assert_cli::Assert::command(&["echo", "42"])
372+
/// .doesnt_print("73")
373+
/// .execute();
374+
/// .unwrap();
375+
/// ```
376+
pub fn doesnt_print<O: Into<String>>(mut self, output: O) -> Self {
377+
self.expect_stdout = Some(OutputAssertion {
378+
expect: output.into(),
379+
fuzzy: true,
380+
expected_result: false,
381+
kind: StdOut,
382+
});
383+
self
384+
}
385+
386+
/// Expect the command to output to not be **exactly** this `output`.
387+
///
388+
/// # Examples
389+
///
390+
/// ```rust
391+
/// extern crate assert_cli;
392+
///
393+
/// assert_cli::Assert::command(&["echo", "42"])
394+
/// .doesnt_print_exactly("73")
395+
/// .execute();
396+
/// .unwrap();
397+
/// ```
398+
pub fn doesnt_print_exactly<O: Into<String>>(mut self, output: O) -> Self {
399+
self.expect_stdout = Some(OutputAssertion {
400+
expect: output.into(),
401+
fuzzy: false,
402+
expected_result: false,
403+
kind: StdOut,
404+
});
405+
self
406+
}
407+
408+
/// Expect the command's stderr output to not **contain** `output`.
409+
///
410+
/// # Examples
411+
///
412+
/// ```rust
413+
/// extern crate assert_cli;
414+
///
415+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
416+
/// .fails()
417+
/// .and()
418+
/// .doesnt_print_error("content)
419+
/// .execute();
420+
/// .unwrap();
421+
/// ```
422+
pub fn doesnt_print_error<O: Into<String>>(mut self, output: O) -> Self {
423+
self.expect_stderr = Some(OutputAssertion {
424+
expect: output.into(),
425+
fuzzy: true,
426+
expected_result: false,
427+
kind: StdErr,
428+
});
429+
self
430+
}
431+
432+
/// Expect the command to output to not be **exactly** this `output` to stderr.
433+
///
434+
/// # Examples
435+
///
436+
/// ```rust
437+
/// extern crate assert_cli;
438+
///
439+
/// assert_cli::Assert::command(&["cat", "non-existing-file"])
440+
/// .fails_with(1)
441+
/// .and()
442+
/// .doesnt_print_error_exactly("content")
443+
/// .execute();
444+
/// .unwrap();
445+
/// ```
446+
pub fn doesnt_print_error_exactly<O: Into<String>>(mut self, output: O) -> Self {
447+
self.expect_stderr = Some(OutputAssertion {
448+
expect: output.into(),
449+
fuzzy: false,
450+
expected_result: false,
355451
kind: StdErr,
356452
});
357453
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)