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.

Examples:

All of the macros come in three flavors:

All of the macros have a second form where a custom error message can be provided, such as:

All of the orderable macros have these comparisons:

Assert

Example to assert that x is less than y:

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 to assume that x is less than y:

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 to assure that x is less than y:

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

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

Macros

Macros for value checking

To compare values:

rust assert_eq!(1, 1); // 1 == 1

Macros for function checking

To compare function return values:

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

To compare function result ok values:

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

Test a function result error strings:

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

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`")] // ) // )

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…