should_match
Pass a test if the output matches a pattern.
Features
This library is lightweight and fast, since it is based on macro_rules, with zero dependencies.
Setup
This crate is primarily intended for use in tests, so add it to your dev-dependencies instead of dependencies:
cargo add --dev should_match
Recommended to work with macro_rules_attr, which provides nice syntactic sugar:
cargo add --dev macro_rules_attr should_match
The should_match macro
With macro_rules_attr
Simply apply the should_match macro, and specify your pattern (the order of apply and should_match does not matter):
use apply;
use should_match;
// This test will pass
// This test will also pass
To specify a custom panic message when it fails, pass an additional message argument:
# use apply;
# use should_match;
#
Direct usage
You can also use the should_match macro directly, but note that #[test] must be wrapped inside the macro:
use should_match;
// Without custom message
should_match!
// With custom message
should_match!
Shortcuts
Predefined shortcuts
should_match provides some shortcuts for common patterns:
| Macro | Pattern | Message |
|---|---|---|
should_ok |
Ok(_) |
Expected `Ok`, but got `Err` |
should_err |
Err(_) |
Expected `Err`, but got `Ok` |
should_none |
None |
Expected `None`, but got `Some` |
should_some |
Some(_) |
Expected `Some`, but got `None` |
An example of using should_err:
use apply;
use should_err;
Other shortcuts can be used in the same way.
Define custom shortcuts
use apply;
use should_match;
}}