Giter Club home page Giter Club logo

hamcrest2-rust's Introduction

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:

#[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:

use hamcrest2::prelude::*;

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

General Matchers

eq, not

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

compared_to

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

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

matches_regex

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

Numerical Matchers

close_to

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

Filesystem Matchers

path_exists, file_exists, dir_exists

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

let var: Option<i8> = Some(5);
assert_that!(var, has(5));

let var: Result<i8, String> = Ok(5);
assert_that!(var, has(5));

ok

let var: Result<i8, String> = Ok(5);
assert_that!(var, ok());
assert_that!(&var, ok());

assert_that!(Ok(5), ok::<i8, String>());
assert_that!(&Ok(5), ok::<i8, String>());

let var: Result<i8, String> = Err("bad!".to_string());
assert_that!(var, not(ok()));
assert_that!(&var, not(ok()));

err

let var: Result<i8, String> = Err("bad!".to_string());
assert_that!(var, err());
assert_that!(&var, err());

assert_that!(Err("bad!".to_string()), err::<i8, String>());
assert_that!(&Err("bad!".to_string()), err::<i8, String>());

let var: Result<i8, String> = Ok(5);
assert_that!(var, not(err()));
assert_that!(&var, not(err()));

some

let var: Option<i8> = Some(5);
assert_that!(var, some());
assert_that!(&var, some());

assert_that!(Some(1), some::<u8>());
assert_that!(&Some(1), some::<u8>());

let var: Option<i8> = None;
assert_that!(var, not(some()));
assert_that!(&var, not(some()));

none

let var: Option<i8> = None;
assert_that!(var, none());
assert_that!(&var, none());

assert_that!(None, none::<u8>());
assert_that!(&None, none::<u8>());
assert_that!(Some(1), not(none::<u8>()));
assert_that!(&Some(1), not(none::<u8>()));

Collection Matchers

contains, contains exactly, contains in order

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

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

assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2)).in_order());
assert_that!(&vec!(1, 2, 3), not(contains(vec!(1, 3)).in_order()));

len

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

empty

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

Compound Matchers

all

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

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)

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

anything

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.

hamcrest2-rust's People

Contributors

alexcrichton avatar aomader avatar carllerche avatar daaang avatar ebfe avatar flier avatar gerschtli avatar globin avatar grahamdennis avatar ianlet avatar killercup avatar o11c avatar povilasb avatar steveklabnik avatar tamird avatar tilde-engineering avatar ujh avatar valloric avatar vhbit avatar wagenet avatar wycats avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.