| 1 | use datatest_stablerust-analyzer cargo datatest-stable 0.3.2 crate/::Utf8Pathrust-analyzer cargo camino 1.1.9 Utf8Path#; |
| 2 | use quoterust-analyzer cargo quote 1.0.38 crate/::ToTokensrust-analyzer cargo quote 1.0.38 to_tokens/ToTokens#; |
| 3 | use synrust-analyzer cargo syn 2.0.96 crate/::{parse_quoterust-analyzer cargo syn 2.0.96 parse_quote/, DeriveInputrust-analyzer cargo syn 2.0.96 derive/DeriveInput#}; |
| 4 | |
| 5 | // We need access to the proc-macro's internals for this test. An alternative |
| 6 | // would be to make this a unit test, but the integration test harness gives us |
| 7 | // automatic discovery of tests in the `fixtures/` directory, along with |
| 8 | // separate reporting for each test. Those are nice benefits. |
| 9 | #[path = "../src/internals/mod.rs"] |
| 10 | mod internalsrust-analyzer cargo daft-derive 0.1.5 internals/; |
| 11 | |
| 12 | datatest_stablerust-analyzer cargo datatest-stable 0.3.2 crate/::harnessrust-analyzer cargo datatest-stable 0.3.2 macros/harness!! { |
| 13 | // The pattern matches all .rs files that aren't .output.rs files. |
| 14 | { test = daft_snapshotrust-analyzer cargo daft-derive 0.1.5 daft_snapshot()., root = "tests/fixtures/valid", pattern = r"^.*(?<!\.output)\.rs$" }, |
| 15 | { test = daft_snapshot_invalidrust-analyzer cargo daft-derive 0.1.5 daft_snapshot_invalid()., root = "tests/fixtures/invalid", pattern = r"^.*(?<!\.output)\.rs$" }, |
| 16 | } |
| 17 | |
| 18 | /// Snapshot tests for valid inputs. |
| 19 | fn daft_snapshotrust-analyzer cargo daft-derive 0.1.5 daft_snapshot().( |
| 20 | path: &Utf8Pathrust-analyzer cargo camino 1.1.9 Utf8Path#, |
| 21 | input: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 22 | ) -> datatest_stablerust-analyzer cargo datatest-stable 0.3.2 crate/::Resultrust-analyzer cargo datatest-stable 0.3.2 Result#<()> { |
| 23 | let data = synrust-analyzer cargo syn 2.0.96 crate/::parse_strrust-analyzer cargo syn 2.0.96 parse_str().::<synrust-analyzer cargo syn 2.0.96 crate/::Filerust-analyzer cargo syn 2.0.96 file/File#>(&input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 24 | |
| 25 | let output = run_derive_macrorust-analyzer cargo daft-derive 0.1.5 run_derive_macro().(&data); |
| 26 | assert_derive_outputrust-analyzer cargo daft-derive 0.1.5 assert_derive_output().(path, output); |
| 27 | |
| 28 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 29 | } |
| 30 | |
| 31 | /// Snapshot tests for invalid inputs. |
| 32 | fn daft_snapshot_invalidrust-analyzer cargo daft-derive 0.1.5 daft_snapshot_invalid().( |
| 33 | path: &Utf8Pathrust-analyzer cargo camino 1.1.9 Utf8Path#, |
| 34 | input: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 35 | ) -> datatest_stablerust-analyzer cargo datatest-stable 0.3.2 crate/::Resultrust-analyzer cargo datatest-stable 0.3.2 Result#<()> { |
| 36 | let data = synrust-analyzer cargo syn 2.0.96 crate/::parse_strrust-analyzer cargo syn 2.0.96 parse_str().::<synrust-analyzer cargo syn 2.0.96 crate/::Filerust-analyzer cargo syn 2.0.96 file/File#>(&input)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 37 | |
| 38 | let output = run_derive_macrorust-analyzer cargo daft-derive 0.1.5 run_derive_macro().(&data).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|output| { |
| 39 | // Drop the errors for snapshot tests -- only use the output. |
| 40 | output.outrust-analyzer cargo daft-derive 0.1.5 internals/imp/DeriveDiffableOutput#out. |
| 41 | }); |
| 42 | assert_derive_outputrust-analyzer cargo daft-derive 0.1.5 assert_derive_output().(path, output); |
| 43 | |
| 44 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 45 | } |
| 46 | |
| 47 | fn run_derive_macrorust-analyzer cargo daft-derive 0.1.5 run_derive_macro().( |
| 48 | data: &synrust-analyzer cargo syn 2.0.96 crate/::Filerust-analyzer cargo syn 2.0.96 file/File#, |
| 49 | ) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = internalsrust-analyzer cargo daft-derive 0.1.5 internals/::DeriveDiffableOutputrust-analyzer cargo daft-derive 0.1.5 internals/imp/DeriveDiffableOutput#> + '_ { |
| 50 | // Look for structs and enums in the input -- give them to the derive macro. |
| 51 | let items = data.itemsrust-analyzer cargo syn 2.0.96 file/File#items..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|item| match item { |
| 52 | synrust-analyzer cargo syn 2.0.96 crate/::Itemrust-analyzer cargo syn 2.0.96 item/Item#::Structrust-analyzer cargo syn 2.0.96 item/Item#Struct#(item) => { |
| 53 | has_derive_diffablerust-analyzer cargo daft-derive 0.1.5 has_derive_diffable().(&item.attrsrust-analyzer cargo syn 2.0.96 item/ItemStruct#attrs.).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| item.to_token_streamrust-analyzer cargo quote 1.0.38 to_tokens/ToTokens#to_token_stream().()) |
| 54 | } |
| 55 | synrust-analyzer cargo syn 2.0.96 crate/::Itemrust-analyzer cargo syn 2.0.96 item/Item#::Enumrust-analyzer cargo syn 2.0.96 item/Item#Enum#(item) => { |
| 56 | has_derive_diffablerust-analyzer cargo daft-derive 0.1.5 has_derive_diffable().(&item.attrsrust-analyzer cargo syn 2.0.96 item/ItemEnum#attrs.).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| item.to_token_streamrust-analyzer cargo quote 1.0.38 to_tokens/ToTokens#to_token_stream().()) |
| 57 | } |
| 58 | synrust-analyzer cargo syn 2.0.96 crate/::Itemrust-analyzer cargo syn 2.0.96 item/Item#::Unionrust-analyzer cargo syn 2.0.96 item/Item#Union#(item) => { |
| 59 | has_derive_diffablerust-analyzer cargo daft-derive 0.1.5 has_derive_diffable().(&item.attrsrust-analyzer cargo syn 2.0.96 item/ItemUnion#attrs.).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| item.to_token_streamrust-analyzer cargo quote 1.0.38 to_tokens/ToTokens#to_token_stream().()) |
| 60 | } |
| 61 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 62 | }); |
| 63 | |
| 64 | // Turn each item into a `syn::DeriveInput` and run the derive macro on it. |
| 65 | items.enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(i, item)| { |
| 66 | let data = synrust-analyzer cargo syn 2.0.96 crate/::parse2rust-analyzer cargo syn 2.0.96 parse2().::<DeriveInputrust-analyzer cargo syn 2.0.96 derive/DeriveInput#>(item).unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|err| { |
| 67 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("failed to parse item {i}: {err}"); |
| 68 | }); |
| 69 | internalsrust-analyzer cargo daft-derive 0.1.5 internals/::derive_diffablerust-analyzer cargo daft-derive 0.1.5 internals/imp/derive_diffable().(data) |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | fn has_derive_diffablerust-analyzer cargo daft-derive 0.1.5 has_derive_diffable().(attrs: &[synrust-analyzer cargo syn 2.0.96 crate/::Attributerust-analyzer cargo syn 2.0.96 attr/Attribute#]) -> bool { |
| 74 | attrs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|attr| { |
| 75 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().attr.pathrust-analyzer cargo syn 2.0.96 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.96 path/impl#[Path]is_ident().("derive") { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | let mut is_diffable = false; |
| 80 | attr.parse_nested_metarust-analyzer cargo syn 2.0.96 attr/impl#[Attribute]parse_nested_meta().(|meta| { |
| 81 | if meta.pathrust-analyzer cargo syn 2.0.96 meta/ParseNestedMeta#path..is_identrust-analyzer cargo syn 2.0.96 path/impl#[Path]is_ident().("Diffable") { |
| 82 | is_diffable = true; |
| 83 | } |
| 84 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 85 | }) |
| 86 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("derive attributes parsed correctly"); |
| 87 | is_diffable |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | fn assert_derive_outputrust-analyzer cargo daft-derive 0.1.5 assert_derive_output().<T: ToTokensrust-analyzer cargo quote 1.0.38 to_tokens/ToTokens#>( |
| 92 | path: &Utf8Pathrust-analyzer cargo camino 1.1.9 Utf8Path#, |
| 93 | output: impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = T>, |
| 94 | ) { |
| 95 | // Read the output as a `syn::File`. |
| 96 | let output = output.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().(); |
| 97 | let file = parse_quoterust-analyzer cargo syn 2.0.96 parse_quote/parse_quote!! { |
| 98 | #(#output)* |
| 99 | }; |
| 100 | |
| 101 | // Format the output. |
| 102 | let output = prettypleaserust-analyzer cargo prettyplease 0.2.29 crate/::unparserust-analyzer cargo prettyplease 0.2.29 unparse().(&file); |
| 103 | |
| 104 | // Compare the output with the snapshot. The new filename is the same as the |
| 105 | // input, but with ".output.rs" at the end. |
| 106 | let mut output_path = path.parentrust-analyzer cargo camino 1.1.9 impl#[Utf8Path]parent().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_ownedrust-analyzer cargo camino 1.1.9 impl#[Utf8Path][ToOwned]to_owned().(); |
| 107 | output_path.pushrust-analyzer cargo camino 1.1.9 impl#[Utf8PathBuf]push().("output"); |
| 108 | output_path.pushrust-analyzer cargo camino 1.1.9 impl#[Utf8PathBuf]push().(path.file_namerust-analyzer cargo camino 1.1.9 impl#[Utf8Path]file_name().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 109 | output_path.set_extensionrust-analyzer cargo camino 1.1.9 impl#[Utf8PathBuf]set_extension().("output.rs"); |
| 110 | |
| 111 | expectoraterust-analyzer cargo expectorate 1.1.0 crate/::assert_contentsrust-analyzer cargo expectorate 1.1.0 assert_contents().(&output_path, &output); |
| 112 | } |