From a598e487733af8e21f010d42653dd88bf34a2ff4 Mon Sep 17 00:00:00 2001 From: Povilas Balciunas Date: Wed, 17 Jan 2018 20:54:08 +0200 Subject: [PATCH 1/2] Add contains_all_of alias to contains Another alias 'contain_all_of' is also added so that it would better fit does_not(contain_all_of()) expression. --- src/lib.rs | 2 ++ src/matchers/vecs.rs | 7 ++++++- tests/all_of.rs | 4 ++-- tests/any_of.rs | 7 +++++-- tests/vecs.rs | 25 +++++++++++++------------ 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 58688c9..739126e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/matchers/vecs.rs b/src/matchers/vecs.rs index 19fff07..ee9b825 100644 --- a/src/matchers/vecs.rs +++ b/src/matchers/vecs.rs @@ -123,7 +123,7 @@ fn is_next_index(current_index: &usize, previous_index: &Option) -> bool return true; } -pub fn contains(items: Vec) -> Contains { +pub fn contains_all_of(items: Vec) -> Contains { Contains { items: items, exactly: false, @@ -131,6 +131,11 @@ pub fn contains(items: Vec) -> Contains { } } +#[deprecated(since = "0.2.0", note = "Use the contains_all_of instead")] +pub fn contains(items: Vec) -> Contains { + contains_all_of(items) +} + struct Pretty<'a, T: 'a>(&'a [T]); impl<'a, T: fmt::Debug> fmt::Display for Pretty<'a, T> { diff --git a/tests/all_of.rs b/tests/all_of.rs index 39e83c9..25911ad 100644 --- a/tests/all_of.rs +++ b/tests/all_of.rs @@ -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]))) ); } } diff --git a/tests/any_of.rs b/tests/any_of.rs index cacb383..04a9d13 100644 --- a/tests/any_of.rs +++ b/tests/any_of.rs @@ -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])) + ) ); } } diff --git a/tests/vecs.rs b/tests/vecs.rs index 9788d77..5e9c85a 100644 --- a/tests/vecs.rs +++ b/tests/vecs.rs @@ -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] From 75bb683ccfe80d3c49a180bf6ef263045e1afaa5 Mon Sep 17 00:00:00 2001 From: Povilas Balciunas Date: Thu, 18 Jan 2018 18:14:22 +0200 Subject: [PATCH 2/2] Document contains_all_of matcher --- src/matchers/vecs.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/matchers/vecs.rs b/src/matchers/vecs.rs index ee9b825..9de4352 100644 --- a/src/matchers/vecs.rs +++ b/src/matchers/vecs.rs @@ -123,6 +123,19 @@ fn is_next_index(current_index: &usize, previous_index: &Option) -> bool return true; } +/// 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(items: Vec) -> Contains { Contains { items: items,