assertables-rust-crate icon indicating copy to clipboard operation
assertables-rust-crate copied to clipboard

Assertables: a Rust crate of assert macros for testing

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 assert macros such as assert!(x) to test code. The assertables crate provides many more for numbers, strings, results, options, iterators, files, streams, and more. See below for examples.

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.

To use this crate, add it to your Cargo.toml file as a development dependency:

[dev-dependencies]
assertables = "*"

Comparable crates: assert_matches, assert_approx_eq, more_asserts, cool_asserts, assert2, claims, static_assertions.

Highlights

Values:

Approximations:

Groups for iterators, chars, etc.:

Infix for order operators, logic operators, etc.:

Parts for strings, vectors, etc.:

Lengths and counts for strings, vectors, iterators, 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 can be especially helpful for refactoring:

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 one item (to an expression) or two items (to each other):

Migrating from version 8 to version 9

A macro naming convention is changing, to improve usability.

Version 8 naming convention:

assert_foo_eq_expr!(a, x) // compare one item with an expression
assert_foo_eq!(a, b) // compare two items of the same type

Version 9 naming conventions:

assert_foo_eq!(a, x) // compare one item with an expression
assert_foo_eq2!(a, b) // compare two items of the same type

To update your code, one way is to use regular expressions.

Run this first:

  • Match: \b(|debug_)(assert_\w*_)(eq|ne|lt|le|gt|ge)(|_as_result)\b

  • Replace: $1$2$32$4

Run this second:

  • Match: \b(|debug_)(assert_\w*_)(eq|ne|lt|le|gt|ge)_expr(|_as_result)\b

  • Replace: $1$2$3$4

Tracking

  • Package: assertables-rust-crate
  • Version: 9.0.0
  • Created: 2021-03-30T15:47:49Z
  • Updated: 2024-10-19T21:00:54Z
  • License: MIT or Apache-2.0 or GPL-2.0 or GPL-3.0 or contact us for more
  • Contact: Joel Parker Henderson ([email protected])