Skip to main content

hamcrest2/
lib.rs

1// Copyright 2014 Carl Lerche, Oliver Mader, Alex Crichton, Thiago Pontes,
2//                Yehuda Katz
3// Copyright 2015 Carl Lerche, Oliver Mader
4// Copyright 2016 Urban Hafner
5// Copyright 2018 Val Markovic
6//
7// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10// option. This file may not be copied, modified, or distributed
11// except according to those terms.
12
13//! ## Usage
14//!
15//! Hamcrest2 supports a number of matchers. The easiest way is to just `use`
16//! them all like this:
17//!
18//! ```
19//! use hamcrest2::prelude::*;
20//! ```
21//!
22//! If you want to be more selective make sure that you also import the
23//! `HamcrestMatcher` trait.
24//!
25//! ## General Matchers
26//!
27//! ### eq, not
28//!
29//! ```
30//! # use hamcrest2::prelude::*;
31//! assert_that!(1, eq(1));  // also equal_to()
32//! assert_that!(1, not(eq(2)));
33//! ```
34//!
35//! ### compared\_to
36//!
37//! ```
38//! # use hamcrest2::prelude::*;
39//! assert_that!(1, lt(2));   // also less_than()
40//! assert_that!(1, leq(1));  // also less_than_or_equal_to()
41//! assert_that!(2, gt(1));   // also greater_than()
42//! assert_that!(2, geq(2));  // also greater_than_or_equal_to()
43//! ```
44//!
45//! ### type_of
46//!
47//! ```
48//! # use hamcrest2::prelude::*;
49//! assert_that!(123usize, type_of::<usize>());
50//! assert_that!("test", type_of::<&str>());
51//! ```
52//!
53//! ### matches_regex
54//!
55//! ```
56//! # use hamcrest2::prelude::*;
57//! assert_that!("1234", matches_regex(r"\d"));
58//! assert_that!("abc", does_not(match_regex(r"\d")));
59//! ```
60//!
61//! ## Numerical Matchers
62//!
63//! ### close_to
64//!
65//! ```
66//! # use hamcrest2::prelude::*;
67//! assert_that!(1e-40f32, close_to(0.0, 0.01));
68//! assert_that!(1e-40f32, not(close_to(0.0, 0.000001)));
69//! ```
70//!
71//! ## Filesystem Matchers
72//!
73//! ### path_exists, file_exists, dir_exists
74//!
75//! ```
76//! # use hamcrest2::prelude::*;
77//! # pub use std::path::Path;
78//! let path = Path::new("./README.md");
79//! assert_that!(path, path_exists());
80//! assert_that!(path, file_exists());
81//! assert_that!(path, not(dir_exists()));
82//! ```
83//!
84//! ## Option and Result
85//!
86//! ### has
87//!
88//! ```
89//! # use hamcrest2::prelude::*;
90//! let var: Option<i8> = Some(5);
91//! assert_that!(var, has(5));
92//!
93//! let var: Result<i8, String> = Ok(5);
94//! assert_that!(var, has(5));
95//! ```
96//! ### ok
97//!
98//! ```
99//! # use hamcrest2::prelude::*;
100//! let var: Result<i8, String> = Ok(5);
101//! assert_that!(var, ok());
102//!
103//! assert_that!(Ok(5), ok::<i8, String>());
104//!
105//! let var: Result<i8, String> = Err("bad!".to_string());
106//! assert_that!(var, not(ok()));
107//! ```
108//!
109//! ### err
110//!
111//! ```
112//! # use hamcrest2::prelude::*;
113//! let var: Result<i8, String> = Err("bad!".to_string());
114//! assert_that!(var, err());
115//!
116//! assert_that!(Err("bad!".to_string()), err::<i8, String>());
117//!
118//! let var: Result<i8, String> = Ok(5);
119//! assert_that!(var, not(err()));
120//! ```
121//!
122//! ### some
123//!
124//! ```
125//! # use hamcrest2::prelude::*;
126//! let var: Option<i8> = Some(5);
127//! assert_that!(var, some());
128//!
129//! assert_that!(Some(1), some::<u8>());
130//!
131//! let var: Option<i8> = None;
132//! assert_that!(var, not(some()));
133//! ```
134//!
135//! ### none
136//!
137//! ```
138//! # use hamcrest2::prelude::*;
139//! let var: Option<i8> = None;
140//! assert_that!(var, none());
141//!
142//! assert_that!(None, none::<u8>());
143//! assert_that!(Some(1), not(none::<u8>()));
144//! ```
145//!
146//! ## Collection Matchers
147//!
148//! ### contains, contains exactly, contains in order
149//!
150//! ```
151//! # use hamcrest2::prelude::*;
152//! assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2)));
153//! assert_that!(&vec!(1, 2, 3), not(contains(vec!(4))));
154//!
155//! assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2, 3)).exactly());
156//! assert_that!(&vec!(1, 2, 3), not(contains(vec!(1, 2)).exactly()));
157//!
158//! assert_that!(&vec!(1, 2, 3), contains(vec!(1, 2)).in_order());
159//! assert_that!(&vec!(1, 2, 3), not(contains(vec!(1, 3)).in_order()));
160//! ```
161//!
162//! ## len
163//! ```
164//! # use hamcrest2::prelude::*;
165//! assert_that!(&vec!(1, 2, 3), len(3));
166//! assert_that!(&vec!(1, 2, 3), not(len(4)));
167//! ```
168//!
169//! ## empty
170//! ```
171//! # use hamcrest2::prelude::*;
172//! assert_that!(&Vec::<i32>::new(), empty());
173//! assert_that!(&vec![1, 2, 3], not(empty()));
174//! ```
175//!
176//! ## Compound Matchers
177//!
178//! ### all
179//!
180//! ```
181//! # use hamcrest2::prelude::*;
182//! assert_that!(4, all!(lt(5), gt(3)));  // also and!()
183//! assert_that!(
184//!     &vec![1, 2, 3],
185//!     all!(contains(vec![1, 2]), not(contains(vec![4])))
186//! );
187//! ```
188//!
189//! ### any
190//!
191//! ```
192//! # use hamcrest2::prelude::*;
193//! assert_that!(4, any!(less_than(2), greater_than(3)));  // also or!()
194//! assert_that!(
195//!     &vec![1, 2, 3],
196//!     any!(contains(vec![1, 2, 5]), not(contains(vec![4])))
197//! );
198//! ```
199//!
200//! ## Misc Matchers
201//!
202//! ### is(bool)
203//!
204//! ```
205//! # use hamcrest2::prelude::*;
206//! assert_that!(true, is(true));
207//! assert_that!(false, is(false));
208//! ```
209//!
210//! ### anything
211//!
212//! ```
213//! # use hamcrest2::prelude::*;
214//! assert_that!(42, anything());
215//! assert_that!("test", is(anything()));
216//! ```
217
218extern crate num;
219extern crate regex;
220
221pub use crate::prelude::*;
222
223#[macro_export]
224macro_rules! assert_that {
225  ($actual:expr, $matcher:expr) => {{
226    // The separate statement is necessary to keep the compiler happy.
227    let m = $matcher;
228    match m.matches($actual) {
229      Ok(_) => {}
230      Err(mismatch) => {
231        // The panic macro produces the correct file and line number
232        // when used in a macro like this, i.e. it's the line where
233        // the macro was originally written.
234        panic!("\nExpected: {}\n    but: {}", m, mismatch);
235      }
236    }
237  }};
238}
239
240pub mod core;
241pub mod matchers;
242mod utils;
243pub mod prelude {
244  pub use crate::all;
245  #[deprecated(since = "0.2.0", note = "Use all() instead")]
246  pub use crate::all as all_of;
247  pub use crate::any;
248  #[deprecated(since = "0.2.0", note = "Use any() instead")]
249  pub use crate::any as any_of;
250  #[deprecated(since = "0.2.0", note = "Use any() instead")]
251  pub use crate::any as or;
252  pub use crate::assert_that;
253  #[allow(deprecated)]
254  pub use crate::core::assert_that as assert_that_fn;
255  pub use crate::core::Matcher as HamcrestMatcher;
256  pub use crate::matchers::anything::anything;
257  pub use crate::matchers::close_to::close_to;
258  pub use crate::matchers::compared_to::greater_than;
259  pub use crate::matchers::compared_to::greater_than as gt;
260  pub use crate::matchers::compared_to::greater_than_or_equal_to;
261  pub use crate::matchers::compared_to::greater_than_or_equal_to as geq;
262  pub use crate::matchers::compared_to::less_than;
263  pub use crate::matchers::compared_to::less_than as lt;
264  pub use crate::matchers::compared_to::less_than_or_equal_to;
265  pub use crate::matchers::compared_to::less_than_or_equal_to as leq;
266  pub use crate::matchers::contains::contains;
267  pub use crate::matchers::empty::empty;
268  pub use crate::matchers::equal_to::equal_to;
269  pub use crate::matchers::equal_to::equal_to as eq;
270  pub use crate::matchers::err::err;
271  pub use crate::matchers::has::has;
272  pub use crate::matchers::is::is;
273  pub use crate::matchers::is::is_not as does_not;
274  pub use crate::matchers::is::is_not as not;
275  pub use crate::matchers::is::is_not;
276  pub use crate::matchers::len::len;
277  #[deprecated(since = "0.2.0", note = "Use len() instead")]
278  pub use crate::matchers::len::len as of_len;
279  pub use crate::matchers::none::none;
280  pub use crate::matchers::ok::ok;
281  pub use crate::matchers::path_exists::dir_exists;
282  #[deprecated(since = "0.2.0", note = "Use dir_exists() instead")]
283  pub use crate::matchers::path_exists::dir_exists as existing_dir;
284  pub use crate::matchers::path_exists::file_exists;
285  #[deprecated(since = "0.2.0", note = "Use file_exists() instead")]
286  pub use crate::matchers::path_exists::file_exists as existing_file;
287  pub use crate::matchers::path_exists::path_exists;
288  #[deprecated(since = "0.2.0", note = "Use path_exists() instead")]
289  pub use crate::matchers::path_exists::path_exists as existing_path;
290  pub use crate::matchers::regex::matches_regex as match_regex;
291  pub use crate::matchers::regex::matches_regex;
292  pub use crate::matchers::some::some;
293  pub use crate::matchers::type_of::type_of;
294}