1 // Copyright 2017 The UNIC Project Developers.
2 //
3 // See the COPYRIGHT file at the top-level directory of this distribution.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 #[macro_use]
12 extern crate unic_char_property;
13 
14 use unic_char_property::PartialCharProperty;
15 
16 char_property! {
17     pub enum MyProp {
18         abbr => "mp";
19         long => "My_Prop";
20         human => "My Property";
21 
22         /// Variants can have multi-line documentations,
23         /// and/or other attributes.
24         Variant1 {
25             abbr => V1,
26             long => Variant_1,
27             human => "Variant 1",
28         }
29 
30         /// One line works too, or...
31         Variant2 {
32             abbr => V2,
33             long => Variant_2,
34             human => "Variant 2",
35         }
36 
37         Variant3 {
38             abbr => V3,
39             long => Variant_3,
40             human => "Variant 3",
41         }
42     }
43 
44     pub mod abbr_names for abbr;
45     pub mod long_names for long;
46 }
47 
48 impl PartialCharProperty for MyProp {
of(_: char) -> Option<Self>49     fn of(_: char) -> Option<Self> {
50         None
51     }
52 }
53 
54 #[test]
test_basic_macro_use()55 fn test_basic_macro_use() {
56     use unic_char_property::EnumeratedCharProperty;
57 
58     assert_eq!(MyProp::Variant1, abbr_names::V1);
59     assert_eq!(MyProp::Variant2, abbr_names::V2);
60     assert_eq!(MyProp::Variant3, abbr_names::V3);
61 
62     assert_eq!(MyProp::Variant1, long_names::Variant_1);
63     assert_eq!(MyProp::Variant2, long_names::Variant_2);
64     assert_eq!(MyProp::Variant3, long_names::Variant_3);
65 
66     assert_eq!(MyProp::Variant1.abbr_name(), "V1");
67     assert_eq!(MyProp::Variant2.abbr_name(), "V2");
68     assert_eq!(MyProp::Variant3.abbr_name(), "V3");
69 
70     assert_eq!(MyProp::Variant1.long_name(), "Variant_1");
71     assert_eq!(MyProp::Variant2.long_name(), "Variant_2");
72     assert_eq!(MyProp::Variant3.long_name(), "Variant_3");
73 
74     assert_eq!(MyProp::Variant1.human_name(), "Variant 1");
75     assert_eq!(MyProp::Variant2.human_name(), "Variant 2");
76     assert_eq!(MyProp::Variant3.human_name(), "Variant 3");
77 }
78 
79 #[test]
test_fromstr_ignores_case()80 fn test_fromstr_ignores_case() {
81     use crate::abbr_names::V1;
82 
83     assert_eq!("variant_1".parse(), Ok(V1));
84     assert_eq!("VaRiAnT_1".parse(), Ok(V1));
85     assert_eq!("vArIaNt_1".parse(), Ok(V1));
86     assert_eq!("VARIANT_1".parse(), Ok(V1));
87 }
88