1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Test that skipped fields are not read into structs when they appear in input.
use std::assert_eq;
use darling::{FromDeriveInput, FromMeta};
use syn::parse_quote;
#[derive(Debug, PartialEq, Eq, FromDeriveInput)]
#[darling(attributes(skip_test))]
pub struct Lorem {
ipsum: String,
#[darling(skip)]
dolor: u8,
}
/// Verify variant-level and field-level skip work correctly for enums.
#[derive(Debug, FromMeta)]
pub enum Sit {
Amet(bool),
#[darling(skip)]
Foo {
hello: bool,
},
Bar {
hello: bool,
#[darling(skip)]
world: u8,
},
}
#[test]
fn verify_skipped_field_not_required() {
let di = parse_quote! {
#[skip_test(ipsum = "Hello")]
struct Baz;
};
assert_eq!(
Lorem::from_derive_input(&di).unwrap(),
Lorem {
ipsum: "Hello".to_string(),
dolor: 0,
}
);
}
/// This test verifies that a skipped field will still prefer an explicit default
/// over the default that would come from its field type. It would be incorrect for
/// `Defaulting::from_derive_input` to fail here, and it would be wrong for the value
/// of `dolor` to be `None`.
#[test]
fn verify_default_supersedes_from_none() {
fn default_dolor() -> Option<u8> {
Some(2)
}
#[derive(Debug, PartialEq, Eq, FromDeriveInput)]
#[darling(attributes(skip_test))]
pub struct Defaulting {
#[darling(skip, default = "default_dolor")]
dolor: Option<u8>,
}
let di = parse_quote! {
#[skip_test]
struct Baz;
};
assert_eq!(
Defaulting::from_derive_input(&di).unwrap(),
Defaulting { dolor: Some(2) }
)
}
/// Verify skipping a variant also excludes that from "did you mean" suggestions.
#[derive(Debug, FromMeta)]
pub enum SkippedVariant {
Foo,
#[darling(skip)]
Boo,
}
#[test]
fn skipped_variants_are_not_suggested() {
let err_msg = SkippedVariant::from_string("boo")
.expect_err("boo variant is not constructable by FromMeta")
.to_string();
// If `boo` was improperly considered for "did-you-mean", it will be the top match.
// At time of writing, only the top candidate was included in the error, so we check
// that the second-place suggestion was present in the message, and try to defend against
// a multi-suggestion future by also checking that `boo` only appears once in the error.
assert_eq!(
err_msg.split("`boo`").count(),
2,
"error message contained `boo` too many times, was: {err_msg}"
);
assert!(
err_msg.contains("`foo`"),
"error message did not suggest `foo`, was actually: {err_msg}",
);
}