Crates.io | assertables |
lib.rs | assertables |
version | 9.5.0 |
source | src |
created_at | 2021-04-03 19:32:43.093705 |
updated_at | 2024-12-07 02:32:38.746858 |
description | Assertables: assert macros for better testing, debugging, quality assurance, and runtime reliability. |
homepage | |
repository | https://github.com/sixarm/assertables-rust-crate/ |
max_upload_size | |
id | 378431 |
size | 2,957,254 |
Assertables is a Rust crate of assert macros to improve you compile-time testing and run-time reliability.
The Assertables Rust crate provides many assert macros that can help you develop, test, and debug.
To use this crate, add it to your file Cargo.toml
:
assertables = "9.5.0"
Benefits:
Learning: FAQ, docs, examples, changes, upgrades, developing.
Comparisons: more_asserts, cool_asserts, assert2, claims, etc.
Examples with numbers:
let i = 10;
assert_lt!(i, 11);
assert_in_range!(i, 1..100);
assert_abs_diff_eq!(i, 12, 2);
Examples with strings:
let s = "hello";
assert_starts_with!(s, "h");
assert_contains!(s, "e");
assert_is_match!(Regex::new(r"h.*o").unwrap(), s);
Examples with arrays:
let a = [1, 2, 3];
assert_not_empty!(a);
assert_len_eq_x!(a, 3);
assert_all!(a.into_iter(), |i: i32| i < 4);
Values:
assert_eq!(a, b)
≈ a = b ≈ equal toassert_ne!(a, b)
≈ a ≠ b ≈ not equal toassert_lt!(a, b)
≈ a < b ≈ less thanassert_le!(a, b)
≈ a ≤ b ≈ less than or equal toassert_gt!(a, b)
≈ a > b ≈ greater thanassert_ge!(a, b)
≈ a ≥ b ≈ greater than or equal toApproximations:
assert_approx_eq!(a, b)
≈ |a-b| ≤ 1e-6assert_diff_eq_x!(a, b, x)
≈ (b-a) = xassert_abs_diff_eq_x!(a, b, x)
≈ |b-a| = xassert_in_delta!(a, b, delta)
≈ |a-b| ≤ Δassert_in_epsilon!(a, b, epsilon)
≈ |a-b| ≤ ε min(a,b)assert_in_range!(a, range)
≈ a ∈ rangeGroups:
assert_all!(group, predicate)
≈ group.all(predicate)assert_any!(group, predicate)
≈ group.any(predicate)assert_is_empty!(group)
≈ a.is_empty()assert_len_eq!(a, b)
≈ a.len() = b.len()assert_count_eq!(a, b)
≈ a.count() = b.count()Matching:
assert_starts_with!(sequence, x)
≈ sequence.starts_with(x)assert_ends_with!(sequence, x)
≈ sequence.ends_with(x)assert_contains!(container, x)
≈ container.contains(x)assert_is_match!(matcher, x)
≈ matcher.is_match(x)assert_matches!(expr, pattern)
≈ matches!(expr, pattern)Results:
assert_ok!(a)
≈ a is Okassert_err!(a)
≈ a is Errassert_ok_eq_x!(a, x)
≈ a is Ok ⇒ unwrap = xOptions:
assert_some!(a)
≈ a is Someassert_none!(a)
≈ a is Noneassert_some_eq_x!(a, x)
≈ a is Some ⇒ unwrap = xPolls:
assert_ready!(a)
≈ a is Readyassert_pending!(a)
≈ a is Pendingassert_ready_eq_x!(a, x)
≈ a is Ready ⇒ unwrap = xReaders:
assert_fs_read_to_string_eq_x!(path, x
≈ path ⇒ file ⇒ string = xassert_io_read_to_string_eq_x!(reader, x)
≈ reader ⇒ bytes ⇒ string = xCollections:
assert_iter_eq!(a, b)
≈ a into iter = b into iterassert_set_eq!(a, b)
≈ a into set = b into setassert_bag_eq!(a, b)
≈ a into bag = b into bagCommands:
assert_command_stdout_eq_x!(command, x)
≈ command ⇒ stdout == xassert_program_args_stdout_eq_x!(program, args, x)
≈ program.args ⇒ stdout = xStatus:
assert_status_success!(a)
≈ a.status().success()assert_status_code_value_eq_x!(a, x)
≈ a.status().code().unwrap() = xInfix:
assert_infix!(a == b)
≈ order operators == != < <= > >=assert_infix!(a && b)
≈ logic operators && || ^ & |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 messageassert_gt!(a, b, "your text")
≈ custom messageAll the macros have forms for different outcomes:
assert_gt!(1, 2)
≈ panicassert_gt_as_result!(1, 2)
≈ Result Errdebug_assert_gt!(a, b)
≈ panic in debug modeMany of the macros have a form "compare left item to right item" that compares items of the same kind, and a form "compare left item to right expression" that compares one item to any arbitrary expression:
assert_len_eq!(a, b)
≈ a.len() = b.len()assert_len_eq_x!(a, x)
≈ a.len() = xMany of the macros has a "success return", which means the macro returns data that you can optionally use for more testing.