shoulda

Crates.ioshoulda
lib.rsshoulda
version0.2.1
sourcesrc
created_at2021-05-08 16:52:08.284905
updated_at2023-06-27 06:06:10.436319
descriptionderive macro for test assertions
homepagehttps://github.com/cramt/shoulda
repositoryhttps://github.com/cramt/shoulda
max_upload_size
id394923
size6,379
Alexandra Østermark (cramt)

documentation

https://docs.rs/shoulda_core/0.1.2/shoulda_core/index.html

README

Shoulda

Shoulda is a BDD/TDD assertion library that adds to rusts pretty rudimentary assertions.

Shoulda is based on heavily chai's should interface, as it makes you able to call should on almost any object and have a very readible chain of method calls.


shoulda = "0.2.0"

std examples

should can be called on most standard library types. However if there is an obvious type which is missing, please create an issue or pull request.

use shoulda::Shoulda;

#[test]
fn something_really_important() {
 let expected = String::from("thingy");
 expected.should().be().equal(format!("{}ingy", "th"));
}
#[test]
fn is_math_real() {
 let expected = 4;
 expected.should().be().equal(2 + 2);
}

for more std examples take a look at the tests within shoulda_core/src/tests.rs

derive examples

Shoulda provides a derive macro which allows you to automatically implement Shoulda on all your core struct you wanna compare without requiring PartialEq.

It does require Debug as a proper error message cant be generated without it

use shoulda::Shoulda;

#[derive(Debug, Shoulda)]
struct Person {
name: String,
       has_toothbrush: bool,
}

#[test]
fn test() {
 Person {
name: "Ronald".to_string(),
       has_toothbrush: false,
 }
 .should()
  .not()
  .be()
  .equal(Person {
name: "Ronald".to_string(),
has_toothbrush: true,
});
}

expect syntax

Shoulda also supports an expect alias instead of should

use shoulda::expect;

#[test]
fn test(){
 expect!(your_super_cool_thing).to();
}

is equivalent to

use shoulda::Shoulda;

#[test]
fn test(){
 your_super_cool_thing.should();
}

for more derive examples take a look at the tests within tests/src/

Float equality

For float equality this library checks wether or not the floats are within a specific threshold of eachother, that threshold i current defined as the envorimental variable SHOULDA_EPSILON or if its not defined its defaulted to 0.0001.

This also happens when struct which Shoulda is applied to through the derive macro contains floating point numbers.

Commit count: 48

cargo fmt