Skip to content

Commit 1cf53cd

Browse files
authored
Merge pull request #20 from epage/more
fix(assert): Isolate API details
2 parents cb89674 + b32476a commit 1cf53cd

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ rust:
88
matrix:
99
include:
1010
- env: RUSTFMT
11-
rust: 1.25.0 # `stable`: Locking down for consistent behavior
11+
rust: 1.27.0 # `stable`: Locking down for consistent behavior
1212
install:
1313
- rustup component add rustfmt-preview
1414
script:
1515
- cargo fmt -- --write-mode=diff
1616
- env: RUSTFLAGS="-D warnings"
17-
rust: 1.25.0 # `stable`: Locking down for consistent behavior
17+
rust: 1.27.0 # `stable`: Locking down for consistent behavior
1818
install:
1919
script:
2020
- cargo check --tests --all-features

src/assert.rs

+58-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! `process::Output` assertions.
2+
13
use std::fmt;
24
use std::process;
35
use std::str;
@@ -400,21 +402,69 @@ where
400402
}
401403
}
402404

403-
impl IntoOutputPredicate<predicates::ord::EqPredicate<&'static [u8]>> for &'static [u8] {
404-
type Predicate = predicates::ord::EqPredicate<&'static [u8]>;
405+
// Keep `predicates` concrete Predicates out of our public API.
406+
/// Predicate used by `IntoOutputPredicate` for bytes
407+
#[derive(Debug)]
408+
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);
409+
410+
impl BytesContentOutputPredicate {
411+
pub(crate) fn new(value: &'static [u8]) -> Self {
412+
let pred = predicates::ord::eq(value);
413+
BytesContentOutputPredicate(pred)
414+
}
415+
}
416+
417+
impl predicates::Predicate<[u8]> for BytesContentOutputPredicate {
418+
fn eval(&self, item: &[u8]) -> bool {
419+
self.0.eval(item)
420+
}
421+
}
422+
423+
impl fmt::Display for BytesContentOutputPredicate {
424+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
425+
self.0.fmt(f)
426+
}
427+
}
428+
429+
impl IntoOutputPredicate<BytesContentOutputPredicate> for &'static [u8] {
430+
type Predicate = BytesContentOutputPredicate;
405431

406432
fn into_output(self) -> Self::Predicate {
407-
predicates::ord::eq(self)
433+
Self::Predicate::new(self)
408434
}
409435
}
410436

411-
impl IntoOutputPredicate<predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>>
412-
for &'static str
413-
{
414-
type Predicate = predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>;
437+
// Keep `predicates` concrete Predicates out of our public API.
438+
/// Predicate used by `IntoOutputPredicate` for `str`
439+
#[derive(Debug, Clone)]
440+
pub struct StrContentOutputPredicate(
441+
predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>,
442+
);
443+
444+
impl StrContentOutputPredicate {
445+
pub(crate) fn new(value: &'static str) -> Self {
446+
let pred = predicates::str::similar(value).from_utf8();
447+
StrContentOutputPredicate(pred)
448+
}
449+
}
450+
451+
impl predicates::Predicate<[u8]> for StrContentOutputPredicate {
452+
fn eval(&self, item: &[u8]) -> bool {
453+
self.0.eval(item)
454+
}
455+
}
456+
457+
impl fmt::Display for StrContentOutputPredicate {
458+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
459+
self.0.fmt(f)
460+
}
461+
}
462+
463+
impl IntoOutputPredicate<StrContentOutputPredicate> for &'static str {
464+
type Predicate = StrContentOutputPredicate;
415465

416466
fn into_output(self) -> Self::Predicate {
417-
predicates::str::similar(self).from_utf8()
467+
Self::Predicate::new(self)
418468
}
419469
}
420470

src/cargo.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ struct MessageFilter<'a> {
107107

108108
fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBuf> {
109109
let filter: MessageFilter = msg.convert().ok()?;
110-
if filter.reason != "compiler-artifact" || filter.target.crate_types != ["bin"]
110+
if filter.reason != "compiler-artifact"
111+
|| filter.target.crate_types != ["bin"]
111112
|| filter.target.kind != [kind]
112113
{
113114
None

src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ extern crate predicates;
4545
#[macro_use]
4646
extern crate serde;
4747

48-
mod assert;
49-
pub use assert::*;
48+
pub mod assert;
49+
pub use assert::Assert;
50+
pub use assert::OutputAssertExt;
5051
pub mod cargo;
5152
mod cmd;
5253
pub use cmd::*;

src/stdin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::process;
44

55
use assert::Assert;
66
use assert::OutputAssertExt;
7-
use cmd::OutputOkExt;
87
use cmd::dump_buffer;
8+
use cmd::OutputOkExt;
99
use errors::OutputError;
1010
use errors::OutputResult;
1111

0 commit comments

Comments
 (0)