assertables/lib.rs
1//! # Assertables: Rust crate of assert macros for testing
2//!
3//! Assertables is a Rust crate that provides many assert macros
4//! to improve your compile-time tests and run-time reliability.
5//!
6//! * Crate: [https://crates.io/crates/assertables](https://crates.io/crates/assertables)
7//! * Docs: [https://docs.rs/assertables/](https://docs.rs/assertables/)
8//! * Repo: [https://github.com/sixarm/assertables-rust-crate/](https://github.com/sixarm/assertables-rust-crate/)
9//! * Contact: [joel@joelparkerhenderson.com](mailto:joel@joelparkerhenderson.com)
10//!
11//!
12//! ## Introduction
13//!
14//! The Assertables Rust crate provides many assert macros
15//! that can help you develop, test, and debug.
16//!
17//! * Test values with
18//! [assert_lt](module@crate::assert_lt),
19//! [assert_gt](module@crate::assert_gt),
20//! [assert_in](module@crate::assert_in),
21//! […](https://docs.rs/assertables)
22//! * Test groups with
23//! [assert_all](module@crate::assert_all),
24//! [assert_any](module@crate::assert_any),
25//! [assert_iter](module@crate::assert_iter),
26//! […](https://docs.rs/assertables)
27//! * Test wrappers with
28//! [assert_ok](module@crate::assert_ok),
29//! [assert_some](module@crate::assert_some),
30//! [assert_ready](module@crate::assert_ready),
31//! […](https://docs.rs/assertables)
32//! * Test matching with
33//! [assert_matches](module@crate::assert_matches),
34//! [assert_is_match](module@crate::assert_is_match),
35//! […](https://docs.rs/assertables)
36//! * Test nearness with
37//! [assert_approx](module@crate::assert_approx),
38//! [assert_abs_diff](module@crate::assert_abs_diff),
39//! […](https://docs.rs/assertables/)
40//! * Test programs with
41//! [assert_command](module@crate::assert_command),
42//! [assert_status](module@crate::assert_status),
43//! […](https://docs.rs/assertables)
44//! * Many more below.
45//!
46//! To use this crate, add it to your file `Cargo.toml`:
47//!
48//! ```toml
49//! assertables = "9.5.0"
50//! ``````
51//!
52//! Benefits:
53//!
54//! * You will write better tests to improve reliability and maintainability.
55//! * You will handle more corner cases without needing to write custom code.
56//! * You will troubleshoot faster because error messages show more detail.
57//!
58//! Learning:
59//! [FAQ](https://github.com/SixArm/assertables-rust-crate/tree/main/help/faq),
60//! [docs](https://docs.rs/assertables/),
61//! [examples](https://github.com/SixArm/assertables-rust-crate/blob/main/tests/examples/),
62//! [changes](https://github.com/SixArm/assertables-rust-crate/tree/main/CHANGES.md),
63//! [upgrades](https://github.com/SixArm/assertables-rust-crate/tree/main/help/upgrades/upgrade-from-version-8-to-9),
64//! [developing](https://github.com/SixArm/assertables-rust-crate/tree/main/help/developing/).
65//!
66//! Comparisons:
67//! [more_asserts](https://github.com/SixArm/assertables-rust-crate/tree/main/help/comparisons/more_asserts),
68//! [cool_asserts](https://github.com/SixArm/assertables-rust-crate/tree/main/help/comparisons/cool_asserts),
69//! [assert2](https://github.com/SixArm/assertables-rust-crate/tree/main/help/comparisons/assert2),
70//! [claims](https://github.com/SixArm/assertables-rust-crate/tree/main/help/comparisons/claims),
71//! [etc.](https://github.com/SixArm/assertables-rust-crate/tree/main/help/comparisons)
72//!
73//! ## Examples
74//!
75//! Examples with numbers:
76//!
77//! ```rust
78//! # use assertables::*;
79//! let i = 1;
80//! assert_lt!(i, 5);
81//! assert_in_range!(i, 1..5);
82//! assert_abs_diff_eq!(i, 5, 4);
83//! ```
84//!
85//! Examples with strings:
86//!
87//! ```rust
88//! # use assertables::*;
89//! # use regex::Regex;
90//! let s = "hello";
91//! assert_starts_with!(s, "h");
92//! assert_contains!(s, "e");
93//! assert_is_match!(Regex::new(r"h.*o").unwrap(), s);
94//! ```
95//!
96//! Examples with arrays:
97//!
98//! ```rust
99//! # use assertables::*;
100//! let a = [1, 2, 3];
101//! assert_not_empty!(a);
102//! assert_len_eq_x!(a, 3);
103//! assert_all!(a.into_iter(), |i: i32| i < 4);
104//! ```
105//!
106//! ## Highlights
107//!
108//! Values:
109//!
110//! * [`assert_eq!(a, b)`](module@crate::assert_eq) ≈ a = b ≈ equal to
111//! * [`assert_ne!(a, b)`](module@crate::assert_ne) ≈ a ≠ b ≈ not equal to
112//! * [`assert_lt!(a, b)`](module@crate::assert_lt) ≈ a < b ≈ less than
113//! * [`assert_le!(a, b)`](module@crate::assert_le) ≈ a ≤ b ≈ less than or equal to
114//! * [`assert_gt!(a, b)`](module@crate::assert_gt) ≈ a > b ≈ greater than
115//! * [`assert_ge!(a, b)`](module@crate::assert_ge) ≈ a ≥ b ≈ greater than or equal to
116//!
117//! Nearness:
118//!
119//! * [`assert_approx_eq!(a, b)`](module@crate::assert_approx::assert_approx_eq) ≈ |a-b| ≤ 1e-6
120//! * [`assert_diff_eq_x!(a, b, x)`](module@crate::assert_diff::assert_diff_eq_x) ≈ (b-a) = x
121//! * [`assert_abs_diff_eq_x!(a, b, x)`](module@crate::assert_abs_diff::assert_abs_diff_eq_x) ≈ |b-a| = x
122//! * [`assert_in_delta!(a, b, delta)`](module@crate::assert_in::assert_in_delta) ≈ |a-b| ≤ Δ
123//! * [`assert_in_epsilon!(a, b, epsilon)`](module@crate::assert_in::assert_in_epsilon) ≈ |a-b| ≤ ε min(a,b)
124//! * [`assert_in_range!(a, range)`](module@crate::assert_in::assert_in_range) ≈ range.contains(a)
125//!
126//! Groups:
127//!
128//! * [`assert_all!(group, predicate)`](module@crate::assert_all) ≈ group.all(predicate)
129//! * [`assert_any!(group, predicate)`](module@crate::assert_any) ≈ group.any(predicate)
130//! * [`assert_is_empty!(group)`](module@crate::assert_is_empty::assert_is_empty) ≈ a.is_empty()
131//! * [`assert_len_eq!(a, b)`](module@crate::assert_len::assert_len_eq) ≈ a.len() = b.len()
132//! * [`assert_count_eq!(a, b)`](module@crate::assert_count::assert_count_eq) ≈ a.count() = b.count()
133//!
134//! Matching:
135//!
136//! * [`assert_starts_with!(sequence, x)`](module@crate::assert_starts_with) ≈ sequence.starts_with(x)
137//! * [`assert_ends_with!(sequence, x)`](module@crate::assert_ends_with) ≈ sequence.ends_with(x)
138//! * [`assert_contains!(container, x)`](module@crate::assert_contains) ≈ container.contains(x)
139//! * [`assert_is_match!(matcher, x)`](module@crate::assert_is_match) ≈ matcher.is_match(x)
140//! * [`assert_matches!(expr, pattern)`](module@crate::assert_matches) ≈ matches!(expr, pattern)
141//!
142//! Results:
143//!
144//! * [`assert_ok!(a)`](module@crate::assert_ok) ≈ a is Ok
145//! * [`assert_err!(a)`](module@crate::assert_err) ≈ a is Err
146//! * [`assert_ok_eq_x!(a, x)`](module@crate::assert_ok::assert_ok_eq_x) ≈ a is Ok ⇒ unwrap = x
147//!
148//! Options:
149//!
150//! * [`assert_some!(a)`](module@crate::assert_some) ≈ a is Some
151//! * [`assert_none!(a)`](module@crate::assert_none) ≈ a is None
152//! * [`assert_some_eq_x!(a, x)`](module@crate::assert_some::assert_some_eq_x) ≈ a is Some ⇒ unwrap = x
153//!
154//! Polls:
155//!
156//! * [`assert_ready!(a)`](module@crate::assert_ready) ≈ a is Ready
157//! * [`assert_pending!(a)`](module@crate::assert_pending) ≈ a is Pending
158//! * [`assert_ready_eq_x!(a, x)`](module@crate::assert_ready::assert_ready_eq_x) ≈ a is Ready ⇒ unwrap = x
159//!
160//! Collections:
161//!
162//! * [`assert_iter_eq!(a, b)`](module@crate::assert_iter) ≈ a into iter = b into iter
163//! * [`assert_set_eq!(a, b)`](module@crate::assert_set) ≈ a into set = b into set
164//! * [`assert_bag_eq!(a, b)`](module@crate::assert_bag) ≈ a into bag = = b into bag
165//!
166//! Readers:
167//!
168//! * [`assert_fs_read_to_string_eq_x!(path, x)`](module@crate::assert_fs_read_to_string) ≈ path ⇒ file ⇒ string = x
169//! * [`assert_io_read_to_string_eq_x!(reader, x)`](module@crate::assert_io_read_to_string) ≈ reader ⇒ bytes ⇒ string = x
170//!
171//! Commands:
172//!
173//! * [`assert_command_stdout_eq_x!(command, x)`](module@crate::assert_command) `// command ⇒ stdout == x`
174//! * [`assert_program_args_stdout_eq_x!(program, args, x)`](module@crate::assert_program_args) `// program.args ⇒ stdout == x`
175//!
176//! Status:
177//!
178//! * [`assert_status_success!(a)`](module@crate::assert_status::assert_status_success) ≈ a.status().success()
179//! * [`assert_status_code_value_eq_x!(a, x)`](module@crate::assert_status::assert_status_code_value_eq_x) ≈ a.status().code().unwrap() = x
180//!
181//! Infix:
182//!
183//! * [`assert_infix!(a == b)`](module@crate::assert_infix) ≈ order operators == != < <= > >=
184//! * [`assert_infix!(a && b)`](module@crate::assert_infix) ≈ logic operators && || ^ & |
185//!
186//! For a complete list of modules and macros, see the
187//! [docs](https://docs.rs/assertables/).
188//!
189//!
190//! ## Forms
191//!
192//! The Assertables macros have a variety of forms to help you write the tests that matter most to you.
193//!
194//! All the macros have forms for an optional message:
195//!
196//! * [`assert_gt!(a, b)`](module@crate::assert_gt) ≈ default message
197//! * [`assert_gt!(a, b, "your text")`](module@crate::assert_gt) ≈ custom message
198//!
199//! All the macros have forms for different outcomes:
200//!
201//! * [`assert_gt!(1, 2)`](macro@crate::assert_gt) ≈ panic
202//! * [`assert_gt_as_result!(1, 2)`](macro@crate::assert_gt_as_result) ≈ Result Err
203//! * [`debug_assert_gt!(1, 2)`](macro@crate::debug_assert_gt) ≈ panic in debug mode
204//!
205//! Many of the macros have a form "compare left item to right item" that compares
206//! items of the same kind, and a form "compare left item to right expression" that
207//! compares one item to any arbitrary expression:
208//!
209//! * [`assert_len_eq!(a, b)`](module@crate::assert_ok::assert_ok_eq) ≈ a.len() = b.len()
210//! * [`assert_len_eq_x!(a, x)`](module@crate::assert_ok::assert_ok_eq_x)) ≈ a.len() = x
211//!
212//! Many of the macros has a "success return", which means the macro returns data that you can optionally use for more testing.
213//!
214//! * [`let inner = assert_ok!(result)`](module@crate::assert_ok::assert_ok)
215//! * [`let string = assert_fs_read_to_string_ne!("alfa.txt", "")`](module@crate::assert_fs_read_to_string::assert_fs_read_to_string_ne)
216//! * [`let stdout = assert_command_stdout_gt!("ls", vec![b' '])`](module@crate::assert_command::assert_command_stdout_gt)
217//!
218//! ## Tracking
219//!
220//! * Package: assertables-rust-crate
221//! * Version: 9.5.3
222//! * Created: 2021-03-30T15:47:49Z
223//! * Updated: 2025-06-02T18:47:17Z
224//! * License: MIT or Apache-2.0 or GPL-2.0 or GPL-3.0 or contact us for more
225//! * Contact: Joel Parker Henderson (joel@joelparkerhenderson.com)
226
227// Assert truth
228pub mod assert; // (in addition to what's provided by Rust `std`)
229
230// Assert value comparison
231pub mod assert_eq; // (in addition to what's provided by Rust `std`)
232pub mod assert_ge;
233pub mod assert_gt;
234pub mod assert_le;
235pub mod assert_lt;
236pub mod assert_ne; // (in addition to what's provided by Rust `std`)
237
238// Assert difference
239pub mod assert_abs_diff;
240pub mod assert_approx;
241pub mod assert_diff;
242pub mod assert_in;
243
244// Assert all/any
245pub mod assert_all;
246pub mod assert_any;
247
248// Infix
249pub mod assert_infix;
250
251// Matching
252pub mod assert_contains;
253pub mod assert_count;
254pub mod assert_ends_with;
255pub mod assert_is_empty;
256pub mod assert_is_match;
257pub mod assert_len;
258pub mod assert_matches;
259pub mod assert_starts_with;
260
261// For Result Ok & Err
262pub mod assert_err;
263pub mod assert_ok;
264pub mod assert_result; // Deprecated
265
266// For Option Some & None
267pub mod assert_none;
268pub mod assert_option;
269pub mod assert_some; // Deprecated
270
271// For Poll Ready & Pending
272pub mod assert_pending;
273pub mod assert_poll;
274pub mod assert_ready; // Deprecated
275
276// For collections
277pub mod assert_bag;
278pub mod assert_iter;
279pub mod assert_set;
280
281// For functions
282pub mod assert_fn;
283pub mod assert_fn_err;
284pub mod assert_fn_ok;
285
286// For reading
287pub mod assert_fs_read_to_string;
288pub mod assert_io_read_to_string;
289
290// For externals
291pub mod assert_command;
292pub mod assert_program_args;
293pub mod assert_status;
294
295// Misc
296pub mod assert_success;