Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assert): Isolate API details #20

Merged
merged 2 commits into from Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -8,13 +8,13 @@ rust:
matrix:
include:
- env: RUSTFMT
rust: 1.25.0 # `stable`: Locking down for consistent behavior
rust: 1.27.0 # `stable`: Locking down for consistent behavior
install:
- rustup component add rustfmt-preview
script:
- cargo fmt -- --write-mode=diff
- env: RUSTFLAGS="-D warnings"
rust: 1.25.0 # `stable`: Locking down for consistent behavior
rust: 1.27.0 # `stable`: Locking down for consistent behavior
install:
script:
- cargo check --tests --all-features
Expand Down
66 changes: 58 additions & 8 deletions src/assert.rs
@@ -1,3 +1,5 @@
//! `process::Output` assertions.

use std::fmt;
use std::process;
use std::str;
Expand Down Expand Up @@ -400,21 +402,69 @@ where
}
}

impl IntoOutputPredicate<predicates::ord::EqPredicate<&'static [u8]>> for &'static [u8] {
type Predicate = predicates::ord::EqPredicate<&'static [u8]>;
// Keep `predicates` concrete Predicates out of our public API.
/// Predicate used by `IntoOutputPredicate` for bytes
#[derive(Debug)]
pub struct BytesContentOutputPredicate(predicates::ord::EqPredicate<&'static [u8]>);

impl BytesContentOutputPredicate {
pub(crate) fn new(value: &'static [u8]) -> Self {
let pred = predicates::ord::eq(value);
BytesContentOutputPredicate(pred)
}
}

impl predicates::Predicate<[u8]> for BytesContentOutputPredicate {
fn eval(&self, item: &[u8]) -> bool {
self.0.eval(item)
}
}

impl fmt::Display for BytesContentOutputPredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl IntoOutputPredicate<BytesContentOutputPredicate> for &'static [u8] {
type Predicate = BytesContentOutputPredicate;

fn into_output(self) -> Self::Predicate {
predicates::ord::eq(self)
Self::Predicate::new(self)
}
}

impl IntoOutputPredicate<predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>>
for &'static str
{
type Predicate = predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>;
// Keep `predicates` concrete Predicates out of our public API.
/// Predicate used by `IntoOutputPredicate` for `str`
#[derive(Debug, Clone)]
pub struct StrContentOutputPredicate(
predicates::str::Utf8Predicate<predicates::str::DifferencePredicate>,
);

impl StrContentOutputPredicate {
pub(crate) fn new(value: &'static str) -> Self {
let pred = predicates::str::similar(value).from_utf8();
StrContentOutputPredicate(pred)
}
}

impl predicates::Predicate<[u8]> for StrContentOutputPredicate {
fn eval(&self, item: &[u8]) -> bool {
self.0.eval(item)
}
}

impl fmt::Display for StrContentOutputPredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl IntoOutputPredicate<StrContentOutputPredicate> for &'static str {
type Predicate = StrContentOutputPredicate;

fn into_output(self) -> Self::Predicate {
predicates::str::similar(self).from_utf8()
Self::Predicate::new(self)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/cargo.rs
Expand Up @@ -107,7 +107,8 @@ struct MessageFilter<'a> {

fn extract_filenames(msg: &escargot::Message, kind: &str) -> Option<path::PathBuf> {
let filter: MessageFilter = msg.convert().ok()?;
if filter.reason != "compiler-artifact" || filter.target.crate_types != ["bin"]
if filter.reason != "compiler-artifact"
|| filter.target.crate_types != ["bin"]
|| filter.target.kind != [kind]
{
None
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Expand Up @@ -45,8 +45,9 @@ extern crate predicates;
#[macro_use]
extern crate serde;

mod assert;
pub use assert::*;
pub mod assert;
pub use assert::Assert;
pub use assert::OutputAssertExt;
pub mod cargo;
mod cmd;
pub use cmd::*;
Expand Down
2 changes: 1 addition & 1 deletion src/stdin.rs
Expand Up @@ -4,8 +4,8 @@ use std::process;

use assert::Assert;
use assert::OutputAssertExt;
use cmd::OutputOkExt;
use cmd::dump_buffer;
use cmd::OutputOkExt;
use errors::OutputError;
use errors::OutputResult;

Expand Down