#assertions #fluent #assert #human-readable

smoothy

Write smooth assertions in a fluent and human readable way

10 unstable releases (3 breaking)

0.4.3 Mar 3, 2024
0.4.2 Mar 1, 2024
0.4.1 Jan 9, 2024
0.3.3 Jan 2, 2024
0.1.0 Nov 14, 2023

#176 in Testing

Download history 14/week @ 2023-12-30 25/week @ 2024-01-06 3/week @ 2024-01-13 45/week @ 2024-01-20 100/week @ 2024-01-27 22/week @ 2024-02-03 39/week @ 2024-02-10 184/week @ 2024-02-17 72/week @ 2024-02-24 258/week @ 2024-03-02 78/week @ 2024-03-09 128/week @ 2024-03-16 9/week @ 2024-03-30

150 downloads per month
Used in datadog-formatting-layer

MIT license

38KB
440 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(String::from("Hello")).equals("Hello");
use smoothy::assert_that;

assert_that("Hello World").contains("Hello").and().contains("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_equqls
        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 --> ImplAsRefStr
    state "Assertable implements AsRef&ltstr&gt" as ImplAsRefStr {
        [*] --> AssertionConnector&ltAsRef&ltstr&gt&gt : contains
        [*] --> 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
~55K SLoC