-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #146
- Loading branch information
Showing
6 changed files
with
255 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use darling::{util::Flag, FromDeriveInput, FromMeta}; | ||
use proc_macro2::Ident; | ||
use syn::parse_quote; | ||
|
||
#[derive(FromMeta)] | ||
struct Vis { | ||
public: Flag, | ||
private: Flag, | ||
} | ||
|
||
#[derive(FromDeriveInput)] | ||
#[darling(attributes(sample))] | ||
struct Example { | ||
ident: Ident, | ||
label: String, | ||
#[darling(flatten)] | ||
visibility: Vis, | ||
} | ||
|
||
#[test] | ||
fn happy_path() { | ||
let di = Example::from_derive_input(&parse_quote! { | ||
#[sample(label = "Hello", public)] | ||
struct Demo {} | ||
}); | ||
|
||
let parsed = di.unwrap(); | ||
assert_eq!(parsed.ident, "Demo"); | ||
assert_eq!(&parsed.label, "Hello"); | ||
assert!(parsed.visibility.public.is_present()); | ||
assert!(!parsed.visibility.private.is_present()); | ||
} | ||
|
||
#[test] | ||
fn unknown_field_errors() { | ||
let errors = Example::from_derive_input(&parse_quote! { | ||
#[sample(label = "Hello", republic)] | ||
struct Demo {} | ||
}) | ||
.map(|_| "Should have failed") | ||
.unwrap_err(); | ||
|
||
assert_eq!(errors.len(), 1); | ||
} | ||
|
||
/// This test demonstrates flatten being used recursively. | ||
/// Fields are expected to be consumed by the outermost matching struct. | ||
#[test] | ||
fn recursive_flattening() { | ||
#[derive(FromMeta)] | ||
struct Nested2 { | ||
above: isize, | ||
below: isize, | ||
port: Option<isize>, | ||
} | ||
|
||
#[derive(FromMeta)] | ||
struct Nested1 { | ||
port: isize, | ||
starboard: isize, | ||
#[darling(flatten)] | ||
z_axis: Nested2, | ||
} | ||
|
||
#[derive(FromMeta)] | ||
struct Nested0 { | ||
fore: isize, | ||
aft: isize, | ||
#[darling(flatten)] | ||
cross_section: Nested1, | ||
} | ||
|
||
#[derive(FromDeriveInput)] | ||
#[darling(attributes(boat))] | ||
struct BoatPosition { | ||
#[darling(flatten)] | ||
pos: Nested0, | ||
} | ||
|
||
let parsed = BoatPosition::from_derive_input(&parse_quote! { | ||
#[boat(fore = 1, aft = 1, port = 10, starboard = 50, above = 20, below = -3)] | ||
struct Demo; | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(parsed.pos.fore, 1); | ||
assert_eq!(parsed.pos.aft, 1); | ||
|
||
assert_eq!(parsed.pos.cross_section.port, 10); | ||
assert_eq!(parsed.pos.cross_section.starboard, 50); | ||
|
||
assert_eq!(parsed.pos.cross_section.z_axis.above, 20); | ||
assert_eq!(parsed.pos.cross_section.z_axis.below, -3); | ||
// This should be `None` because the `port` field in `Nested1` consumed | ||
// the field before the leftovers were passed to `Nested2::from_list`. | ||
assert_eq!(parsed.pos.cross_section.z_axis.port, None); | ||
} | ||
|
||
/// This test confirms that a collection - in this case a HashMap - can | ||
/// be used with `flatten`. | ||
#[test] | ||
fn flattening_into_hashmap() { | ||
#[derive(FromDeriveInput)] | ||
#[darling(attributes(ca))] | ||
struct Catchall { | ||
hello: String, | ||
volume: usize, | ||
#[darling(flatten)] | ||
others: std::collections::HashMap<String, String>, | ||
} | ||
|
||
let parsed = Catchall::from_derive_input(&parse_quote! { | ||
#[ca(hello = "World", volume = 10, first_name = "Alice", second_name = "Bob")] | ||
struct Demo; | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!(parsed.hello, "World"); | ||
assert_eq!(parsed.volume, 10); | ||
assert_eq!(parsed.others.len(), 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use darling::{util::Flag, Error, FromDeriveInput, FromMeta}; | ||
use proc_macro2::Ident; | ||
use syn::parse_quote; | ||
|
||
#[derive(FromMeta)] | ||
#[darling(and_then = Self::validate)] | ||
struct Vis { | ||
public: Flag, | ||
private: Flag, | ||
} | ||
|
||
impl Vis { | ||
fn validate(self) -> darling::Result<Self> { | ||
if self.public.is_present() && self.private.is_present() { | ||
return Err(Error::custom("Cannot be both public and private")); | ||
} | ||
|
||
Ok(self) | ||
} | ||
} | ||
|
||
#[derive(FromDeriveInput)] | ||
#[darling(attributes(sample))] | ||
#[allow(dead_code)] | ||
struct Example { | ||
ident: Ident, | ||
label: String, | ||
volume: usize, | ||
#[darling(flatten)] | ||
visibility: Vis, | ||
} | ||
|
||
#[test] | ||
fn many_errors() { | ||
let e = Example::from_derive_input(&parse_quote! { | ||
#[sample(volume = 10, public, private)] | ||
struct Demo {} | ||
}) | ||
.map(|_| "Should have failed") | ||
.unwrap_err(); | ||
|
||
// We are expecting an error from the Vis::validate method and an error for the | ||
// missing `label` field. | ||
assert_eq!(e.len(), 2); | ||
} |