Crate assertables[−][src]
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/assure
Docs: https://docs.rs/assure/
Repo: https://github.com/joelparkerhenderson/assurerustcrate/
Introduction
This Rust crate provides macros for Rust runtime checking, and each macro comes in three flavors:

assert…!
returns()
or callspanic!

assume…!
returnsResult
withOk(true)
orErr(…)

assure…!
returnsResult
withOk(true)
orOk(false)
orErr(…)
Examples of assert_lt!
:
assert_lt!(1, 2); //> ()
// assert_lt!(2, 1); //> panic!("assertion failed: `(left == right)`\n left: `2`,\n right: `1`")
Examples of assume_lt!
:
let x = assume_lt!(1, 2); //> Ok(true)
let x = assume_lt!(2, 1); //> Err("assumption failed: `(left == right)`\n left: `2`,\n right: `1`")
Examples of assure_lt!
:
let x = assure_lt!(1, 2); //> Ok(true)
let x = assure_lt!(2, 1); //> Ok(false)
Assert
The assert…
macros can be useful with Rust testing,
such as with macros that Rust std
does not provide.
Example:
fn sum_positive_numbers(a: i32, b: i32) > Result<i32, String> { assert_lt!(0, a); assert_lt!(0, b); Ok(a + b) } sum_positive_numbers(1, 2); //> 3 // sum_positive_numbers(1, 2); //> panic!("assertion failed: `(left == right)`\n left: `0`,\n right: `1`")
Assume
The assume…
macros can be useful with the ?
operator,
such as with early exits in functions.
Example:
fn sum_positive_numbers(a: i32, b: i32) > Result<i32, String> { assume_lt!(0, a)?; assume_lt!(0, b)?; Ok(a + b) } sum_positive_numbers(1, 2); //> Ok(3) sum_positive_numbers(1, 2); //> Err("assumption failed: `assume_lt(left, right)`\n left: `0`,\n right: `1`")
Assure
The assure…
macros can be useful with chaining,
such as with gate conditions in functions.
Example:
fn sum_positive_numbers(a: i32, b: i32) > Result<i32, String> { if assure_lt!(0, a).unwrap() && assure_lt!(0, b).unwrap() { Ok(a + b) } else { Err(format!("please use positive numbers")) } } sum_positive_numbers(1, 2); //> Ok(3) sum_positive_numbers(1, 2); //> Err("must use postive numbers")
Messages
When a macro fails, it generates a failure message with the values of expressions with their debug representations, such as:
// assert_lt!(2, 1) //> panic!("assertion failed: `(left == right)`\n left: `2`,\n right: `1`")
These macros have a second form where a custom message can be provided, such as:
// assert_lt!(2, 1, "my message here"); //> panic!("my message here")
Macros for values
Examples:
let x = assume_lt!(1, 2); //> Ok(true)
let x = assume_lt!(2, 1); //> Err("assumption failed: `assert_lt(left, right)`\n left: `2`\n right: `1`")
assert…
macros:

assert!
(a)
: assurea
is true, provided by Ruststd
. 
assert_eq!
(a, b)
: asserta
is equal tob
, provided by Ruststd
. 
assert_ne!
(a, b)
: asserta
is not equal tob
, provided by Ruststd
. 
assert_lt!
(a, b)
: asserta
is less thanb
. 
assert_le!
(a, b)
: asserta
is less than or equal tob
. 
assert_gt!
(a, b)
: asserta
is greater thanb
. 
assert_ge!
(a, b)
: asserta
is greater than or equal tob
.
assume…
macros:

assume!
(a)
: assumea
is true. 
assume_eq!
(a, b)
: assumea
is equal tob
. 
assume_ne!
(a, b)
: assumea
is not equal tob
. 
assume_lt!
(a, b)
: assumea
is less thanb
. 
assume_le!
(a, b)
: assumea
is less than or equal tob
. 
assume_gt!
(a, b)
: assumea
is greater thanb
. 
assume_ge!
(a, b)
: assumea
is greater than or equal tob
.
assure…
macros:

assure!
(a)
: assurea
is true. 
assure_eq!
(a, b)
: assurea
is equal tob
. 
assure_ne!
(a, b)
: assurea
is not equal tob
. 
assure_lt!
(a, b)
: assurea
is less thanb
. 
assure_le!
(a, b)
: assurea
is less than or equal tob
. 
assure_gt!
(a, b)
: assurea
is greater thanb
. 
assure_ge!
(a, b)
: assurea
is greater than or equal tob
.
Macros for set checking
These macros help with comparison of set parameters, such as two arrays or two vectors. where the item order does not matter, and the item count does not matter.
Examples:
let x = assume_set_eq!([1, 2], [2, 1]); //> Ok(true)
let x = assume_set_eq!([1, 2], [3, 4]); //> Err("assertion failed: `assert_set_eq(left, right)`\n left: `[1, 2]`\n right: `[3, 4]`")
assert_set…
macros:

assert_set_eq!
(a, b)
: assert the seta
is equal to the setb
. 
assert_set_ne!
(a, b)
: assert the seta
is not equal to the setb
.
assume_set…
macros:

assume_set_eq!
(a, b)
: assume the seta
is equal to the setb
. 
assume_set_ne!
(a, b)
: assume the seta
is not equal to the setb
.
assure_set…
macros:

