Build Status

Hamcrest2

A port of Hamcrest to Rust. Fork of original hamcrest-rust (which is unmaintained) with extra matchers, better docs etc.

Installing

To use Hamcrest, add this to your Cargo.toml:

[dev-dependencies] hamcrest2 = "*"

And this to your crate root:

```rust

[cfg(test)] #[macro_use] extern crate hamcrest2;

```

After a quick cargo build, you should be good to go!

Usage

Hamcrest2 supports a number of matchers. The easiest way is to just use them all like this:

rust use hamcrest2::prelude::*;

If you want to be more selective make sure that you also import the HamcrestMatcher trait.

General Matchers

eq, not

rust assert_that!(1, eq(1)); // also equal_to() assert_that!(1, not(eq(2)));

compared_to

rust assert_that!(1, lt(2)); // also less_than() assert_that!(1, leq(1)); // also less_than_or_equal_to() assert_that!(2, gt(1)); // also greater_than() assert_that!(2, geq(2)); // also greater_than_or_equal_to()

type_of

rust assert_that!(123usize, type_of::<usize>()); assert_that!("test", type_of::<&str>());

matches_regex

rust assert_that!("1234", matches_regex(r"\d")); assert_that!("abc", does_not(match_regex(r"\d")));

Numerical Matchers

close_to

rust assert_that!(1e-40f32, close_to(0.0, 0.01)); assert_that!(1e-40f32, not(close_to(0.0, 0.000001)));

Filesystem Matchers

pathexists, fileexists, dir_exists

rust let path = Path::new("./README.md"); assert_that!(path, path_exists()); assert_that!(path, file_exists()); assert_that!(path, not(dir_exists()));

Option and Result

has

```rust let var: Option = Some(5); assert_that!(var, has(5));

let var: Result = Ok(5); assert_that!(var, has(5)); ```

ok

```rust let var: Result = Ok(5); assertthat!(var, ok()); assertthat!(&var, ok());

assertthat!(Ok(5), ok::()); assertthat!(&Ok(5), ok::());

let var: Result = Err("bad!".tostring()); assertthat!(var, not(ok())); assert_that!(&var, not(ok())); ```

err

```rust let var: Result = Err("bad!".tostring()); assertthat!(var, err()); assert_that!(&var, err());

assertthat!(Err("bad!".tostring()), err::()); assertthat!(&Err("bad!".tostring()), err::());

let var: Result = Ok(5); assertthat!(var, not(err())); assertthat!(&var, not(err())); ```

some

```rust let var: Option = Some(5); assertthat!(var, some()); assertthat!(&var, some());

assertthat!(Some(1), some::()); assertthat!(&Some(1), some::());

let var: Option = None; assertthat!(var, not(some())); assertthat!(&var, not(some())); ```

none

```rust let var: Option = None; assertthat!(var, none()); assertthat!(&var, none());

assertthat!(None, none::()); assertthat!(&None, none::()); assertthat!(Some(1), not(none::())); assertthat!(&Some(1), not(none::())); ```

Collection Matchers

contains, contains exactly, contains in order

```rust assertthat!(&vec!(1, 2, 3), contains(vec!(1, 2))); assertthat!(&vec!(1, 2, 3), contains(1)); assert_that!(&vec!(1, 2, 3), not(contains(4i)));

assertthat!(&vec!(1, 2, 3), contains(vec!(1, 2, 3)).exactly()); assertthat!(&vec!(1, 2, 3), not(contains(vec!(1, 2)).exactly()));

assertthat!(&vec!(1, 2, 3), contains(vec!(1, 2)).inorder()); assertthat!(&vec!(1, 2, 3), not(contains(vec!(1, 3)).inorder())); ```

len

rust assert_that!(&vec!(1, 2, 3), len(3)); assert_that!(&vec!(1, 2, 3), not(len(4)));

empty

rust assert_that!(&Vec::<i32>::new(), empty()); assert_that!(&vec![1, 2, 3], not(empty()));

Compound Matchers

all

rust assert_that!(4, all!(lt(5), gt(3))); // also and!() assert_that!( &vec![1, 2, 3], all!(contains(vec![1, 2]), not(contains(vec![4]))) );

any

rust assert_that!(4, any!(less_than(2), greater_than(3))); // also or!() assert_that!( &vec![1, 2, 3], any!(contains(vec![1, 2, 5]), not(contains(vec![4]))) );

Misc Matchers

is(bool)

rust assert_that!(true, is(true)); assert_that!(false, is(false));

anything

rust assert_that!(42, anything()); assert_that!("test", is(anything()));

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.