Cotton code
201 lines · 6.8 KB · RustRaw
//! The zone-to-city mapping, exhaustively: every zone the bundled tz database//! knows is asked, and the three rules are held one by one.use std::str::FromStr;use chrono_tz::{Tz, TZ_VARIANTS};use super::*;#[test]fn a_geographic_zone_answers_its_last_part_as_the_city() {    assert_eq!(main_city("Europe/London").as_deref(), Some("London"));    assert_eq!(main_city("Asia/Tokyo").as_deref(), Some("Tokyo"));    assert_eq!(main_city("Africa/Nairobi").as_deref(), Some("Nairobi"));    assert_eq!(main_city("America/Sao_Paulo").as_deref(), Some("Sao Paulo"));    assert_eq!(main_city("Australia/Sydney").as_deref(), Some("Sydney"));    assert_eq!(main_city("Pacific/Auckland").as_deref(), Some("Auckland"));}#[test]fn a_zone_three_parts_deep_answers_the_town_and_never_the_country_between() {    // The last component is the city; the middle one is a country or a state    // and would be the wrong answer if the second part were read.    assert_eq!(        main_city("America/Argentina/Buenos_Aires").as_deref(),        Some("Buenos Aires")    );    assert_eq!(        main_city("America/Indiana/Indianapolis").as_deref(),        Some("Indianapolis")    );    assert_eq!(        main_city("America/Kentucky/Louisville").as_deref(),        Some("Louisville")    );    assert_eq!(        main_city("America/North_Dakota/New_Salem").as_deref(),        Some("New Salem")    );}#[test]fn a_zone_that_names_no_place_answers_no_city() {    for zone in [        "Etc/UTC",        "Etc/GMT",        "Etc/GMT+5",        "Etc/GMT-14",        "Etc/Universal",        "UTC",        "GMT",        "Universal",        "Zulu",        "EST",        "MST",        "HST",        "EST5EDT",        "CST6CDT",        "PST8PDT",        "CET",        "EET",        "WET",        "MET",    ] {        if Tz::from_str(zone).is_err() {            // Not every build carries every legacy name; a zone it does not            // know is covered by the unknown-zone case below.            continue;        }        assert_eq!(main_city(zone), None, "{zone} names no place");    }}#[test]fn a_renamed_zone_answers_the_city_by_its_current_name() {    assert_eq!(main_city("Asia/Calcutta").as_deref(), Some("Kolkata"));    assert_eq!(        main_city("Asia/Saigon").as_deref(),        Some("Ho Chi Minh City")    );    assert_eq!(main_city("Asia/Rangoon").as_deref(), Some("Yangon"));    assert_eq!(main_city("Asia/Katmandu").as_deref(), Some("Kathmandu"));    assert_eq!(main_city("Europe/Kiev").as_deref(), Some("Kyiv"));    assert_eq!(main_city("America/Godthab").as_deref(), Some("Nuuk"));    assert_eq!(        main_city("America/Buenos_Aires").as_deref(),        Some("Buenos Aires")    );}#[test]fn a_non_geographic_spelling_answers_the_city_of_the_zone_it_links_to() {    assert_eq!(main_city("US/Eastern").as_deref(), Some("New York"));    assert_eq!(main_city("US/Pacific").as_deref(), Some("Los Angeles"));    assert_eq!(main_city("GB").as_deref(), Some("London"));    assert_eq!(main_city("Japan").as_deref(), Some("Tokyo"));    assert_eq!(main_city("Brazil/East").as_deref(), Some("Sao Paulo"));    assert_eq!(main_city("NZ").as_deref(), Some("Auckland"));}#[test]fn an_old_name_for_a_different_real_place_keeps_that_place() {    // Merged into another zone for its clock, but still where somebody lives:    // moving them to the zone's namesake would be the wrong city.    assert_eq!(main_city("Asia/Tel_Aviv").as_deref(), Some("Tel Aviv"));    assert_eq!(main_city("America/Montreal").as_deref(), Some("Montreal"));    assert_eq!(main_city("Europe/Belfast").as_deref(), Some("Belfast"));}#[test]fn a_zone_named_after_a_country_or_an_island_answers_its_main_town() {    assert_eq!(main_city("Asia/Qatar").as_deref(), Some("Doha"));    assert_eq!(main_city("Atlantic/Canary").as_deref(), Some("Las Palmas"));    assert_eq!(main_city("Indian/Maldives").as_deref(), Some("Malé"));    assert_eq!(main_city("America/Panama").as_deref(), Some("Panama City"));    assert_eq!(main_city("America/St_Johns").as_deref(), Some("St. John's"));    assert_eq!(main_city("Pacific/Guam").as_deref(), Some("Hagåtña"));}#[test]fn text_that_is_not_a_zone_is_never_guessed_at() {    for text in [        "",        "   ",        "Middle/Earth",        "London",        "europe/london",        "Asia/",        "/",    ] {        assert_eq!(main_city(text), None, "{text:?} is not a zone");    }}#[test]fn surrounding_space_is_not_part_of_the_zone() {    assert_eq!(main_city("  Europe/Lisbon ").as_deref(), Some("Lisbon"));}#[test]fn the_answer_is_the_same_every_time_it_is_asked() {    for variant in TZ_VARIANTS {        let zone = variant.name();        assert_eq!(main_city(zone), main_city(zone), "{zone}");    }}#[test]fn every_zone_the_database_knows_answers_a_readable_city_or_none() {    // Exhaustive over the bundled tz database: a zone in a geographic area    // always has a city, a zone outside one only when it is a spelling the    // renamed table resolves, and no answer ever carries an underscore, a    // slash or surrounding space.    for variant in TZ_VARIANTS {        let zone = variant.name();        let area = current_zone(zone).split('/').next().unwrap_or_default();        let geographic = GEOGRAPHIC_AREAS.contains(&area) && current_zone(zone).contains('/');        match main_city(zone) {            Some(city) => {                assert!(geographic, "{zone} is not a place and answered {city:?}");                assert!(!city.is_empty(), "{zone}");                assert!(!city.contains('_'), "{zone} → {city:?}");                assert!(!city.contains('/'), "{zone} → {city:?}");                assert_eq!(city.trim(), city, "{zone} → {city:?}");            }            None => assert!(!geographic, "{zone} is a place and answered no city"),        }    }}#[test]fn every_row_of_both_tables_names_a_zone_the_database_knows() {    // A table row for a zone that does not exist is a row that can never    // match, and a rename to a zone that does not exist would answer a city    // for a clock nobody can draw.    for (old, now) in RENAMED_ZONES {        assert!(            Tz::from_str(old).is_ok(),            "{old} is not a zone this build knows"        );        assert!(            Tz::from_str(now).is_ok(),            "{now} is not a zone this build knows"        );        assert!(            RENAMED_ZONES.iter().all(|(other, _)| other != now),            "{old} renames to {now}, which is itself renamed"        );    }    for (zone, town) in MAIN_TOWNS {        assert!(            Tz::from_str(zone).is_ok(),            "{zone} is not a zone this build knows"        );        assert!(!town.trim().is_empty(), "{zone}");        assert!(            RENAMED_ZONES.iter().all(|(old, _)| old != zone),            "{zone} is renamed, so its town row can never be reached"        );    }}