north_america/
lib.rs

1#![forbid(unsafe_code)]
2#![allow(unused_variables)]
3#![deny(clippy::all)]
4
5#[macro_use] mod imports; use imports::*;
6
7x!{abbreviation}
8x!{from_str}
9x!{canada}
10x!{country}
11x!{error}
12x!{impl_serde}
13x!{north_america}
14x!{from_subregion}
15
16//-------------------------------------------------------------
17// Tests
18//-------------------------------------------------------------
19#[cfg(test)]
20mod test_north_america_region {
21    use super::*;
22    use serde_json;
23    use std::convert::TryFrom;
24    use std::str::FromStr;
25    // Assume USRegion and Country are available from external crates.
26
27    #[test]
28    fn test_default() {
29        // Default should be Canada(Ontario)
30        let def = NorthAmericaRegion::default();
31        assert!(NorthAmericaRegion::Greenland == def);
32    }
33
34    #[test]
35    fn test_from_str() {
36        // Parsing a top-level variant without subregions (e.g., Mexico)
37        let mexico = NorthAmericaRegion::from_str("Mexico").expect("Should parse Mexico");
38        assert_eq!(mexico, NorthAmericaRegion::Mexico);
39    }
40
41    #[test]
42    fn test_abbreviations() {
43        assert_eq!(NorthAmericaRegion::Canada(CanadaRegion::Quebec).abbreviation(), "QC");
44        assert_eq!(NorthAmericaRegion::Greenland.abbreviation(), "GL");
45        assert_eq!(NorthAmericaRegion::UnitedStates(USRegion::default()).abbreviation(), "DC");
46    }
47
48    #[test]
49    fn test_north_america_region_variants() {
50        let variants = NorthAmericaRegion::VARIANTS;
51        assert!(variants.contains(&"Canada"));
52        assert!(variants.contains(&"Greenland"));
53        assert!(variants.contains(&"UnitedStates"));
54        assert!(variants.contains(&"Mexico"));
55    }
56
57    #[test]
58    fn test_north_america_region_to_country() {
59        assert_eq!(Country::try_from(NorthAmericaRegion::Canada(CanadaRegion::Alberta)).unwrap(), Country::Canada);
60        assert_eq!(Country::try_from(NorthAmericaRegion::Greenland).unwrap(), Country::Denmark);
61        assert_eq!(Country::try_from(NorthAmericaRegion::Mexico).unwrap(), Country::Mexico);
62        assert_eq!(Country::try_from(NorthAmericaRegion::UnitedStates(USRegion::default())).unwrap(), Country::USA);
63    }
64
65    #[test]
66    fn test_country_to_north_america_region() {
67        assert_eq!(NorthAmericaRegion::try_from(Country::Canada).unwrap(), NorthAmericaRegion::Canada(CanadaRegion::default()));
68        assert_eq!(NorthAmericaRegion::try_from(Country::Denmark).unwrap(), NorthAmericaRegion::Greenland);
69        assert_eq!(NorthAmericaRegion::try_from(Country::Mexico).unwrap(), NorthAmericaRegion::Mexico);
70        assert_eq!(NorthAmericaRegion::try_from(Country::USA).unwrap(), NorthAmericaRegion::UnitedStates(USRegion::default()));
71    }
72
73    #[test]
74    fn test_country_to_north_america_region_errors() {
75        // A non-North American country:
76        match NorthAmericaRegion::try_from(Country::France) {
77            Err(NorthAmericaRegionConversionError::NotNorthAmerican { .. }) => {}
78            _ => panic!("Expected NotNorthAmerican for France"),
79        }
80    }
81
82    #[test]
83    fn test_iso_code_conversions() {
84        let region = NorthAmericaRegion::Canada(CanadaRegion::NovaScotia);
85        let alpha2: Iso3166Alpha2 = region.try_into().unwrap();
86        let alpha3: Iso3166Alpha3 = region.try_into().unwrap();
87        let code: CountryCode = region.try_into().unwrap();
88
89        assert_eq!(alpha2, Iso3166Alpha2::CA);
90        assert_eq!(alpha3, Iso3166Alpha3::CAN);
91        match code {
92            CountryCode::Alpha2(a2) => assert_eq!(a2, Iso3166Alpha2::CA),
93            _ => panic!("Expected Alpha2 code"),
94        }
95    }
96
97    #[test]
98    fn test_serialize_deserialize_non_subdivided() {
99        // Greenland (non-subdivided)
100        let region = NorthAmericaRegion::Greenland;
101        let serialized = serde_json::to_string(&region).expect("Serialize");
102        let deserialized: NorthAmericaRegion = serde_json::from_str(&serialized).expect("Deserialize");
103        assert_eq!(region, deserialized);
104    }
105
106    #[test]
107    fn test_serialize_deserialize_subdivided() {
108        // Canada with a subregion
109        let region = NorthAmericaRegion::Canada(CanadaRegion::Quebec);
110        let serialized = serde_json::to_string(&region).expect("Serialize");
111        let deserialized: NorthAmericaRegion = serde_json::from_str(&serialized).expect("Deserialize");
112        assert_eq!(region, deserialized);
113
114        // USA with a subregion
115        // Assume USRegion::California exists and can be parsed if part of USRegion enum
116        // For demonstration, we use USRegion::default().
117        let region2 = NorthAmericaRegion::UnitedStates(USRegion::default());
118        let serialized2 = serde_json::to_string(&region2).expect("Serialize");
119        let deserialized2: NorthAmericaRegion = serde_json::from_str(&serialized2).expect("Deserialize");
120        assert_eq!(region2, deserialized2);
121    }
122}