Assertables is a Rust crate that 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: [email protected]
The Assertables Rust crate provides many assert macros that can help you develop, test, and debug.
- Test values with assert_lt, assert_gt, etc.
- Test strings with assert_starts_with, assert_ends_with, etc.
- Test groups with assert_all, assert_any, etc.
There are many more for results, options, polls, matches, iterators, sets, files, bytes, commands, etc.
To use this crate, add it to your file Cargo.toml
:
assertables = "9.1.0"
Top 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 features:
- Easy to use: everything is well-documented with runnable examples.
- Zero overhead: if you don't use a macro, then it's not compiled.
- Multiple forms: for debugging, for results, and for success returns.
Help:
- Documentation
- Frequently asked questions
- Examples
- Upgrade from version 8 to 9
- Comparisons to more_asserts, cool_asserts, assert2, claims, etc.
Values:
assert_eq!(a, b)
// a == b
assert_ne!(a, b)
// a != b
assert_lt!(a, b)
// a < b
assert_le!(a, b)
// a <= b
assert_gt!(a, b)
// a > b
assert_ge!(a, b)
// a >= b
Differences:
assert_approx_eq!(a, b)
// |a-b| <= 1e-6
assert_abs_diff_eq!(a, b, delta)
// |a-b| == Δ
assert_in_delta!(a, b, delta)
// |a-b| <= Δ
assert_in_epsilon!(a, b, epsilon)
// |a-b| <= ε min(a,b)
Groups for iterators, chars, etc.:
assert_all!(group, predicate)
// group.all(predicate)
assert_any!(group, predicate)
// group.any(predicate)
Infix for order operators, logic operators, etc.:
assert_infix!(a == b)
// order: == != < <= > >=
assert_infix!(a && b)
// logic: && || ^ & |
Lengths and counts for strings, vectors, iterators, etc.:
assert_len_eq!(a, b)
// a.len() == b.len()
assert_count_eq!(a, b)
// a.count() == b.count()
assert_is_empty!(a)
// a.is_empty()
Matching:
assert_starts_with!(whole, part)
// whole.starts_with(part)
assert_ends_with!(whole, part)
// whole.ends_with(part)
assert_contains!(container, x)
// container.contains(x)
assert_matches!(expr, pattern)
// matches!(expr, pattern)
assert_is_match!(matcher, x)
// matcher.is_match(x)
Results:
assert_ok!(a)
// a is Ok
assert_ok_eq_x!(a, x)
// a is Ok(x)
assert_err!(a)
// a is Err
Options:
assert_some!(a)
// a is Some
assert_some_eq_x!(a, x)
// a is Some(x)
assert_none!(a)
// a is None
Polls:
assert_ready!(a)
// a is Ready
assert_ready_eq_x!(a, x)
// a is Ready(x)
assert_pending!(a)
// a is Pending
Readers:
assert_fs_read_to_string_eq!(a_path, b_path)
// read a_path == read b_path
assert_io_read_to_string_eq!(a_bytes, b_bytes)
// read a_bytes == read b_bytes
Commands:
assert_command_stdout_eq!(a_command, b_command)
// a_command stdout == b_command stdout
assert_program_args_stderr_eq!(a_program, a_args, b_program, b_args)
// a_program a_args stderr == b_program b_args stderr
Collections:
assert_iter_eq!(arr1, arr2)
// a into iter == b into iter
assert_set_eq!(vec1, vec2)
// a into set == b into set
assert_bag_eq!(map1, map2)
// a into bag == b into bag
For a complete list of modules and macros, see the docs
All the macros have forms for an optional message:
assert_gt!(a, b)
// default message
assert_gt!(a, b, "your text")
// custom message
All the macros have forms for different outcomes:
assert_gt!(a, b)
// panic
assert_gt_as_result!(a, b)
// return Result, no panic
debug_assert_gt!(a, b)
// special use in debug mode
Many of the macros have a "solo" form for comparing one item to an expression, and a "pair" form for comparing two items to each other:
assert_ok_eq!(a, x)
// a.unwrap() == x
assert_ok_eq!(a, b)
// a.unwrap() == b.unwrap()
Many of the macros has a "success return", which means the macro returns data that you can optionally use for more testing.
let inner = assert_ok!(result)
let string = assert_fs_read_to_string_ne!("alfa.txt", "")
let stdout = assert_command_stdout_gt!("ls", vec![b' '])
- Package: assertables-rust-crate
- Version: 9.1.0
- Created: 2021-03-30T15:47:49Z
- Updated: 2024-10-29T20:21:37Z
- 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])