Crate assertables

source
Expand description

§Assertables: Rust crate of assert macros for testing

The assertables Rust crate provides many assert macros to improve your compile-time tests and run-time reliability.

§Introduction

The Rust programming language provides a few built-in assert macros to test code:

The Rust programming language provides assert macros to test code:

The assertables crate provides many more, to help you work with numbers, strings, results, options, polls, iterators, files, streams, commands, and more.

Examples:

use assertables::*;
let s = "hello world";
assert_matches!(s, "hello world");
assert_starts_with!(s, "hello");
assert_ends_with!(s, "world");
assert_contains!(s, "o");
assert_len_eq!(s, "***********");
assert_all!(s.chars(), |c: char| c < 'x');
assert_any!(s.chars(), |c: char| c.is_whitespace());

To use the macros, add this to your Cargo.toml file:

[dev-dependencies]
assertables = "*"

Top benefits:

  1. You can write better tests to improve reliability and maintainability.
  2. You can handle more corner cases without needing to write custom code.
  3. You can troubleshoot faster because error messages show specifics.

Top features:

  1. Easy to use: everything is well-documented with runnable examples.
  2. Zero overhead: if you don’t use a macro, then it’s never compiled.
  3. Runtime options: all the assertables macros have runtime versions.

Top comparison crates: [assert_matches](https://crates.io/crates/assert_matches), [assert_approx_eq](https://crates.io/crates/assert_approx_eq), [more_asserts](https://crates.io/crates/more_asserts), [cool_asserts](https://crates.io/crates/cool_asserts). [claims](https://crates.io/crates/claims).

§Highlights

Values:

Approximations:

Groups for iterators, chars, etc.:

Infix for order operators, logic operators, etc.:

Parts for strings, vectors, etc.:

Lengths for strings, vectors, etc.:

Matching for strings, regex, etc.:

Collections for arrays, vectors, iterators, sets, maps:

Result Ok/Err:

Option Some/None:

Poll Ready/Pending:

Read file system paths and input/output streams:

Run commands and programs then assert on stdout or stderr:

Function comparisons, which are especially good for refactoring:

§Forms

All assertables macros have forms for different outcomes:

  • [assert_gt!(a, b);](module@crate::macro.assert_gt.html) // panic during typical test
  • [assert_gt_as_result!(a, b);](module@crate::macro.assert_gt_as_result.html) // return Ok or Err
  • [debug_assert_gt!(a, b);](module@crate::macro.debug_assert_gt.html) // panic when in debug mode

All assertables macros have forms for an optional message:

  • [assert_gt!(a, b);](module@crate::macro.assert_gt) // automatic error message
  • [assert_gt!(a, b, "your text");](module@crate::macro.assert_gt) // custom error message

Many assertables macros have forms for comparing left hand side (LHS) and right hand side (RHS) as the same type or as an arbitrary expression:

  • [assert_ok_eq!(a, b);](module@crate::macro.assert_ok_eq.html) // Ok(…) = Ok(…)
  • [assert_ok_eq_expr!(a, b);](module@crate::macro.assert_ok_eq_expr.html) // Ok(…) = expression

§Forms

All assertables macros have forms for an optional message:

All assertables macros have forms for different outcomes:

Many assertables macros have forms for comparing left hand side (LHS) and right hand side (RHS) as the same type or as an arbitrary expression:

§Tracking

  • Package: assertables-rust-crate
  • Version: 8.17.0
  • Created: 2021-03-30T15:47:49Z
  • Updated: 2024-10-09T18:40:20Z
  • License: MIT or Apache-2.0 or GPL-2.0 or GPL-3.0 or contact us for more
  • Contact: Joel Parker Henderson (joel@sixarm.com)

Modules§

Macros§