#![forbid(unsafe_code)]
#![deny(clippy::all)]
#[macro_use] mod imports; use imports::*;
x!{abbreviation}
x!{africa}
x!{country}
x!{error}
x!{impl_serde}
#[cfg(test)]
mod africa_tests {
use super::*;
#[test]
fn test_default() {
let def = AfricaRegion::default();
assert_eq!(def, AfricaRegion::Algeria);
}
#[test]
fn test_from_str() {
let parsed = AfricaRegion::from_str("Nigeria").expect("Should parse Nigeria");
assert_eq!(parsed, AfricaRegion::Nigeria);
let parsed2 = AfricaRegion::from_str("Congo (Democratic Republic/Kinshasa)").expect("Should parse Congo DR");
assert_eq!(parsed2, AfricaRegion::CongoDemocraticRepublicKinshasa);
let parsed3 = AfricaRegion::from_str("cHaD").expect("Should parse Chad");
assert_eq!(parsed3, AfricaRegion::Chad);
let err = AfricaRegion::from_str("Atlantis");
assert!(err.is_err(), "Unknown variant should fail");
}
#[test]
fn test_abbreviations() {
assert_eq!(AfricaRegion::Nigeria.abbreviation(), "NG");
assert_eq!(AfricaRegion::Morocco.abbreviation(), "MA");
assert_eq!(AfricaRegion::IvoryCoast.abbreviation(), "CI");
assert_eq!(AfricaRegion::Swaziland.abbreviation(), "SZ");
assert_eq!(AfricaRegion::Tanzania.abbreviation(), "TZ");
assert_eq!(AfricaRegion::SenegalAndGambia.abbreviation(), "SN-GM");
assert_eq!(AfricaRegion::SaintHelenaAscensionTristanDaCunha.abbreviation(), "SH-AC-TA");
assert_eq!(AfricaRegion::CanaryIslands.abbreviation(), "IC");
}
#[test]
fn test_variant_names() {
let variants = AfricaRegion::VARIANTS;
assert!(variants.contains(&"Nigeria"));
assert!(variants.contains(&"Algeria"));
assert!(variants.contains(&"Senegal and Gambia"));
}
#[test]
fn test_africa_region_to_country_success() {
assert_eq!(Country::try_from(AfricaRegion::Nigeria).unwrap(), Country::Nigeria);
assert_eq!(Country::try_from(AfricaRegion::Ghana).unwrap(), Country::Ghana);
assert_eq!(Country::try_from(AfricaRegion::Kenya).unwrap(), Country::Kenya);
assert_eq!(Country::try_from(AfricaRegion::CongoDemocraticRepublicKinshasa).unwrap(), Country::CongoKinshasa);
assert_eq!(Country::try_from(AfricaRegion::SenegalAndGambia).unwrap(), Country::Senegal);
}
#[test]
fn test_africa_region_to_country_errors() {
match Country::try_from(AfricaRegion::CanaryIslands) {
Err(_) => {}
_ => panic!("Expected error for Canary Islands"),
}
match Country::try_from(AfricaRegion::SaintHelenaAscensionTristanDaCunha) {
Err(AfricaRegionConversionError::UnsupportedRegion { .. }) => {}
_ => panic!("Expected UnsupportedRegion for Saint Helena, Ascension, and Tristan da Cunha"),
}
}
#[test]
fn test_country_to_africa_region_success() {
assert_eq!(AfricaRegion::try_from(Country::Nigeria).unwrap(), AfricaRegion::Nigeria);
assert_eq!(AfricaRegion::try_from(Country::Ethiopia).unwrap(), AfricaRegion::Ethiopia);
assert_eq!(AfricaRegion::try_from(Country::Ghana).unwrap(), AfricaRegion::Ghana);
assert_eq!(AfricaRegion::try_from(Country::Gambia).unwrap(), AfricaRegion::SenegalAndGambia);
assert_eq!(AfricaRegion::try_from(Country::Eswatini).unwrap(), AfricaRegion::Swaziland);
}
#[test]
fn test_country_to_africa_region_errors() {
match AfricaRegion::try_from(Country::Brazil) {
Err(AfricaRegionConversionError::NotAfrican { .. }) => {}
_ => panic!("Expected NotAfrican for Brazil"),
}
match AfricaRegion::try_from(Country::USA) {
Err(AfricaRegionConversionError::NotAfrican { .. }) => {}
_ => panic!("Expected NotAfrican for USA"),
}
}
#[test]
fn test_iso_code_conversions() {
let region = AfricaRegion::Egypt;
let alpha2: Iso3166Alpha2 = region.try_into().unwrap();
let alpha3: Iso3166Alpha3 = region.try_into().unwrap();
let code: CountryCode = region.try_into().unwrap();
assert_eq!(alpha2, Iso3166Alpha2::EG);
assert_eq!(alpha3, Iso3166Alpha3::EGY);
match code {
CountryCode::Alpha2(a2) => assert_eq!(a2, Iso3166Alpha2::EG),
_ => panic!("Expected Alpha2 code"),
}
}
#[test]
fn test_serialize_deserialize() {
let region = AfricaRegion::Rwanda;
let serialized = serde_json::to_string(®ion).expect("Serialize");
let deserialized: AfricaRegion = serde_json::from_str(&serialized).expect("Deserialize");
assert_eq!(region, deserialized);
let region2 = AfricaRegion::SenegalAndGambia;
let serialized2 = serde_json::to_string(®ion2).expect("Serialize");
let deserialized2: AfricaRegion = serde_json::from_str(&serialized2).expect("Deserialize");
assert_eq!(region2, deserialized2);
}
#[test]
fn test_round_trip_country_africa_region() {
let region = AfricaRegion::SenegalAndGambia;
let c: Country = region.try_into().unwrap();
assert_eq!(c, Country::Senegal);
let back: AfricaRegion = c.try_into().unwrap();
assert_eq!(back, AfricaRegion::SenegalAndGambia);
let region2 = AfricaRegion::Swaziland;
let c2: Country = region2.try_into().unwrap();
assert_eq!(c2, Country::Eswatini);
let back2: AfricaRegion = c2.try_into().unwrap();
assert_eq!(back2, AfricaRegion::Swaziland);
}
#[test]
fn test_error_conditions_iso_codes() {
match Iso3166Alpha2::try_from(AfricaRegion::CanaryIslands) {
Err(_) => {}
_ => panic!("Expected error for Canary Islands -> ISO codes"),
}
match Iso3166Alpha2::try_from(AfricaRegion::SaintHelenaAscensionTristanDaCunha) {
Err(_) => {}
_ => panic!("Expected error for Saint Helena, Ascension, and Tristan da Cunha -> ISO codes"),
}
}
}