Debian Codegraph @

tests/snapshot_test.rs

raw symbols SCIP
1 use datatest_stable::Utf8Path;
2 use quote::ToTokens;
3 use syn::{parse_quote, 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 internals;
11
12 datatest_stable::harness! {
13 // The pattern matches all .rs files that aren't .output.rs files.
14 { test = daft_snapshot, root = "tests/fixtures/valid", pattern = r"^.*(?<!\.output)\.rs$" },
15 { test = daft_snapshot_invalid, root = "tests/fixtures/invalid", pattern = r"^.*(?<!\.output)\.rs$" },
16 }
17
18 /// Snapshot tests for valid inputs.
19 fn daft_snapshot(
20 path: &Utf8Path,
21 input: String,
22 ) -> datatest_stable::Result<()> {
23 let data = syn::parse_str::<syn::File>(&input)?;
24
25 let output = run_derive_macro(&data);
26 assert_derive_output(path, output);
27
28 Ok(())
29 }
30
31 /// Snapshot tests for invalid inputs.
32 fn daft_snapshot_invalid(
33 path: &Utf8Path,
34 input: String,
35 ) -> datatest_stable::Result<()> {
36 let data = syn::parse_str::<syn::File>(&input)?;
37
38 let output = run_derive_macro(&data).map(|output| {
39 // Drop the errors for snapshot tests -- only use the output.
40 output.out
41 });
42 assert_derive_output(path, output);
43
44 Ok(())
45 }
46
47 fn run_derive_macro(
48 data: &syn::File,
49 ) -> impl Iterator<Item = internals::DeriveDiffableOutput> + '_ {
50 // Look for structs and enums in the input -- give them to the derive macro.
51 let items = data.items.iter().filter_map(|item| match item {
52 syn::Item::Struct(item) => {
53 has_derive_diffable(&item.attrs).then(|| item.to_token_stream())
54 }
55 syn::Item::Enum(item) => {
56 has_derive_diffable(&item.attrs).then(|| item.to_token_stream())
57 }
58 syn::Item::Union(item) => {
59 has_derive_diffable(&item.attrs).then(|| item.to_token_stream())
60 }
61 _ => None,
62 });
63
64 // Turn each item into a `syn::DeriveInput` and run the derive macro on it.
65 items.enumerate().map(|(i, item)| {
66 let data = syn::parse2::<DeriveInput>(item).unwrap_or_else(|err| {
67 panic!("failed to parse item {i}: {err}");
68 });
69 internals::derive_diffable(data)
70 })
71 }
72
73 fn has_derive_diffable(attrs: &[syn::Attribute]) -> bool {
74 attrs.iter().any(|attr| {
75 if !attr.path().is_ident("derive") {
76 return false;
77 }
78
79 let mut is_diffable = false;
80 attr.parse_nested_meta(|meta| {
81 if meta.path.is_ident("Diffable") {
82 is_diffable = true;
83 }
84 Ok(())
85 })
86 .expect("derive attributes parsed correctly");
87 is_diffable
88 })
89 }
90
91 fn assert_derive_output<T: ToTokens>(
92 path: &Utf8Path,
93 output: impl IntoIterator<Item = T>,
94 ) {
95 // Read the output as a `syn::File`.
96 let output = output.into_iter();
97 let file = parse_quote! {
98 #(#output)*
99 };
100
101 // Format the output.
102 let output = prettyplease::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.parent().unwrap().to_owned();
107 output_path.push("output");
108 output_path.push(path.file_name().unwrap());
109 output_path.set_extension("output.rs");
110
111 expectorate::assert_contents(&output_path, &output);
112 }