1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
//! This project is inspired by [Nimble](https://github.com/Quick/Nimble) for Swift. //! It provides matchers and matcher functions to express expectations in tests for common cases, //! such as: Equality, Order, Option, Result, etc. //! The crate also provides instruments to implement custom matchers for your project's domain. //! //! You express expectations in your tests using three basic constructs: //! //! ```rust,ignore //! expect!(...).to(...); //! expect!(...).to_not(...); //! expect!(...).not_to(...); //! ``` //! //! If a test fails, you will see the reason in a nice, human readable format. //! //! # Example //! //! ```rust,should_panic //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! let result = vec![1, 2, 2]; //! expect!(result).to(be_equal_to([1, 2, 3])); //! # } //! // -- //! // This will print: //! // expected to be equal to <[1, 2, 3]>, got <[1, 2, 2]> //! ``` //! //! # Structure //! //! The crate consists of two modules `core` and `matchers` but for general use //! you can access all you need using `prelude` module. The `expect!` macro saves //! file name and line number to print it later if an expectation fails. Internally the macro uses //! the `expect` function which is also available to you. //! //! # Usage //! //! The crate is meant to be used in tests, so we recommend you include it as a dev dependency //! with `#[cfg(test)]` attribute. //! //! In your Cargo.toml: //! //! ```toml //! [dev-dependencies] //! expectest = "0.9.1" //! ``` //! //! If you prefer nightly rust and want failure messages to be integrated in rust's //! standard panic message, enable `nightly` feature: //! //! ```toml //! [dev-dependencies] //! expectest = { version = "0.9.1", features = ["nightly"] } //! ``` //! //! In your crate root: //! //! ```rust,ignore //! #[cfg(test)] //! #[macro_use(expect)] //! extern crate expectest; //! ``` //! //! In your tests: //! //! ```rust,ignore //! use expectest::prelude::*; //! ``` //! # Matchers //! //! Keep in mind that some matchers work with types that implement `Debug` trait //! to print inner representation. You need to derive it for your types. //! //! ## Equality //! //! For types that implement `PartialEq` trait. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!("hello".to_string()).to(be_equal_to("hello")); //! # } //! ``` //! //! ## Equality of Floats //! //! With default `delta` equal to `0.001`. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!(12.001_f64).to(be_close_to(12.0)); //! expect!(12.1_f64).to(be_close_to(12.0).delta(0.1)); //! # } //! ``` //! //! ## Order //! //! For types that implement `PartialOrd` trait. //! //! Use any of the following matchers: //! //! - `be_less_than` //! - `be_less_or_equal_to` //! - `be_greater_than` //! - `be_greater_or_equal_to` //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!(1).to(be_greater_than(0)); //! # } //! ``` //! //! ## Option\<T\> //! //! Use `be_some` or `be_none` matchers. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!(Some(9)).to(be_some()); //! expect!(Some(9)).to(be_some().value(9)); //! # } //! ``` //! //! ## Result\<T, E\> //! //! Use `be_ok` or `be_err` matchers. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!("4".parse::<u32>()).to(be_ok()); //! expect!("4".parse::<u32>()).to(be_ok().value(4)); //! # } //! ``` //! //! ## Iterators //! //! For types that implement `Iterator + Clone` trait. //! //! Use `be_empty` or `have_count` matchers. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!("".chars()).to(be_empty()); //! expect!("abc".chars()).to(have_count(3)); //! # } //! ``` //! //! ## Boolean //! //! Use `be_true` or `be_false` matchers. //! //! ```rust //! # #[macro_use] extern crate expectest; //! # use expectest::prelude::*; //! # fn main() { //! expect!(9 == 9).to(be_true()); //! # } //! ``` //! //! That's all you need to know. //! //! **Happy Testing 😊** //! #![cfg_attr(feature = "nightly", feature(core_panic))] extern crate num_traits; #[cfg(feature="nightly")] extern crate core as rust_core; /// A macro intended to use instead of `expect` function. /// /// Provides a file name and a line number for a failed test case. #[macro_export] macro_rules! expect { ($e: expr) => ({ let location = $crate::core::SourceLocation::new(file!(), line!(), column!()); $crate::core::expect($e).location(location) }); } pub mod prelude { //! A module contains reexport of all useful functions. pub use core::expect; pub use matchers::{be_equal_to, be_less_than, be_less_or_equal_to, be_greater_than, be_greater_or_equal_to, be_true, be_false, be_some, be_none, be_ok, be_err, be_close_to, be_empty, have_count}; } pub mod core; pub mod matchers;