|
1 |
| -# Compile-Fail Testing |
| 1 | +# Testing the `#[versioned]` macro |
2 | 2 |
|
3 |
| -> [!NOTE] |
4 |
| -> Also see the snapshot tests, described [here](../fixtures/README.md). |
5 |
| -
|
6 |
| -This type of testing is part of UI testing. These tests assert two things: First, the code should |
7 |
| -**not** compile and secondly should also produce the expected rustc (compiler) error message. For |
8 |
| -this type of testing, we use the [`trybuild`][trybuild] crate. |
| 3 | +This folder contains both snapshot and compile (trybuild) tests. Both types of tests use the same |
| 4 | +set of input files to both ensure the macro generates the expected code and either compiles or |
| 5 | +produces the expected compile error. |
9 | 6 |
|
10 | 7 | Tests are currently separated into two folders: `default` and `k8s`. The default test cases don't
|
11 | 8 | require any additional features to be activated. The Kubernetes specific tests require the `k8s`
|
12 | 9 | feature to be enabled. These tests can be run with `cargo test --all-features`.
|
13 | 10 |
|
| 11 | +## Snapshot Testing |
| 12 | + |
| 13 | +> [!NOTE] |
| 14 | +> Please have `rust-src` installed, e.g. using `rustup component add rust-src`. |
| 15 | +> |
| 16 | +> Also see the compile-fail tests, described [here](#compile-fail-testing). |
| 17 | +
|
| 18 | +Snapshot testing is done using the [insta] crate. It provides a [CLI tool][insta-cli] calle |
| 19 | + `cargo-insta` and a [VS Code extension][insta-ext]. |
| 20 | + |
| 21 | +Test inputs and snapshots of the expected output are located in the `inputs` and `snapshots` folder |
| 22 | +respectively. Each Rust attribute macro expects two inputs as a token stream: |
| 23 | + |
| 24 | +> The first TokenStream is the delimited token tree following the attribute’s name, not including |
| 25 | +> the outer delimiters. If the attribute is written as a bare attribute name, the attribute |
| 26 | +> TokenStream is empty. The second TokenStream is the rest of the item including other attributes on |
| 27 | +> the item. |
| 28 | +> |
| 29 | +> _(Taken from the [Rust reference][rust-ref])_ |
| 30 | +
|
| 31 | +Because of that, a special delimiter is used in the input files which separates different sections |
| 32 | +of the input file while still enabling developers to write valid Rust code. The delimiter is |
| 33 | +`// ---\n`. Most of the inner workings are located in [this file](../src/test_utils.rs). |
| 34 | + |
| 35 | +```rust |
| 36 | +use stackable_versioned::versioned; |
| 37 | +// --- <- See here! |
| 38 | +#[versioned( |
| 39 | + version(name = "v1alpha1"), |
| 40 | + version(name = "v1beta1"), |
| 41 | + version(name = "v1") |
| 42 | +)] |
| 43 | +// --- <- See here! |
| 44 | +pub(crate) struct Foo { |
| 45 | + #[versioned( |
| 46 | + changed(since = "v1beta1", from_name = "jjj", from_type = "u8"), |
| 47 | + changed(since = "v1", from_type = "u16"), |
| 48 | + )] |
| 49 | + bar: usize, |
| 50 | + baz: bool, |
| 51 | +} |
| 52 | +// --- <- See here! |
| 53 | +fn main() {} |
| 54 | + |
| 55 | +// Rest of code ... |
| 56 | +``` |
| 57 | + |
| 58 | +Input files must include **three** separators which produce **four** distinct sections: |
| 59 | + |
| 60 | +- Imports, like `stackable_versioned::versioned` |
| 61 | +- The attribute macro |
| 62 | +- The item the macro is applied to |
| 63 | +- The rest of the code, like the `main` function |
| 64 | + |
| 65 | +### Recommended Workflow |
| 66 | + |
| 67 | +First, add new input files (which automatically get picked up by `insta`) to the `inputs` |
| 68 | +folder. Make sure the delimiter is placed correctly between the different sections. Doc comments on |
| 69 | +the container have to be placed after the delimiter. Next, generate the snapshot files (initially |
| 70 | +not accepted) by running |
| 71 | + |
| 72 | +```shell |
| 73 | +cargo insta test -p stackable-versioned-macros --all-features |
| 74 | +``` |
| 75 | + |
| 76 | +This command will place the new snapshot files (with a `.new` extension) in the `snapshots` folder. |
| 77 | +These new snapshot files must not appear on `main`, but can be shared on branches for collaboration. |
| 78 | +To review them, run the `cargo insta review` command, then accept or fix the snapshots. Once all are |
| 79 | +accepted (ie: no `.new` files remaining), check in the files. |
| 80 | + |
| 81 | +## Compile-Fail Testing |
| 82 | + |
| 83 | +> [!NOTE] |
| 84 | +> Also see the snapshot tests, described [here](#snapshot-testing). |
| 85 | +
|
| 86 | +This type of testing is part of UI testing. These tests assert two things: First, some code should |
| 87 | +compile without errors and secondly other code should produce the expected rustc (compiler) error |
| 88 | +message. For this type of testing, we use the [`trybuild`][trybuild] crate. |
| 89 | + |
14 | 90 | Further information about the workflow are described [here][workflow].
|
15 | 91 |
|
| 92 | +[rust-ref]: https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros |
16 | 93 | [workflow]: https://docs.rs/trybuild/latest/trybuild/#workflow
|
17 | 94 | [trybuild]: https://docs.rs/trybuild/latest/trybuild/
|
| 95 | +[insta-ext]: https://insta.rs/docs/vscode/ |
| 96 | +[insta-cli]: https://insta.rs/docs/cli/ |
| 97 | +[insta]: https://insta.rs/ |
0 commit comments