central_america/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(clippy::all)]
3
4#[macro_use] mod imports; use imports::*;
5
6x!{abbreviation}
7x!{country}
8x!{error}
9x!{impl_serde}
10x!{central_america}
11
12//-------------------------------------------------------------
13// Tests
14//-------------------------------------------------------------
15#[cfg(test)]
16mod test_central_america_region {
17    use super::*;
18    use serde_json;
19    use std::convert::TryFrom;
20    use std::str::FromStr;
21
22    #[test]
23    fn test_default() {
24        // Default should be Cuba
25        let def = CentralAmericaRegion::default();
26        assert_eq!(def, CentralAmericaRegion::Cuba);
27    }
28
29    #[test]
30    fn test_from_str() {
31        let parsed = CentralAmericaRegion::from_str("Panama").expect("Should parse Panama");
32        assert_eq!(parsed, CentralAmericaRegion::Panama);
33    }
34
35    #[test]
36    fn test_abbreviations() {
37        assert_eq!(CentralAmericaRegion::Bahamas.abbreviation(), "BS");
38        assert_eq!(CentralAmericaRegion::HaitiAndDominicanRepublic.abbreviation(), "HT-DO");
39        assert_eq!(CentralAmericaRegion::Nicaragua.abbreviation(), "NI");
40    }
41
42    #[test]
43    fn test_central_america_region_variants() {
44        let variants = CentralAmericaRegion::VARIANTS;
45        assert!(variants.contains(&"HaitiAndDominicanRepublic"));
46        assert!(variants.contains(&"Guatemala"));
47        assert!(variants.contains(&"ElSalvador"));
48    }
49
50    #[test]
51    fn test_central_america_region_to_country_success() {
52        assert_eq!(Country::try_from(CentralAmericaRegion::CostaRica).unwrap(), Country::CostaRica);
53        assert_eq!(Country::try_from(CentralAmericaRegion::HaitiAndDominicanRepublic).unwrap(), Country::Haiti);
54        assert_eq!(Country::try_from(CentralAmericaRegion::Panama).unwrap(), Country::Panama);
55    }
56
57    #[test]
58    fn test_country_to_central_america_region_success() {
59        assert_eq!(CentralAmericaRegion::try_from(Country::Cuba).unwrap(), CentralAmericaRegion::Cuba);
60        assert_eq!(CentralAmericaRegion::try_from(Country::Haiti).unwrap(), CentralAmericaRegion::HaitiAndDominicanRepublic);
61        assert_eq!(CentralAmericaRegion::try_from(Country::DominicanRepublic).unwrap(), CentralAmericaRegion::HaitiAndDominicanRepublic);
62    }
63
64    #[test]
65    fn test_country_to_central_america_region_errors() {
66        // A non-Central American country
67        match CentralAmericaRegion::try_from(Country::Brazil) {
68            Err(CentralAmericaRegionConversionError::NotCentralAmerican { .. }) => {}
69            _ => panic!("Expected NotCentralAmerican for Brazil"),
70        }
71    }
72
73    #[test]
74    fn test_iso_code_conversions() {
75        let region = CentralAmericaRegion::Belize;
76        let alpha2: Iso3166Alpha2 = region.try_into().unwrap();
77        let alpha3: Iso3166Alpha3 = region.try_into().unwrap();
78        let code: CountryCode = region.try_into().unwrap();
79
80        assert_eq!(alpha2, Iso3166Alpha2::BZ);
81        assert_eq!(alpha3, Iso3166Alpha3::BLZ);
82        match code {
83            CountryCode::Alpha2(a2) => assert_eq!(a2, Iso3166Alpha2::BZ),
84            _ => panic!("Expected Alpha2 code"),
85        }
86    }
87
88    #[test]
89    fn test_serialize_deserialize_non_subdivided() {
90        // Test round-trip for a non-subdivided country
91        let region = CentralAmericaRegion::Jamaica;
92        let serialized = serde_json::to_string(&region).expect("Serialize");
93        let deserialized: CentralAmericaRegion = serde_json::from_str(&serialized).expect("Deserialize");
94        assert_eq!(region, deserialized);
95    }
96
97    #[test]
98    fn test_serialize_deserialize_combined() {
99        // Test round-trip for the combined region (HaitiAndDominicanRepublic)
100        let region = CentralAmericaRegion::HaitiAndDominicanRepublic;
101        let serialized = serde_json::to_string(&region).expect("Serialize");
102        let deserialized: CentralAmericaRegion = serde_json::from_str(&serialized).expect("Deserialize");
103        assert_eq!(region, deserialized);
104    }
105}