Skip to content

Commit f4fc719

Browse files
authored
[2/n] [daft-derive] make snapshot_test only look at types annotated with derive(Diffable)
We want to add some more tests where some types are annotated with `derive(Diffable)` and others aren't. Also extend snapshot tests to look at unions. Reviewers: andrewjstone Reviewed By: andrewjstone Pull Request: #59
1 parent cbeb377 commit f4fc719

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

daft-derive/tests/fixtures/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ These fixtures ensure that:
99

1010
Each file in `valid` is automatically picked up by the snapshot and UI tests.
1111

12-
Currently, `snapshot_test.rs` only tests the first struct or enum in the file.
13-
The test can be extended to test multiple macro invocations per file if
14-
necessary.
12+
`snapshot_test.rs` tests all macro invocations annotated with `#[derive(Diffable)]`.
1513

1614
## Invalid fixtures
1715

@@ -21,3 +19,5 @@ These fixtures ensure that:
2119
* the macro's output fails with a good error message, via `ui_test.rs`.
2220

2321
Each file in `invalid` is automatically picked up by the snapshot and UI tests.
22+
23+
Like with valid fixtures, `snapshot_test.rs` tests all macro invocations annotated with `#[derive(Diffable)]`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl ::daft::Diffable for MyUnion {
2+
type Diff<'__daft> = ::daft::Leaf<&'__daft Self> where Self: '__daft;
3+
fn diff<'__daft>(&'__daft self, other: &'__daft Self) -> Self::Diff<'__daft> {
4+
::daft::Leaf {
5+
before: self,
6+
after: other,
7+
}
8+
}
9+
}
10+
impl ::daft::Diffable for MyUnion2 {
11+
type Diff<'__daft> = ::daft::Leaf<&'__daft Self> where Self: '__daft;
12+
fn diff<'__daft>(&'__daft self, other: &'__daft Self) -> Self::Diff<'__daft> {
13+
::daft::Leaf {
14+
before: self,
15+
after: other,
16+
}
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl ::daft::Diffable for Inner {
2+
type Diff<'__daft> = ::daft::Leaf<&'__daft Self> where Self: '__daft;
3+
fn diff<'__daft>(&'__daft self, other: &'__daft Self) -> Self::Diff<'__daft> {
4+
::daft::Leaf {
5+
before: self,
6+
after: other,
7+
}
8+
}
9+
}

daft-derive/tests/snapshot_test.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ fn run_derive_macro(
4949
) -> impl Iterator<Item = internals::DeriveDiffableOutput> + '_ {
5050
// Look for structs and enums in the input -- give them to the derive macro.
5151
let items = data.items.iter().filter_map(|item| match item {
52-
syn::Item::Struct(item) => Some(item.to_token_stream()),
53-
syn::Item::Enum(item) => Some(item.to_token_stream()),
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+
}
5461
_ => None,
5562
});
5663

@@ -63,6 +70,24 @@ fn run_derive_macro(
6370
})
6471
}
6572

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+
6691
fn assert_derive_output<T: ToTokens>(
6792
path: &Utf8Path,
6893
output: impl IntoIterator<Item = T>,

0 commit comments

Comments
 (0)