pub struct Spec<'s, S: 's> {
pub subject: &'s S,
pub subject_name: Option<&'s str>,
pub location: Option<String>,
pub description: Option<&'s str>,
}
Expand description
An assertion.
This is created by either the assert_that
function, or by calling that
on a
SpecDescription
.
Fields
subject: &'s S
subject_name: Option<&'s str>
location: Option<String>
description: Option<&'s str>
Implementations
sourceimpl<'s, S> Spec<'s, S>
impl<'s, S> Spec<'s, S>
sourcepub fn at_location(self, location: String) -> Self
pub fn at_location(self, location: String) -> Self
Provides the actual location of the assertion.
Usually you would not call this directly, but use the macro forms of assert_that
and
asserting
, which will call this on your behalf with the correct location.
sourceimpl<'s, S> Spec<'s, S> where
S: Debug + PartialEq,
impl<'s, S> Spec<'s, S> where
S: Debug + PartialEq,
sourcepub fn is_equal_to<E: Borrow<S>>(&mut self, expected: E)
pub fn is_equal_to<E: Borrow<S>>(&mut self, expected: E)
Asserts that the actual value and the expected value are equal. The value type must
implement PartialEq
.
assert_that(&"hello").is_equal_to(&"hello");
sourcepub fn is_not_equal_to<E: Borrow<S>>(&mut self, expected: E)
pub fn is_not_equal_to<E: Borrow<S>>(&mut self, expected: E)
Asserts that the actual value and the expected value are not equal. The value type must
implement PartialEq
.
assert_that(&"hello").is_not_equal_to(&"olleh");
sourceimpl<'s, S> Spec<'s, S> where
S: Debug,
impl<'s, S> Spec<'s, S> where
S: Debug,
sourcepub fn matches<F>(&mut self, matching_function: F) where
F: Fn(&'s S) -> bool,
pub fn matches<F>(&mut self, matching_function: F) where
F: Fn(&'s S) -> bool,
Accepts a function accepting the value type which returns a bool. Returning false will cause the assertion to fail.
NOTE: The resultant panic message will only state the actual value. It’s recommended that you write your own assertion rather than relying upon this.
assert_that(&"hello").matches(|x| x.eq(&"hello"));
Trait Implementations
sourceimpl<'s> BooleanAssertions for Spec<'s, bool>
impl<'s> BooleanAssertions for Spec<'s, bool>
sourceimpl<'s, T: 's, I> ContainingIntoIterAssertions<'s, T> for Spec<'s, I> where
T: Debug + PartialEq,
&'s I: IntoIterator<Item = &'s T>,
impl<'s, T: 's, I> ContainingIntoIterAssertions<'s, T> for Spec<'s, I> where
T: Debug + PartialEq,
&'s I: IntoIterator<Item = &'s T>,
sourcefn contains<E: 's + Borrow<T>>(&mut self, expected_value: E)
fn contains<E: 's + Borrow<T>>(&mut self, expected_value: E)
Asserts that the subject contains the provided value. The subject must implement
IntoIterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec).contains(&2);
sourcefn contains_all_of<E: 's>(&mut self, expected_values_iter: &'s E) where
E: IntoIterator<Item = &'s T> + Clone,
fn contains_all_of<E: 's>(&mut self, expected_values_iter: &'s E) where
E: IntoIterator<Item = &'s T> + Clone,
Asserts that the subject contains all of the provided values. The subject must implement
IntoIterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec).contains_all_of(&vec![&2, &3]);
sourcefn does_not_contain<E: 's + Borrow<T>>(&mut self, expected_value: E)
fn does_not_contain<E: 's + Borrow<T>>(&mut self, expected_value: E)
Asserts that the subject does not contain the provided value. The subject must implement
IntoIterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec).does_not_contain(&4);
sourcefn equals_iterator<E: 's>(&mut self, expected_iter: &'s E) where
E: Iterator<Item = &'s T> + Clone,
fn equals_iterator<E: 's>(&mut self, expected_iter: &'s E) where
E: Iterator<Item = &'s T> + Clone,
Asserts that the subject is equal to provided iterator. The subject must implement
IntoIterator
, the contained type must implement PartialEq
and Debug
and the expected
value must implement Iterator and Clone.
let expected_vec = vec![1,2,3];
let test_vec = vec![1,2,3];
assert_that(&test_vec).equals_iterator(&expected_vec.iter());
sourceimpl<'s, T: 's, I> ContainingIteratorAssertions<'s, T> for Spec<'s, I> where
T: Debug + PartialEq,
I: Iterator<Item = &'s T> + Clone,
impl<'s, T: 's, I> ContainingIteratorAssertions<'s, T> for Spec<'s, I> where
T: Debug + PartialEq,
I: Iterator<Item = &'s T> + Clone,
sourcefn contains<E: 's + Borrow<T>>(&mut self, expected_value: E)
fn contains<E: 's + Borrow<T>>(&mut self, expected_value: E)
Asserts that the iterable subject contains the provided value. The subject must implement
Iterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec.iter()).contains(&2);
sourcefn contains_all_of<E: 's>(&mut self, expected_values_iter: &'s E) where
E: IntoIterator<Item = &'s T> + Clone,
fn contains_all_of<E: 's>(&mut self, expected_values_iter: &'s E) where
E: IntoIterator<Item = &'s T> + Clone,
Asserts that the subject contains all of the provided values. The subject must implement
Iterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec.iter()).contains_all_of(&vec![&2, &3]);
sourcefn does_not_contain<E: 's + Borrow<T>>(&mut self, expected_value: E)
fn does_not_contain<E: 's + Borrow<T>>(&mut self, expected_value: E)
Asserts that the iterable subject does not contain the provided value. The subject must
implement Iterator
, and the contained type must implement PartialEq
and Debug
.
let test_vec = vec![1,2,3];
assert_that(&test_vec.iter()).does_not_contain(&4);
sourcefn equals_iterator<E: 's>(&mut self, expected_iter: &'s E) where
E: Iterator<Item = &'s T> + Clone,
fn equals_iterator<E: 's>(&mut self, expected_iter: &'s E) where
E: Iterator<Item = &'s T> + Clone,
Asserts that the iterable subject is equal to provided iterator. The subject must implement
Iterator
, the contained type must implement PartialEq
and Debug
and the expected
value must implement Iterator and Clone.
let expected_vec = vec![1,2,3];
let test_vec = vec![1,2,3];
assert_that(&test_vec.iter()).equals_iterator(&expected_vec.iter());
sourceimpl<'s, T> ContainingOptionAssertions<T> for Spec<'s, Option<T>> where
T: Debug + PartialEq,
impl<'s, T> ContainingOptionAssertions<T> for Spec<'s, Option<T>> where
T: Debug + PartialEq,
sourcefn contains_value<E: Borrow<T>>(&mut self, expected_value: E)
fn contains_value<E: Borrow<T>>(&mut self, expected_value: E)
Asserts that the subject is a Some
containing the expected value. The subject type must
be an Option
.
assert_that(&Some(1)).contains_value(&1);
sourceimpl<'s, T, E> ContainingResultAssertions<T, E> for Spec<'s, Result<T, E>> where
T: Debug,
E: Debug,
impl<'s, T, E> ContainingResultAssertions<T, E> for Spec<'s, Result<T, E>> where
T: Debug,
E: Debug,
sourcefn is_ok_containing<V: Borrow<T>>(&mut self, expected_value: V) where
T: PartialEq,
fn is_ok_containing<V: Borrow<T>>(&mut self, expected_value: V) where
T: PartialEq,
Asserts that the subject is an Ok
Result containing the expected value.
The subject type must be a Result
.
assert_that(&Result::Ok::<usize, usize>(1)).is_ok_containing(&1);
sourcefn is_err_containing<V: Borrow<E>>(&mut self, expected_value: V) where
E: PartialEq,
fn is_err_containing<V: Borrow<E>>(&mut self, expected_value: V) where
E: PartialEq,
Asserts that the subject is an Err
Result containing the expected value.
The subject type must be a Result
.
assert_that(&Result::Err::<usize, usize>(1)).is_err_containing(&1);
sourceimpl<'r, T> DescriptiveSpec<'r> for Spec<'r, T>
impl<'r, T> DescriptiveSpec<'r> for Spec<'r, T>
sourceimpl<'s, K, V> EntryHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: PartialEq + Debug,
impl<'s, K, V> EntryHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: PartialEq + Debug,
sourcefn contains_entry<E: Borrow<K>, F: Borrow<V>>(
&mut self,
expected_key: E,
expected_value: F
)
fn contains_entry<E: Borrow<K>, F: Borrow<V>>(
&mut self,
expected_key: E,
expected_value: F
)
Asserts that the subject hashmap contains the expected key with the expected value.
The subject type must be of HashMap
.
let mut test_map = HashMap::new();
test_map.insert("hello", "hi");
assert_that(&test_map).contains_entry(&"hello", &"hi");
sourcefn does_not_contain_entry<E: Borrow<K>, F: Borrow<V>>(
&mut self,
expected_key: E,
expected_value: F
)
fn does_not_contain_entry<E: Borrow<K>, F: Borrow<V>>(
&mut self,
expected_key: E,
expected_value: F
)
Asserts that the subject hashmap does not contains the provided key and value.
The subject type must be of HashMap
.
let mut test_map = HashMap::new();
test_map.insert("hello", "hi");
assert_that(&test_map).does_not_contain_entry(&"hello", &"hey");
sourceimpl<'s, T: Float + Debug> FloatAssertions<T> for Spec<'s, T>
impl<'s, T: Float + Debug> FloatAssertions<T> for Spec<'s, T>
sourcefn is_close_to<E: Borrow<T>, O: Borrow<T>>(&mut self, expected: E, tolerance: O)
fn is_close_to<E: Borrow<T>, O: Borrow<T>>(&mut self, expected: E, tolerance: O)
Asserts that the subject is close to the expected value by the specified tolerance.
The subject type must implement Float
and Debug
.
assert_that(&2.0f64).is_close_to(2.0f64, 0.01f64);
sourceimpl<'s, K, V> HashMapAssertions<'s> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: Debug,
impl<'s, K, V> HashMapAssertions<'s> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: Debug,
sourcefn has_length(&mut self, expected: usize)
fn has_length(&mut self, expected: usize)
Asserts that the length of the subject hashmap is equal to the provided length. The subject
type must be of HashMap
.
let mut test_map = HashMap::new();
test_map.insert(1, 1);
test_map.insert(2, 2);
assert_that(&test_map).has_length(2);
sourceimpl<'s, K, V> KeyHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: Debug,
impl<'s, K, V> KeyHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>> where
K: Hash + Eq + Debug,
V: Debug,
sourcefn contains_key<E: Borrow<K>>(&mut self, expected_key: E) -> Spec<'s, V>
fn contains_key<E: Borrow<K>>(&mut self, expected_key: E) -> Spec<'s, V>
Asserts that the subject hashmap contains the expected key. The subject type must be
of HashMap
.
This will return a new Spec
containing the associated value if the key is present.
let mut test_map = HashMap::new();
test_map.insert("hello", "hi");
assert_that(&test_map).contains_key(&"hello");
sourcefn does_not_contain_key<E: Borrow<K>>(&mut self, expected_key: E)
fn does_not_contain_key<E: Borrow<K>>(&mut self, expected_key: E)
Asserts that the subject hashmap does not contain the provided key. The subject type must be
of HashMap
.
let mut test_map = HashMap::new();
test_map.insert("hello", "hi");
assert_that(&test_map).does_not_contain_key(&"hey");
sourceimpl<'s, T: 's, I> MappingIterAssertions<'s, T> for Spec<'s, I> where
T: Debug,
&'s I: IntoIterator<Item = &'s T>,
impl<'s, T: 's, I> MappingIterAssertions<'s, T> for Spec<'s, I> where
T: Debug,
&'s I: IntoIterator<Item = &'s T>,
sourcefn matching_contains<F>(&mut self, matcher: F) where
F: Fn(&'s T) -> bool,
fn matching_contains<F>(&mut self, matcher: F) where
F: Fn(&'s T) -> bool,
Asserts that the subject contains a matching item by using the provided function.
The subject must implement IntoIterator
, and the contained type must implement Debug
.
#[derive(Debug, PartialEq)]
let mut test_into_iter = LinkedList::new();
test_into_iter.push_back(TestEnum::Bad);
test_into_iter.push_back(TestEnum::Good);
test_into_iter.push_back(TestEnum::Bad);
assert_that(&test_into_iter).matching_contains(|val| {
match val {
&TestEnum::Good => true,
_ => false
}
});
sourcefn mapped_contains<F, M: 's>(&mut self, mapping_function: F, expected_value: &M) where
M: Debug + PartialEq,
F: Fn(&'s T) -> M,
fn mapped_contains<F, M: 's>(&mut self, mapping_function: F, expected_value: &M) where
M: Debug + PartialEq,
F: Fn(&'s T) -> M,
Maps the values of the subject before asserting that the mapped subject contains the
provided value. The subject must implement IntoIterator, and the type of the mapped
value must implement PartialEq
.
NOTE: The panic message will refer to the mapped values rather than the values present in the original subject.
#[derive(PartialEq, Debug)]
struct Simple {
pub val: usize,
}
//...
assert_that(&vec![Simple { val: 1 }, Simple { val: 2 } ]).mapped_contains(|x| x.val, &2);
sourceimpl<'s, T> OptionAssertions<'s, T> for Spec<'s, Option<T>> where
T: Debug,
impl<'s, T> OptionAssertions<'s, T> for Spec<'s, Option<T>> where
T: Debug,
sourceimpl<'s, T> OrderedAssertions<T> for Spec<'s, T> where
T: Debug + PartialOrd,
impl<'s, T> OrderedAssertions<T> for Spec<'s, T> where
T: Debug + PartialOrd,
sourcefn is_less_than<E: Borrow<T>>(&mut self, other: E)
fn is_less_than<E: Borrow<T>>(&mut self, other: E)
Asserts that the subject is less than the expected value. The subject type must
implement PartialOrd
.
assert_that(&1).is_less_than(&2);
sourcefn is_less_than_or_equal_to<E: Borrow<T>>(&mut self, other: E)
fn is_less_than_or_equal_to<E: Borrow<T>>(&mut self, other: E)
Asserts that the subject is less than or equal to the expected value. The subject type
must implement PartialOrd
.
assert_that(&2).is_less_than_or_equal_to(&2);
sourcefn is_greater_than<E: Borrow<T>>(&mut self, other: E)
fn is_greater_than<E: Borrow<T>>(&mut self, other: E)
Asserts that the subject is greater than the expected value. The subject type must
implement PartialOrd
.
assert_that(&2).is_greater_than(&1);
sourcefn is_greater_than_or_equal_to<E: Borrow<T>>(&mut self, other: E)
fn is_greater_than_or_equal_to<E: Borrow<T>>(&mut self, other: E)
Asserts that the subject is greater than or equal to the expected value. The subject type
must implement PartialOrd
.
assert_that(&2).is_greater_than_or_equal_to(&1);
sourceimpl<'s, T> PathAssertions for Spec<'s, T> where
T: AsRef<Path>,
impl<'s, T> PathAssertions for Spec<'s, T> where
T: AsRef<Path>,
sourcefn exists(&mut self)
fn exists(&mut self)
Asserts that the subject Path
refers to an existing location.
assert_that(&Path::new("/tmp/file")).exists();
sourcefn does_not_exist(&mut self)
fn does_not_exist(&mut self)
Asserts that the subject Path
does not refer to an existing location.
assert_that(&Path::new("/tmp/file")).does_not_exist();
sourcefn is_a_file(&mut self)
fn is_a_file(&mut self)
Asserts that the subject Path
refers to an existing file.
assert_that(&Path::new("/tmp/file")).is_a_file();
sourcefn is_a_directory(&mut self)
fn is_a_directory(&mut self)
Asserts that the subject Path
refers to an existing directory.
assert_that(&Path::new("/tmp/dir/")).is_a_directory();
sourcefn has_file_name<'r, E: Borrow<&'r str>>(&mut self, expected_file_name: E)
fn has_file_name<'r, E: Borrow<&'r str>>(&mut self, expected_file_name: E)
Asserts that the subject Path
has the expected file name.
assert_that(&Path::new("/tmp/file")).has_file_name(&"file");
sourceimpl<'s, T, E> ResultAssertions<'s, T, E> for Spec<'s, Result<T, E>> where
T: Debug,
E: Debug,
impl<'s, T, E> ResultAssertions<'s, T, E> for Spec<'s, Result<T, E>> where
T: Debug,
E: Debug,
sourceimpl<'s, T> StrAssertions<T> for Spec<'s, T> where
T: AsRef<str>,
impl<'s, T> StrAssertions<T> for Spec<'s, T> where
T: AsRef<str>,
sourcefn starts_with<E: AsRef<str>>(&mut self, expected: E)
fn starts_with<E: AsRef<str>>(&mut self, expected: E)
Asserts that the subject &str
starts with the provided &str
.
assert_that(&"Hello").starts_with("H");
sourcefn ends_with<E: AsRef<str>>(&mut self, expected: E)
fn ends_with<E: AsRef<str>>(&mut self, expected: E)
Asserts that the subject &str
ends with the provided &str
.
assert_that(&"Hello").ends_with("o");
sourcefn contains<E: AsRef<str>>(&mut self, expected: E)
fn contains<E: AsRef<str>>(&mut self, expected: E)
Asserts that the subject &str
contains the provided &str
.
assert_that(&"Hello").contains("e");
sourcefn does_not_contain<E: AsRef<str>>(&mut self, expected: E)
fn does_not_contain<E: AsRef<str>>(&mut self, expected: E)
Asserts that the subject &str
contains the provided &str
.
assert_that(&"Hello").contains("e");
sourceimpl<'s, T> VecAssertions for Spec<'s, Vec<T>>
impl<'s, T> VecAssertions for Spec<'s, Vec<T>>
sourcefn has_length(&mut self, expected: usize)
fn has_length(&mut self, expected: usize)
Asserts that the length of the subject vector is equal to the provided length. The subject
type must be of Vec
.
assert_that!(vec![1, 2, 3, 4]).has_length(4);
sourcefn is_empty(&mut self)
fn is_empty(&mut self)
Asserts that the subject vector is empty. The subject type must be of Vec
.
let test_vec: Vec<u8> = vec![];
assert_that!(test_vec).is_empty();
sourcefn is_not_empty(&mut self)
fn is_not_empty(&mut self)
Asserts that the subject vector is not empty. The subject type must be of Vec
.
let test_vec: Vec<u8> = vec![1];
assert_that!(test_vec).is_not_empty();
sourceimpl<'s, T> VecAssertions for Spec<'s, &'s Vec<T>>
impl<'s, T> VecAssertions for Spec<'s, &'s Vec<T>>
sourcefn has_length(&mut self, expected: usize)
fn has_length(&mut self, expected: usize)
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.
assert_that(&&vec![1, 2, 3, 4]).has_length(4);
sourcefn is_empty(&mut self)
fn is_empty(&mut self)
Asserts that the subject vector is empty. The subject type must be of &Vec
with a
matching lifetime.
let test_vec: &Vec<u8> = &vec![];
assert_that(&test_vec).is_empty();
sourcefn is_not_empty(&mut self)
fn is_not_empty(&mut self)
Asserts that the subject vector is not empty. The subject type must be of &Vec
with a
matching lifetime.
let test_vec: Vec<u8> = vec![1];
assert_that(&test_vec).is_not_empty();
Auto Trait Implementations
impl<'s, S> RefUnwindSafe for Spec<'s, S> where
S: RefUnwindSafe,
impl<'s, S> Send for Spec<'s, S> where
S: Sync,
impl<'s, S> Sync for Spec<'s, S> where
S: Sync,
impl<'s, S> Unpin for Spec<'s, S>
impl<'s, S> UnwindSafe for Spec<'s, S> where
S: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more