dannymcgee / vscode-wgsl

Rich language support for WebGPU Shading Language
Apache License 2.0
14 stars 2 forks source link

Snapshot testing #30

Closed dannymcgee closed 6 months ago

dannymcgee commented 6 months ago

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]:

// ...
#[snapshot_test]
fn my_snapshot_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:

How it works

Each snapshot! invocation is associated with a file path in this format:

<crate>/snapshots/<path/to/test/module>/<function_name>-<test_number>.wgsl-ast

Where:

When the crate is tested, for each snapshot! invocation:

[!IMPORTANT] If you intend to update the snapshots, you must invoke the tests with Nx:

npx nx test <project> --update

Attempting to call cargo test ... --update will fail due to the unrecognized --update flag.