Assertables: Rust crate of macros assert, assume, assure

This assertables Rust crate provides macros assert…!, assume…!, assure…!, all for runtime reliability checking, and all described below. By SixArm.com.

Crate: https://crates.io/crates/assertables

Docs: https://docs.rs/assertables/

Repo: https://github.com/sixarm/assertables-rust-crate/

Contents:

Introduction

This Rust crate provides macros for Rust runtime checking:

Assert

Example: assert that x is less than y; return () or call panic!(message).

rust assert_lt!(1, 2); //-> ()

rust assert_lt!(2, 1); //-> panic!("assertion failed: `assert_lt(left, right)`\n left: `2`,\n right: `1`")

Assume

Example: assume that x is less than y; return Result with Ok(true) or Err(message).

rust assume_lt!(1, 2); //-> Ok(true)

rust assume_lt!(2, 1); //-> Err("assumption failed: `assume_lt(left, right)`\n left: `2`,\n right: `1`")

Assure

Example: assure that x is less than y; return Result with Ok(true) or Ok(false) or exceptional Err(message).

rust assure_lt!(1, 2); //-> Ok(true)

rust assure_lt!(2, 1); //-> Ok(false)

Macros

Macros for value checking

To compare values:

rust assert_lt!(1, 2); // check that 1 is less than 2

Macros for function checking

To compare function return values:

rust assert_fn_eq!\(i32::abs, 1, -1); // abs(1) == abs(-1)

To compare function Result Ok() values:

rust assert_fn_ok_eq!(i32::from_str, "1", "1"); // i32::from_str("1").unwrap() == i32::from_str("1").unwrap()

Test a function Result Err() strings:

rust assert_fn_err_string_eq!(i32::from_str, "foo", "goo"); // i32::from_str("foo").unwrap_err().to_string() == i32::from_str("goo").unwrap_err().to_string()

Two functions that are our favorites to use in our tests:

Macros for set checking

To compare sets, such as two arrays, where the item order does not matter, and the item count does not matter.

Examples:

rust assert_set_eq!([1, 2], [2, 1]); //-> ()

Macros for bag checking

Compare bags, such as two arrays, where the item order does not matter, and the item count does matter.

Examples:

rust assert_bag_eq!([1, 1, 2], [2, 1, 1]); //-> ()

Macros for IO-related checking

These macros help with IO-related checking, such as comparison of files, streams, etc. These macros return a Result with Ok(true) or Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, message)).

Examples:

rust assert_io_lt!(1, 2); //-> ()

rust assert_io_lt!(2, 1); //-> Err( // std::io::Error::new( // std::io::ErrorKind::InvalidInput, // "assumption failed: `assume_io_lt(left, right)`\n left: `2`\n right: `1`")] // ) // )

Extras

Custom error messages

The macros have a second form where a custom error message can be provided.

Comparison abbreviations

The comparison macros use abbreviations such as eq (equals), ne (not equals), lt (less than), le (less than or equal to), gt (greater than), ge (greater than or equals).

Complete list of macros

assert…

assert_fn…

assertfnok…

assertfnerr_string…

assert_set…

assert_bag…

assert_io…

assume…

assume_fn…

assumefnok…

assumefnerr_string…

assume_set…

assume_bag…

assume_io…

assure…

assure_fn…

assurefnok…

assurefnerr_string…

assure_set…

assure_bag…

assure_io…