This PR adds some utilities for snapshot testing (inspired by Jest), and migrates most of the parser tests over to snapshots.
Creating snapshot tests
To use snapshot testing, first import snapshot!, snapshot_test, and begin_snapshots! from the new snapshot crate. Call begin_snapshots! at the top of the test file, before creating any snapshot tests.
use snapshot::{begin_snapshots, snapshot, snapshot_test};
begin_snapshots!();
Annotate snapshot test functions with the #[snapshot_test] attribute — this is a drop-in replacement for #[test]:
This PR adds some utilities for snapshot testing (inspired by Jest), and migrates most of the parser tests over to snapshots.
Creating snapshot tests
To use snapshot testing, first import
snapshot!
,snapshot_test
, andbegin_snapshots!
from the newsnapshot
crate. Callbegin_snapshots!
at the top of the test file, before creating any snapshot tests.Annotate snapshot test functions with the
#[snapshot_test]
attribute — this is a drop-in replacement for#[test]
:In the body of the test function, create snapshot tests by calling the
snapshot!
macro. The macro can be invoked with one of two signatures:Check the result of parsing a particular syntax node (or an entire syntax tree)
Check the result of any arbitrary format string (using the familiar
std::fmt
syntax)How it works
Each
snapshot!
invocation is associated with a file path in this format:Where:
<crate>
is the manifest directory of the crate being tested<path/to/test/module>
is the module path of the file where the test functions are written<function_name>
is the name of the#[snapshot_test]
function<test_number>
is n for the nth invocation of thesnapshot!
macro within that functionWhen the crate is tested, for each
snapshot!
invocation:nx test <project>
was called with the--update
flag, the snapshot file is overwritten with the new result--update
flag was not passed, the test fails, with the diff printed tostderr