summaryrefslogtreecommitdiffstats
path: root/third_party/rust/darling/tests/skip.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/darling/tests/skip.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/darling/tests/skip.rs')
-rw-r--r--third_party/rust/darling/tests/skip.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/third_party/rust/darling/tests/skip.rs b/third_party/rust/darling/tests/skip.rs
new file mode 100644
index 0000000000..f930ca5d31
--- /dev/null
+++ b/third_party/rust/darling/tests/skip.rs
@@ -0,0 +1,74 @@
+//! Test that skipped fields are not read into structs when they appear in input.
+
+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) }
+ )
+}