Expand description
Write smooth assertions in a fluent and human readable way.
Examples
All asserted are stared by calling assert_that on a value.
After that various assertions based on the type of the asserted value can be made.
Basic value assertions
There are several assertions available for all types:
Equality
There are two ways to assert equality:
iscompares the value with something of the same type.equalscompares the value with something that can be converted into the same type. This is done by using the Into trait.
assert_that(1).equals(1);
assert_that(String::from("Hello")).equals("Hello");assert_that(1).not_equals(2);
assert_that(String::from("Hello")).not_equals("Hello There");Same for try_into_equals and try_into_not_equals but here the trait TryInto is used.
assert_that(1u8).try_into_equals(1i8);assert_that(1u8).try_into_not_equals(2i8);When one wants to asserts a value while assuring the same type without any conversions is used is] can be used.
assert_that(1).is(1);assert_that(1).is_not(2);Results
Results can be asserted by calling is_err or is_ok.
Furthermore their actual content can be asserted as well.
Ok
Basic assertion that the result is Ok:
let result: Result<u8, ()> = Ok(1);
assert_that(result).is_ok();Asserting the Ok-value:
let result: Result<u8, ()> = Ok(1);
assert_that(result).is_ok().and_value().equals(1);Err
Basic assertion that the result is Err:
let result: Result<(), String> = Err(String::from("Oh no!"));
assert_that(result).is_err();When the Err-value implements PartialEq one can use and_error_equals
#[derive(Debug, PartialEq)]
struct CustomError(String);
let result: Result<(), CustomError> = Err(CustomError(String::from("Oh no!")));
assert_that(result).is_err().and_error().equals(CustomError(String::from("Oh no!")));Alternatively one can assert the error message (given the error implements Display):
#[derive(Debug)]
struct CustomError(String);
let result: Result<(), CustomError> = Err(CustomError(String::from("Oh no!")));
assert_that(result)
.is_err()
.and_error()
.to_string()
.equals("Oh no!");Option
Options can be asserted by calling is_none or is_some.
Instances of Some can be further asserted with and_value.
None
let option: Option<()> = None;
assert_that(option).is_none();Some
let option: Option<u8> = Some(1);
let asserter: BasicAsserter<u8> = assert_that(option).is_some().and_value();
// further assertions
asserter.equals(1);Structs
- Main struct with various assertions on
AssertedType - Enables various assertions on Err-values
- Enables various assertions on Ok-values
- Enables various assertions on Some-values
Functions
- Entrypoint for all assertions