The assertables
Rust crate provides many assert macros to improve your
compile-time tests and run-time reliability.
- Crate: https://crates.io/crates/assertables
- Docs: https://docs.rs/assertables/
- Repo: https://github.com/sixarm/assertables-rust-crate/
- Contact: joel@joelparkerhenderson.com
The Rust programming language provides assert macros to test code:
assert!(…)
assert_eq!(a, b)
// equal
assert_ne!(a, b)
// not equal
The assertables crate provides many more, so you can write smarter tests.
Examples:
assert_lt!(1, 2)
// compare values using less than
assert_approx_eq!(1.0000001, 1.0000002)
// compare floats
assert_len_eq!("hello", "world")
// compare lengths
assert_starts_with!("hello world", "hello")
// compare strings
assert_fs_read_to_string_eq!("a.txt", "b.txt")
// compare files
Top 3 benefits:
- You can write better tests to improve reliability and maintainability.
- You can handle more corner cases without needing to write custom code.
- You can troubleshoot faster because error messages show specifics.
Top 3 features:
- Easy to use: everything is well-documented with runnable examples.
- Runtime savvy: all the assertables macros have runtime versions.
- Zero overhead: if you don't use a macro, then it's never compiled.
To use this crate, add it to your Cargo.toml
file:
[dev-dependencies]
assertables = "8.14.0"
For values:
assert_lt!(a, b)
// less than
assert_le!(a, b)
// less than
or equal toassert_gt!(a, b)
// greater than
assert_ge!(a, b)
// greater than
or equal to
For approximation:
For strings:
For lengths:
For matching:
For infix operators:
For nearness:
For Result Ok/Err:
For Option Some/None:
For Poll Ready/Pending:
For collections such as arrays, vectors, iterators, sets:
For file system paths and input/output readers:
For command capture of standard output and standard error:
assert_command_stdout_eq!(command1, command2)
assert_program_args_stdout_eq!(program1, args1, program2, args2)
There are many more macros that are organized in modules.
Modules for values:
Modules for strings:
Modules for lengths:
Modules for matching:
Modules for collections such as arrays, vectors, iterators, sets, bags:
assert_iter
for iterator collectionsassert_set
for set collectionsassert_bag
for bag collections
Modules for Result Ok/Err:
assert_ok
assert_err
Modules for Option Some/None:
assert_some
assert_none
Modules for Poll Ready/Pending:
assert_ready
assert_pending
Modules for functions:
assert_fn
for functions in general.assert_fn_ok
for functions that returnResult::Ok
.assert_fn_err
for functions that returnResult::Err
.
Modules for readers:
assert_fs_read_to_string
for file system path contents.assert_io_read_to_string
for input/output reader streams.
Modules for external calls:
assert_command
for commands with stdout/stderr.assert_program_args
for programs with args with stdout/stderr.
All assertables macros have forms for different outcomes:
assert_gt!(a, b)
// panic during typical test
assert_gt_as_result!(a, b)
// return Ok or Err
debug_assert_gt!(a, b)
// panic when in debug mode
All assertables macros have forms for an optional message:
assert_gt!(a, b)
// automatic error message
assert_gt!(a, b, "your text")
// 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 expression:
assert_ok_eq!(a, b)
// Ok(…) = Ok(…)
assert_ok_eq_expr!(a, b)
// Ok(…) = expression
- Package: assertables-rust-crate
- Version: 8.15.0
- Created: 2021-03-30T15:47:49Z
- Updated: 2024-10-06T23:11:30Z
- 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)