Expect Tests
Expect Tests is a Rust crate inspired by rust-analyzer's expect-test. Explanation can be found here.
Basic Usage:
expect!;
The macro uses the Debug trait representation (except for string literals) and compares it to the provided string literal. In case of a mismatch, an error with a diff will be shown:
You can update all `expect!` tests by running:
UPDATE_EXPECT=1 cargo test
To update a single test, place the cursor on `expect` token and use the `run` feature of rust-analyzer.
Expect:
----
987
----
Actual:
----
610
----
Diff:
----
987610
----
If the UPDATE_EXPECT
environment variable is set, the macro will directly update the source file to fix it. For example:
expect!;
After running UPDATE_EXPECT=1 cargo test
, your source file should automatically be updated to:
expect!;
Multiple Expects:
Testing callbacks can be cumbersome because you'd need to collect into a vec. Here's an alternative using expect!
:
some_complicated_io_func;
Caveat: Expect-tests cannot detect when the expect!
macro is never called. Therefore, a loop like the following will pass even though "2", "3", and "4" are never run:
for i in 0..2
Expect Tokens:
Testing proc macros is now easier with expect tokens:
Examples:
Check out the examples used to test this crate.
Alternatives:
Other Notes:
- Makes a "best effort" to comply with rustfmt. When the macro detects a multiline expect, updating will attempt to insert newlines where appropriate.
- Indentations are ignored in the string literals when comparing to make the code look nicer.