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

Rename contains matcher #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub mod prelude {
pub use matchers::regex::matches_regex as match_regex;
pub use matchers::regex::matches_regex;
pub use matchers::vecs::contains;
pub use matchers::vecs::contains_all_of;
pub use matchers::vecs::contains_all_of as contain_all_of;
pub use matchers::vecs::of_len;
pub use matchers::anything::anything;
pub use matchers::type_of::type_of;
Expand Down
20 changes: 19 additions & 1 deletion src/matchers/vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,32 @@ fn is_next_index(current_index: &usize, previous_index: &Option<usize>) -> bool
return true;
}

pub fn contains<T>(items: Vec<T>) -> Contains<T> {
/// Creates a matcher that checks if actual vector has all items of the given vector.
///
/// # Examples
///
/// ```
/// #[macro_use] extern crate hamcrest;
/// use hamcrest::prelude::*;
///
/// fn main() {
/// assert_that!(&vec![1, 2, 3, 4], contains_all_of(vec![1, 2, 3]));
/// assert_that!(&vec![1, 2, 3], does_not(contain_all_of(vec![1, 2, 4])));
/// }
/// ```
pub fn contains_all_of<T>(items: Vec<T>) -> Contains<T> {
Contains {
items: items,
exactly: false,
in_order: false,
}
}

#[deprecated(since = "0.2.0", note = "Use the contains_all_of instead")]
pub fn contains<T>(items: Vec<T>) -> Contains<T> {
contains_all_of(items)
}

struct Pretty<'a, T: 'a>(&'a [T]);

impl<'a, T: fmt::Debug> fmt::Display for Pretty<'a, T> {
Expand Down
4 changes: 2 additions & 2 deletions tests/all_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ mod all_of {
}

#[test]
fn vec_contains() {
fn vec_contains_all_of() {
assert_that!(
&vec![1, 2, 3],
all_of!(contains(vec![1, 2]), not(contains(vec![4])))
all_of!(contains_all_of(vec![1, 2]), not(contains_all_of(vec![4])))
);
}
}
7 changes: 5 additions & 2 deletions tests/any_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ mod any_of {
}

#[test]
fn vec_contains() {
fn vec_contains_all_of() {
assert_that!(
&vec![1, 2, 3],
any_of!(contains(vec![1, 2, 5]), not(contains(vec![4])))
any_of!(
contains_all_of(vec![1, 2, 5]),
not(contains_all_of(vec![4]))
)
);
}
}
25 changes: 13 additions & 12 deletions tests/vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,38 @@ mod vecs {
use hamcrest::prelude::*;

#[test]
fn vec_contains() {
assert_that!(&vec![1, 2, 3], contains(vec![1, 2]));
assert_that!(&vec![1, 2, 3], not(contains(vec![4])));
fn vec_contains_all_of() {
assert_that!(&vec![1, 2, 3], contains_all_of(vec![1, 2]));
assert_that!(&vec![1, 2, 3], not(contains_all_of(vec![4])));
assert_that!(&vec![1, 2, 3], does_not(contain_all_of(vec![4])));
}

#[test]
fn vec_contains_exactly() {
assert_that!(&vec![1, 2, 3], contains(vec![1, 2, 3]).exactly());
assert_that!(&vec![1, 2, 3], not(contains(vec![1, 2]).exactly()));
fn vec_contains_all_of_exactly() {
assert_that!(&vec![1, 2, 3], contains_all_of(vec![1, 2, 3]).exactly());
assert_that!(&vec![1, 2, 3], not(contains_all_of(vec![1, 2]).exactly()));
}

#[test]
fn it_contains_elements_in_order() {
assert_that!(&vec![1, 2, 3], contains(vec![1, 2]).in_order());
fn it_contains_all_of_elements_in_order() {
assert_that!(&vec![1, 2, 3], contains_all_of(vec![1, 2]).in_order());
}

#[test]
fn it_does_not_contain_elements_in_order() {
assert_that!(&vec![1, 2, 3], not(contains(vec![1, 3]).in_order()));
assert_that!(&vec![1, 2, 3], not(contains_all_of(vec![1, 3]).in_order()));
}

#[test]
#[should_panic]
fn it_unsuccessfully_contains_elements_in_order() {
assert_that!(&vec![1, 2, 3], contains(vec![1, 3]).in_order());
fn it_unsuccessfully_contains_all_of_elements_in_order() {
assert_that!(&vec![1, 2, 3], contains_all_of(vec![1, 3]).in_order());
}

#[test]
#[should_panic]
fn it_unsuccessfully_does_not_contain_elements_in_order() {
assert_that!(&vec![1, 2, 3], not(contains(vec![2, 3]).in_order()));
assert_that!(&vec![1, 2, 3], not(contains_all_of(vec![2, 3]).in_order()));
}

#[test]
Expand Down