Crate fluid[][src]

Fluid

Summary

fluid is an human readable test library.

The current goals of this crate are:

  • Easily readable tests: they should be read like english sentences.
  • Nice and understandable error messages.
  • Provide the most possible useful assertions for common cases: numbers, Iterators, Options, Results, etc.

How to use it

Add the crate in your Cargo.toml:

# Do not use in crate build, only in test
[dev-dependencies]
fluid = "0.2"

Reference the crate in your main file:

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

Import the crate content in scope of your tests file:

use fluid::*;

Do not forget that everything related to this crate must be used only within a test config (i.e., with a #[cfg(test)] attribute). Otherwise, you try to mix your library sources with your tests.

Examples

Simple equality

theory!(1 + 1).should().be_equal_to(2);

Negation

theory!(1 + 1).should().not().be_equal_to(10);

Explanation

theory!(1 + 1).should().be_equal_to(2)
    .because("this is basic arithmetic");

Nice error message

let the_answer: Result<i32, _> = Err("Oops, error!");
 
theory!(the_answer).should().not().be_an_error()
    .and_should().contain(42)
    .because("we need the answer");

Displays:

The test failed at src/tests/message.rs:16:
    the_answer is Err("Oops, error!")
    but it should not be an error.
 
    the_answer does not contain 42: Err("Oops, error!")
    but it should.
This test should pass because we need the answer.

Floats precision

theory!(1.).should().be_equal_to(1.01).with_precision(0.1);

Result::Err

let parse_error = match "?".parse::<i32>() {
    Ok(_) => unreachable!(),
    Err(e) => e,
};
let result = "two".parse::<i32>();
theory!(result).should().be_an_error()
    .and_should().be_this_error(parse_error);

Iterators

fn error(e: bool) -> Result<i32, i32> {
    match e {
        true => Result::Err(0),
        false => Result::Ok(0),
    }
}
 
theory!(error(false)).should().contain(0);
theory!(error(true)).should().not().contain(0);
theory!(&[1, 2, 3]).should().not().contain(&0);

Macros

theory

Structs

LeftElement

The left element of the assertion.

Theory

Main data structure used to create assertions: see its methods.

Traits

Not

The unary logical negation operator !.