| 1 | # Expecting |
| 2 | |
| 3 | [](https://crates.io/crates/expecting) |
| 4 | [](https://docs.rs/expecting) |
| 5 | [](LICENSE) |
| 6 | |
| 7 | **Expecting** provides macros for testing conditions without panicking. The |
| 8 | `expect_*` family of macros cause an early return of `anyhow::Error` if the |
| 9 | expected condition is not met. |
| 10 | |
| 11 | | Macro | Description | |
| 12 | |---------------------------------------|--------------------------------------------------------| |
| 13 | | `expect!(condition)` | Expects `condition` to be `true` | |
| 14 | | `expect_eq!(a, b)` | Expects `a == b` | |
| 15 | | `expect_ne!(a, b)` | Expects `a != b` | |
| 16 | | `expect_some(option)` | Expects `option` to be `Some(x)` and returns `x` | |
| 17 | | `expect_some_eq(some_a, a)` | Expects `some_a == Some(a)` | |
| 18 | | `expect_none(option)` | Expects `option` to be `None` | |
| 19 | | `expect_ok(result)` | Expects `result` to be `Ok(x)` and returns `x` | |
| 20 | | `expect_err(result)` | Expects `result` to be `Err(e)` and returns `e` | |
| 21 | | `expect_contains(string, substr)` | Expects `string` to contain `substr` | |
| 22 | | `expect_contains(container, element)` | Expects `container` (e.g., `Vec`) to contain `element` | |
| 23 | | `expect_empty(container)` | Expects `container` to have no elements. | |
| 24 | | `expect_empty(string)` | Expects `string` to have a length of zero. | |
| 25 | | `expect_not_empty(container)` | Expects `container` to have 1+ elements. | |
| 26 | | `expect_not_empty(string)` | Expects `string` to have non-zero length. | |
| 27 | |
| 28 | This crate is especially helpful in async integration tests that involve |
| 29 | provisioning and tearing down resources; rather than struggle to catch panics, |
| 30 | you can simply use `expect_*` instead of `assert_*` to return `Result`. |
| 31 | |
| 32 | ## Examples |
| 33 | |
| 34 | ```rust |
| 35 | use expecting::expect_eq; |
| 36 | |
| 37 | #[test] |
| 38 | fn passing_test() -> Result<()> { |
| 39 | expect_eq!(1, 1); |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn failing_test() -> Result<()> { |
| 45 | expect_eq!(1, 2); // returns early |
| 46 | Ok(()) // won't be reached |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | The error message for a failed test includes the line number and detailed |
| 51 | description of what went wrong. |
| 52 | |
| 53 |  |
| 54 | |
| 55 |  |
| 56 | |
| 57 | See [the docs](https://docs.rs/expecting) for usage examples and more info. |