Expand description
§cov-mark
This library at its core provides two macros, hit!
and check!
,
which can be used to verify that a certain test exercises a certain code
path.
Here’s a short example:
fn parse_date(s: &str) -> Option<(u32, u32, u32)> {
if 10 != s.len() {
// By using `cov_mark::hit!`
// we signal which test exercises this code.
cov_mark::hit!(short_date);
return None;
}
if "-" != &s[4..5] || "-" != &s[7..8] {
cov_mark::hit!(bad_dashes);
return None;
}
// ...
}
#[test]
fn test_parse_date() {
{
// `cov_mark::check!` creates a guard object
// that verifies that by the end of the scope we've
// executed the corresponding `cov_mark::hit`.
cov_mark::check!(short_date);
assert!(parse_date("92").is_none());
}
// This will fail. Although the test looks like
// it exercises the second condition, it does not.
// The call to `check!` call catches this bug in the test.
// {
// cov_mark::check!(bad_dashes);
// assert!(parse_date("27.2.2013").is_none());
// }
{
cov_mark::check!(bad_dashes);
assert!(parse_date("27.02.2013").is_none());
}
}
Here’s why coverage marks are useful:
- Verifying that something doesn’t happen for the right reason.
- Finding the test that exercises the code (grep for
check!(mark_name)
). - Finding the code that the test is supposed to check (grep for
hit!(mark_name)
). - Making sure that code and tests don’t diverge during refactorings.
- (If used pervasively) Verifying that each branch has a corresponding test.
§Limitations
- Names of marks must be globally unique.
§Implementation Details
Each coverage mark is an AtomicUsize
counter. hit!
increments
this counter, check!
returns a guard object which checks that
the mark was incremented.
Each counter is stored as a thread-local, allowing for accurate per-thread
counting.
Macros§
- check
- Checks that a specified mark was hit.
- check_
count - Checks that a specified mark was hit exactly the specified number of times.
- hit
- Hit a mark with a specified name.