| 1 |
use darling::{Error, FromMeta}; |
| 2 |
use syn::parse_quote; |
| 3 |
|
| 4 |
#[derive(Debug, FromMeta)] |
| 5 |
struct Meta { |
| 6 |
#[darling(default)] |
| 7 |
meta1: Option<String>, |
| 8 |
#[darling(default)] |
| 9 |
meta2: bool, |
| 10 |
} |
| 11 |
|
| 12 |
#[test] |
| 13 |
fn nested_meta_meta_value() { |
| 14 |
let meta = Meta::from_list(&[parse_quote! { |
| 15 |
meta1 = "thefeature" |
| 16 |
}]) |
| 17 |
.unwrap(); |
| 18 |
assert_eq!(meta.meta1, Some("thefeature".to_string())); |
| 19 |
assert!(!meta.meta2); |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn nested_meta_meta_bool() { |
| 24 |
let meta = Meta::from_list(&[parse_quote! { |
| 25 |
meta2 |
| 26 |
}]) |
| 27 |
.unwrap(); |
| 28 |
assert_eq!(meta.meta1, None); |
| 29 |
assert!(meta.meta2); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn nested_meta_lit_string_errors() { |
| 34 |
let err = Meta::from_list(&[parse_quote! { |
| 35 |
"meta2" |
| 36 |
}]) |
| 37 |
.unwrap_err(); |
| 38 |
assert_eq!( |
| 39 |
err.to_string(), |
| 40 |
Error::unsupported_format("literal").to_string() |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn nested_meta_lit_integer_errors() { |
| 46 |
let err = Meta::from_list(&[parse_quote! { |
| 47 |
2 |
| 48 |
}]) |
| 49 |
.unwrap_err(); |
| 50 |
assert_eq!( |
| 51 |
err.to_string(), |
| 52 |
Error::unsupported_format("literal").to_string() |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn nested_meta_lit_bool_errors() { |
| 58 |
let err = Meta::from_list(&[parse_quote! { |
| 59 |
true |
| 60 |
}]) |
| 61 |
.unwrap_err(); |
| 62 |
assert_eq!( |
| 63 |
err.to_string(), |
| 64 |
Error::unsupported_format("literal").to_string() |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/// Tests behavior of FromMeta implementation for enums. |
| 69 |
mod enum_impl { |
| 70 |
use darling::{Error, FromMeta}; |
| 71 |
use syn::parse_quote; |
| 72 |
|
| 73 |
/// A playback volume. |
| 74 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromMeta)] |
| 75 |
enum Volume { |
| 76 |
Normal, |
| 77 |
Low, |
| 78 |
High, |
| 79 |
#[darling(rename = "dB")] |
| 80 |
Decibels(u8), |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn string_for_unit_variant() { |
| 85 |
let volume = Volume::from_string("low").unwrap(); |
| 86 |
assert_eq!(volume, Volume::Low); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn single_value_list() { |
| 91 |
let unit_variant = Volume::from_list(&[parse_quote!(high)]).unwrap(); |
| 92 |
assert_eq!(unit_variant, Volume::High); |
| 93 |
|
| 94 |
let newtype_variant = Volume::from_list(&[parse_quote!(dB = 100)]).unwrap(); |
| 95 |
assert_eq!(newtype_variant, Volume::Decibels(100)); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn empty_list_errors() { |
| 100 |
let err = Volume::from_list(&[]).unwrap_err(); |
| 101 |
assert_eq!(err.to_string(), Error::too_few_items(1).to_string()); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn multiple_values_list_errors() { |
| 106 |
let err = Volume::from_list(&[parse_quote!(low), parse_quote!(dB = 20)]).unwrap_err(); |
| 107 |
assert_eq!(err.to_string(), Error::too_many_items(1).to_string()); |
| 108 |
} |
| 109 |
} |