diff --git a/src/option.rs b/src/option.rs index cff97e0..a375b52 100644 --- a/src/option.rs +++ b/src/option.rs @@ -122,6 +122,11 @@ mod tests { assert_that(&option).is_some().is_equal_to(&"Hello"); } + #[test] + fn should_be_able_to_unwrap_referenced_vec() { + assert_that(&Some(&vec![1,2,3])).is_some().has_length(3); + } + #[test] fn contains_value_should_allow_multiple_borrow_types() { let option = Some("Hello"); diff --git a/src/vec.rs b/src/vec.rs index 2b949d1..7c31ac5 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -40,6 +40,43 @@ impl<'s, T> VecAssertions for Spec<'s, Vec> { } } +impl<'s, T> VecAssertions for Spec<'s, &'s Vec> { + /// Asserts that the length of the subject vector is equal to the provided length. The subject + /// type must be of `&Vec` with a matching lifetime. + /// + /// ```rust,ignore + /// assert_that(&&vec![1, 2, 3, 4]).has_length(4); + /// ``` + fn has_length(&mut self, expected: usize) { + let length = self.subject.len(); + if length != expected { + AssertionFailure::from_spec(self) + .with_expected(format!("vec to have length <{}>", expected)) + .with_actual(format!("<{}>", length)) + .fail(); + } + } + + /// Asserts that the subject vector is empty. The subject type must be of `&Vec` with a + /// matching lifetime. + /// + /// ```rust,ignore + /// let test_vec: &Vec = &vec![]; + /// assert_that(&test_vec).is_empty(); + /// ``` + fn is_empty(&mut self) { + let subject = self.subject; + + if !subject.is_empty() { + AssertionFailure::from_spec(self) + .with_expected(format!("an empty vec")) + .with_actual(format!("a vec with length <{:?}>", subject.len())) + .fail(); + } + } +} + + #[cfg(test)] mod tests { @@ -49,6 +86,7 @@ mod tests { fn should_not_panic_if_vec_length_matches_expected() { let test_vec = vec![1, 2, 3]; assert_that(&test_vec).has_length(3); + assert_that(&&test_vec).has_length(3); } #[test] @@ -56,12 +94,14 @@ mod tests { fn should_panic_if_vec_length_does_not_match_expected() { let test_vec = vec![1, 2, 3]; assert_that(&test_vec).has_length(1); + assert_that(&&test_vec).has_length(1); } #[test] fn should_not_panic_if_vec_was_expected_to_be_empty_and_is() { let test_vec: Vec = vec![]; assert_that(&test_vec).is_empty(); + assert_that(&&test_vec).is_empty(); } #[test] @@ -69,6 +109,7 @@ mod tests { \n\t but was: a vec with length <1>")] fn should_panic_if_vec_was_expected_to_be_empty_and_is_not() { assert_that(&vec![1]).is_empty(); + assert_that(&&vec![1]).is_empty(); } }