assure_set_eq!
(a, b)
: assure the seta
is equal to the setb
. 
assure_set_ne!
(a, b)
: assure the seta
is not equal to the setb
.
Macros for bag checking
Thes macros help with comparison of bag parameters, such as comparison of two arrays or two vectors, where the item order does not matter, and the item count does matter.
Examples:
let x = assume_bag_eq!([1, 1], [1, 1]); //> Ok(true)
let x = assume_bag_eq!([1, 1], [1, 1, 1]); //> Err("assumption failed: `assume_bag_eq(left, right)`\n left: `[1, 1]`\n right: `[1, 1, 1]`")]
assert_bag…
macros:

[
assert_bag_eq
](a, b)
: assert the baga
is equal to the bagb
. 
[
assert_bag_ne
](a, b)
: assert the baga
is not equal to the bagb
.
assume_bag…
macros:

assume_bag_eq!
(a, b)
: assume the baga
is equal to the bagb
. 
assume_bag_ne!
(a, b)
: assume the baga
is not equal to the bagb
.
assure_bag…
macros:

assure_bag_eq!
(a, b)
: assure the baga
is equal to the bagb
. 
assure_bag_ne!
(a, b)
: assure the baga
is not equal to the bagb
.
Macros for IOrelated checking
These macros help with IOrelated 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:
let x = assume_io_lt!(1, 2); //> Ok(true)
let x = assume_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`")] // ) // )
assert_io…
macros:

[
assert_io!
](a)
: asserta
is true. 
assert_io_eq!
(a, b)
: asserta
is equal tob
. 
assert_io_ne!
(a, b)
: asserta
is not equal tob
. 
assert_io_lt!
(a, b)
: asserta
is less thanb
. 
assert_io_le!
(a, b)
: asserta
is less than or equal tob
. 
assert_io_gt!
(a, b)
: asserta
is greater thanb
. 
assert_io_ge!
(a, b)
: asserta
is greater than or equal tob
.
assume_io…
macros:

assume_io!
(a)
: assumea
is true. 
assume_io_eq!
(a, b)
: assumea
is equal tob
. 
assume_io_ne!
(a, b)
: assumea
is not equal tob
. 
assume_io_lt!
(a, b)
: assumea
is less thanb
. 
assume_io_le!
(a, b)
: assumea
is less than or equal tob
. 
assume_io_gt!
(a, b)
: assumea
is greater thanb
. 
assume_io_ge!
(a, b)
: assumea
is greater than or equal tob
.
assure_io…
macros:

assure_io!
(a)
: assurea
is true. 
assure_io_eq!
(a, b)
: assurea
is equal tob
. 
assure_io_ne!
(a, b)
: assurea
is not equal tob
. 
assure_io_lt!
(a, b)
: assurea
is less thanb
. 
assure_io_le!
(a, b)
: assurea
is less than or equal tob
. 
assure_io_gt!
(a, b)
: assurea
is greater thanb
. 
assure_io_ge!
(a, b)
: assurea
is greater than or equal tob
.
Modules
Macros
assert_bag_eq  Assert two bags are equal. 
assert_bag_ne  Assert two bags are not equal. 
assert_ge  Assert one value is greater than or equal to another value. 
assert_gt  Assert one value is greater than another value. 
assert_io_eq  Assert two values are equal. 
assert_io_ge  Assert one value is greater than or equal to another value. 
assert_io_gt  Assert one value is greater than another value. 
assert_io_le  Assert one value is less than or equal to another value. 
assert_io_lt  Assert one value is less than another value. 
assert_io_ne  Assert two values are equal. 
assert_le  Assert one value is less than or equal to another value. 
assert_lt  Assert one value is less than another value. 
assert_set_eq  Assert two sets are equal. 
assert_set_ne  Assert two sets are not equal. 
assume  Assume a condition is true. 
assume_bag_eq  Assume two bags are equal. 
assume_bag_ne  Assume two bags are not equal. 
assume_eq  Assume two values are equal. 
assume_ge  Assume one value is greater than or equal to another value. 
assume_gt  Assume one value is greater than another value. 
assume_io  Assume a condition is true. 
assume_io_eq  Assume one value is equal to another value. 
assume_io_ge  Assume one value is greater than or equal to another value. 
assume_io_gt  Assume one value is greater than another value. 
assume_io_le  Assume one value is less than or equal to another value. 
assume_io_lt  assume one value is less than another value. 
assume_io_ne  Assume one value is not equal to another value. 
assume_le  Assume one value is less than or equal to another value. 
assume_lt  Assume one value is less than another value. 
assume_ne  Assume two values are not equal. 
assume_set_eq  Assume two sets are equal. 
assume_set_ne  Assume two sets are not equal. 
assure  Assure a condition is true. 
assure_bag_eq  Assure two bags are equal. 
assure_bag_ne  Assure two bags are not equal. 
assure_eq  Assure two values are equal. 
assure_ge  Assure one value is greater than or equal to another value. 
assure_gt  Assure one value is greater than another value. 
assure_io  Assure a condition is true. 
assure_io_eq  Assure one value is equal to another value. 
assure_io_ge  Assure one value is greater than or equal to another value. 
assure_io_gt  Assure one value is greater than another value. 
assure_io_le  Assure one value is less than or equal to another value. 
assure_io_lt  Assure one value is less than another value. 
assure_io_ne  Assure one value is not equal to another value. 
assure_le  Assure one value is less than or equal to another value. 
assure_lt  Assure one value is less than another value. 
assure_ne  Assure two values are not equal. 
assure_set_eq  Assure two sets are equal. 
assure_set_ne  Assure two sets are not equal. 