Giter Site home page Giter Site logo

hamcrest-rust's Introduction

Build Status

Hamcrest

A port of Hamcrest to Rust.

Installing

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

[dev-dependencies]
hamcrest = "*"

And this to your crate root:

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

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

Usage

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

use hamcrest::prelude::*;

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

equal_to

assert_that!(1, is(equal_to(1)));

close_to

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

compared_to

assert_that!(1, is(less_than(2)));
assert_that!(1, is(less_than_or_equal_to(1)));
assert_that!(2, is(greater_than(1)));
assert_that!(2, is(greater_than_or_equal_to(2)));

existing_{file,path,dir}

assert_that!(&path, is(existing_path()));
assert_that!(&path, is(existing_file()));
assert_that!(&path, is_not(existing_dir()));

none

assert_that!(None, is(none::<int>()));
assert_that!(Some(1), is_not(none::<int>()));

anything

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

contains, contains_exactly, contains_in order

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

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

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

matches_regex

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

type_of

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

all_of

assert_that!(4, all_of!(less_than(5), greater_than(3)));
assert_that!(
    &vec![1, 2, 3],
    all_of!(contains(vec![1, 2]), not(contains(vec![4])))
);

any_of

assert_that!(4, any_of!(less_than(2), greater_than(3)));
assert_that!(
    &vec![1, 2, 3],
    any_of!(contains(vec![1, 2, 5]), not(contains(vec![4])))
);

is(bool)

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

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.

hamcrest-rust's People

Contributors

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

Watchers

James Cloos avatar Igor Raits 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.