#assertions #fluent #assert #human-readable

smoothy

Write smooth assertions in a fluent and human readable way

15 releases

0.5.2 Nov 27, 2024
0.4.5 Aug 25, 2024
0.4.4 May 15, 2024
0.4.3 Mar 3, 2024
0.3.0 Nov 27, 2023

#198 in Testing

Download history 8/week @ 2024-08-17 301/week @ 2024-08-24 297/week @ 2024-08-31 9/week @ 2024-09-07 51/week @ 2024-09-14 9/week @ 2024-09-21 41/week @ 2024-09-28 5/week @ 2024-10-05 3/week @ 2024-10-26 16/week @ 2024-11-09 9/week @ 2024-11-16 158/week @ 2024-11-23 46/week @ 2024-11-30

229 downloads per month
Used in datadog-formatting-layer

MIT license

47KB
467 lines

Smoothy

Write smooth assertions in a fluent and readable way.

check test License Crates.io

Features

The crate is heavily inspired by AssertJ

  • simple and readable syntax
  • assertions based on the type of the asserted value
  • assertion values use type conversion traits to make assertions readable

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.

use smoothy::assert_that;

assert_that(42).equals(42);
use smoothy::assert_that;

assert_that(1u8).try_into_equals(1i8);
use smoothy::assert_that;

assert_that(true).is_true();
use smoothy::assert_that;

assert_that(String::from("Hello")).equals("Hello");
use smoothy::assert_that;

assert_that("Hello World").contains_string("Hello").and().contains_string("World");
use smoothy::assert_that;

let result: Result<u8, String> = Ok(42);
assert_that(result)
    .is_ok()
    .and_value()
    .equals(42);
use smoothy::assert_that;

let result: Result<(), String> = Err(String::from("ups!"));
assert_that(result)
    .is_err()
    .and_error()
    .to_string()
    .equals("ups!");
use smoothy::assert_that;

let option: Option<()> = None;
assert_that(option).is_none();
use smoothy::assert_that;

let option: Option<u8> = Some(1);
assert_that(option)
    .is_some()
    .and_value()
    .equals(1);
use smoothy::assert_that;

let iterable: Vec<String> = vec![];
assert_that(iterable).is_empty();
use smoothy::assert_that;

assert_that(vec![1, 2, 3]).is_not_empty();
use smoothy::assert_that;

assert_that([1, 2, 3]).first().is(1);
use smoothy::assert_that;

assert_that([1, 2, 3]).nth(1).is(2);

Assertion Structure Diagram

stateDiagram-v2
    [*] --> BasicAsserter&ltAssertable&gt : assert_that
    BasicAsserter&ltAssertable&gt --> Anything
    state "Assertable is any type" as Anything {
        [*] --> AssertionConnector&ltAssertable&gt : is
        [*] --> AssertionConnector&ltAssertable&gt : is_not
        [*] --> AssertionConnector&ltAssertable&gt : equals
        [*] --> AssertionConnector&ltAssertable&gt : not_equals
        [*] --> AssertionConnector&ltAssertable&gt : try_into_equals
        [*] --> AssertionConnector&ltAssertable&gt : try_into_not_equals
        [*] --> AssertionConnector&ltAssertable&gt : map
        AssertionConnector&ltAssertable&gt --> [*] : and
    }
    BasicAsserter&ltAssertable&gt --> Result
    state "Assertable is Result&ltOk, Err&gt" as Result {
        [*] --> OkAsserter&ltOk&gt : is_ok
        OkAsserter&ltOk&gt --> BasicAsserter&ltOk&gt : and_value
        [*] --> ErrAsserter&ltErr&gt : is_err
        ErrAsserter&ltErr&gt --> BasicAsserter&ltErr&gt : and_error
    }
    BasicAsserter&ltAssertable&gt --> Option
    state "Assertable is Option&ltSome&gt" as Option {
        [*] --> [*] : is_none
        [*] --> SomeAsserter&ltSome&gt : is_some
        SomeAsserter&ltSome&gt --> BasicAsserter&ltSome&gt : and_value
    }
    BasicAsserter&ltAssertable&gt --> ImplString
    state "Assertable implements ToString" as ImplString {
        [*] --> BasicAsserter&ltString&gt : to_string
    }
    BasicAsserter&ltAssertable&gt --> ImplToBool
    state "Assertable implements Into<bool>" as ImplToBool {
        [*] --> BasicAsserter&ltbool&gt : is_true
        [*] --> BasicAsserter&ltbool&gt : is_false
    }
    BasicAsserter&ltAssertable&gt --> ImplAsRefStr
    state "Assertable implements AsRef&ltstr&gt" as ImplAsRefStr {
        [*] --> AssertionConnector&ltAsRef&ltstr&gt&gt : contains_string
        [*] --> AssertionConnector&ltAsRef&ltstr&gt&gt : starts_with_string
        [*] --> AssertionConnector&ltAsRef&ltstr&gt&gt : is_matching
        AssertionConnector&ltAsRef&ltstr&gt&gt --> [*] : and
    }
    BasicAsserter&ltAssertable&gt --> ImplIntoIter
    state "Assertable implements IntoIter&ltItem&gt" as ImplIntoIter {
        [*] --> [*] : is_empty
        [*] --> [*] : is_not_empty
        [*] --> BasicAsserter&ltItem&gt: first
        [*] --> BasicAsserter&ltItem&gt: second
        [*] --> BasicAsserter&ltItem&gt: third
        [*] --> BasicAsserter&ltItem&gt: nth
    }

Dependencies

~2.3–3.5MB
~56K SLoC