Write unit tests like humans
cargo install morq
Morq Crate: https://crates.io/crates/morq
Since we are using a macro here, you need to wrap the following rules in morq!
macro.
Example:
morq!(
expect(3).to.be.an(i32);
);
You use following chains to make the assertions more user friendly and readable.
- to
- be
- have
expect(30).to.be.equal(10 * 3);
expect(3).to.be.equal(1 + 2);
To compare two given float
values
expect(3f32).to.be.close(3.0001f32);
expect(3f32).to.be.close_to(3.0001f32);
Negates the chain.
expect(30).to.not.be.equal(10);
expect(3).to.not.be.equal(1);
expect(vec![1, 2, 3]).to.not.be.a(Vec<char>);
To check the data type.
expect(30).to.be.an(i32);
expect("hola".to_string()).to.not.be.a(f32);
expect(vec![1, 2, 3]).to.be.a(Vec<i32>);
To check and see if the iterator is empty or not
expect(vec![1, 2, 3]).to.not.be.empty();
expect(0..2).to.not.be.empty();
To check the count of elements in an iterator
expect(vec![1, 2, 3]).to.not.have.length_of(1usize);
expect(0..3).to.have.length_of(3usize);
Given iterator must contain the element
expect(vec![1, 2, 3]).to.contain(2);
expect(vec![false, false]).to.not.contain(true);
To check a Result enum
let res: Result<String, String> = Ok(format!("boo"));
morq!(
expect(res).to.be.ok();
);
let res: Result<String, String> = Err(format!("boo"));
morq!(
expect(res).to.be.err();
);
Of course, you can combine it with not
:
let res: Result<String, String> = Err(format!("boo"));
morq!(
expect(res).to.not.be.ok();
);
-
Adding more chain rules
-
Adding more assert (terminal)
-
Ability to add two or more asserts in one chain:
expect("hello").to.be.equal("hello").and.not.be.a(i32);
Means chicken in Farsi. Like a lazy chicken, you know.
Artwork: clipart-library.com
Afshin Mehrabani
MIT
Inspired by http://chaijs.com and https://github.com/carllerche/hamcrest-rust