Skip to content

Commit

Permalink
Merge pull request #27 from zummenix/patch_3
Browse files Browse the repository at this point in the history
Reimplement the be_empty matcher using Iterator.
  • Loading branch information
zummenix committed Apr 24, 2016
2 parents 3bd95ed + 77c2958 commit 7b51ad8
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 101 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ expect!("4".parse::<u32>()).to(be_ok().value(4));
Use any of the following matchers: `be_ok`, `be_err`

#### Emptyness
There is `be_empty` matcher for types that implement `IsEmpty` trait:
There is `be_empty` matcher for types that implement `Iterator + Clone` trait:
```rust
expect!("").to(be_empty());
expect!("".chars()).to(be_empty());
```
> Note: `IsEmpty` trait implemented by library for following types:
`String`, `&str`, `Vec<T>`, `&[T]`.

#### Count of elements in a collection
There is `have_count` matcher for types that implement `Iterator + Clone` trait:
Expand Down
23 changes: 0 additions & 23 deletions examples/empty.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ pub mod prelude {

pub mod core;
pub mod matchers;
pub mod traits;
15 changes: 7 additions & 8 deletions src/matchers/empty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

use std::fmt;
use core::{Matcher, Join};
use traits::IsEmpty;

/// A matcher for `be_empty` assertions.
pub struct BeEmpty;
Expand All @@ -11,17 +9,18 @@ pub fn be_empty() -> BeEmpty {
BeEmpty
}

impl<A> Matcher<A, ()> for BeEmpty where A: IsEmpty + fmt::Debug {
impl<A, T> Matcher<A, ()> for BeEmpty where A: Iterator<Item = T> + Clone {
fn failure_message(&self, join: Join, actual: &A) -> String {
if join.is_assertion() {
format!("expected {} be empty, got <{:?}>", join, actual)
let count = actual.clone().count();
format!("expected {} be empty, got the length of {}", join, count)
} else {
format!("expected {} be empty", join)
}
}

fn matches(&self, actual: &A) -> bool {
actual.is_empty()
actual.clone().count() == 0
}
}

Expand All @@ -32,14 +31,14 @@ mod tests {

#[test]
fn be_empty_str_failure_message() {
expect("hello")
expect("hello".chars())
.to(be_empty())
.assert_eq_message("expected to be empty, got <\"hello\">");
.assert_eq_message("expected to be empty, got the length of 5");
}

#[test]
fn to_not_be_empty_str_failure_message() {
expect("")
expect("".chars())
.to_not(be_empty())
.assert_eq_message("expected to not be empty");
}
Expand Down
31 changes: 0 additions & 31 deletions src/traits/is_empty.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/traits/mod.rs

This file was deleted.

32 changes: 4 additions & 28 deletions tests/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,26 @@
extern crate expectest;
use expectest::prelude::*;

#[test]
fn emptiness_of_string() {
expect!("".to_string()).to(be_empty());
}

#[test]
fn emptiness_of_str() {
expect!("").to(be_empty());
}

#[test]
#[should_panic]
fn emptiness_of_string_should_panic() {
expect!("s".to_string()).to(be_empty());
expect!("".chars()).to(be_empty());
}

#[test]
#[should_panic]
fn emptiness_of_str_should_panic() {
expect!("world").to(be_empty());
expect!("world".chars()).to(be_empty());
}

#[test]
fn emptiness_of_vec() {
let v: Vec<u32> = vec![];
expect!(v).to(be_empty());
}

#[test]
fn emptiness_of_array() {
let v: &[u32] = &[];
expect!(v).to(be_empty());
expect!(v.iter()).to(be_empty());
}

#[test]
#[should_panic]
fn emptiness_of_vec_should_panic() {
let v = vec![1, 2];
expect!(v).to(be_empty());
}

#[test]
#[should_panic]
fn emptiness_of_array_should_panic() {
let v: &[u32] = &[1, 2];
expect!(v).to(be_empty());
expect!(v.iter()).to(be_empty());
}

0 comments on commit 7b51ad8

Please sign in to comment.