To use Hamcrest, add this to your Cargo.toml
:
``` [dev-dependencies.hamcrest]
git = "https://github.com/carllerche/hamcrest-rust.git" ```
And this to your crate root:
```{rust}
extern crate hamcrest; ```
After a quick cargo build
, you should be good to go!
Hamcrest supports a number of matchers. You'll have to use
them just like any other Rust library.
```{rust} // Successful match assertthat(&1, is(equalto(&1i)));
// Unsuccessful match let res = task::try(proc() { assertthat(&2, is(equalto(&1i))); });
assert!(res.is_err()); ```
{rust}
assert_that(1e-40f32, is(close_to(0.0, 0.01)));
assert_that(1e-40f32, is_not(close_to(0.0, 0.000001)));
{rust}
assert_that(&path, is(existing_path()));
assert_that(&path, is(existing_file()));
assert_that(&path, is_not(existing_dir()));
{rust}
assert_that(None, is(none::<int>()));
assert_that(Some(1), is_not(none::<int>()));
```{rust} assertthat(&vec!(1i, 2, 3), contains(vec!(1i, 2))); assertthat(&vec!(1i, 2, 3), not(contains(vec!(4i))));
assertthat(&vec!(1i, 2, 3), contains(vec!(1i, 2, 3)).exactly()); assertthat(&vec!(1i, 2, 3), not(contains(vec!(1i, 2)).exactly())); ```