Cotton code
109 lines · 5.4 KB · RustRaw
use super::{international_words, normalize_phone, refold_stored_phone};#[test]fn a_number_with_its_country_code_is_e164_without_the_trunk_zero() {    for (raw, want) in [        ("+91 98765 43210", "919876543210"),        ("+91 098765 43210", "919876543210"),        ("0091 98765 43210", "919876543210"),        ("+44 20 7946 0958", "442079460958"),        ("+44 (0)20 7946 0958", "442079460958"),        ("+1 (202) 555-0143", "12025550143"),        ("  +1.202.555.0143  ", "12025550143"),    ] {        assert_eq!(normalize_phone(raw).as_deref(), Some(want), "{raw}");    }}#[test]fn a_number_without_a_country_code_stays_as_typed() {    // No default country: a guess files someone's messages under a stranger.    assert_eq!(normalize_phone("098765 43210").as_deref(), Some("09876543210"));    assert_eq!(normalize_phone("919876543210").as_deref(), Some("919876543210"));    assert_eq!(normalize_phone("020 7946 0958").as_deref(), Some("02079460958"));}#[test]fn what_is_not_a_number_is_none() {    for raw in ["", "100", "x12", "++919876543210", "+91 98765 4321O", "1234567890123456"] {        assert_eq!(normalize_phone(raw), None, "{raw}");    }}#[test]fn a_canonical_number_reads_grouped() {    assert_eq!(international_words("442079460958").as_deref(), Some("+44 20 7946 0958"));    assert_eq!(international_words("919876543210").as_deref(), Some("+91 98765 43210"));    assert_eq!(international_words("12025550143").as_deref(), Some("+1 202-555-0143"));}#[test]fn a_stored_number_folds_only_its_trunk_zero() {    assert_eq!(refold_stored_phone("9109876543210").as_deref(), Some("919876543210"));    assert_eq!(refold_stored_phone("4402079460958").as_deref(), Some("442079460958"));    assert_eq!(refold_stored_phone("00919876543210").as_deref(), Some("919876543210"));    // Already canonical, or a national number that only starts like a code.    for stored in ["919876543210", "442079460958", "12025550143", "09876543210", "3105550143"] {        assert_eq!(refold_stored_phone(stored), None, "{stored}");    }}/// The normaliser before libphonenumber (to 22 Oct 2026): presentation out,/// one leading `+` off, 8–15 digits. Kept here only to prove nothing that/// matched under it stops matching.fn before(raw: &str) -> Option<String> {    let stripped = super::strip_presentation(raw);    let digits = stripped.strip_prefix('+').unwrap_or(&stripped);    ((8..=15).contains(&digits.len()) && digits.bytes().all(|b| b.is_ascii_digit())).then(|| digits.to_string())}/// About forty real-shaped numbers, each group one number in the spellings/// people and exports write: spaces, dashes, dots, brackets, a leading 00, a/// trunk 0 after the code. The last group has no code and is its own number.const SPELLINGS: &[&[&str]] = &[    &["+919876543210", "+91 98765 43210", "+91-98765-43210", "+91 (98765) 43210", "919876543210", "+91 098765 43210", "0091 98765 43210", "+91 0 98765 43210", "00 91 98765 43210"],    &["+44 20 7946 0958", "+44 (0)20 7946 0958", "+44 020 7946 0958", "0044 20 7946 0958", "442079460958", "+44.20.7946.0958", "00 44 (0) 20 7946 0958"],    &["+1 202 555 0143", "+1 (202) 555-0143", "12025550143", "001 202 555 0143", "+1.202.555.0143"],    &["+49 30 12345678", "+49 (0)30 12345678", "0049 30 12345678", "+49 030 12345678"],    &["+61 2 9876 5432", "+61 (0)2 9876 5432", "0061 2 9876 5432", "61298765432"],    &["+33 1 23 45 67 89", "+33 (0)1 23 45 67 89", "0033 1 23 45 67 89", "33123456789"],    &["+44 7911 123456", "+44 (0) 7911 123456", "447911123456", "+44 07911 123456"],    &["098765 43210", "09876543210", "0987-654-3210"],];/// RULE: every pair of spellings that matched under the old normaliser still/// matches, every spelling of one number now matches every other (the trunk-0/// and 00 spellings included), and no two numbers are made one — a wrong match/// files someone's messages under a stranger.#[test]fn every_spelling_of_a_number_matches_and_nothing_that_matched_stops() {    let all: Vec<(usize, &str)> =        SPELLINGS.iter().enumerate().flat_map(|(group, spellings)| spellings.iter().map(move |s| (group, *s))).collect();    assert!(all.len() >= 40, "{} spellings", all.len());    for &(group_a, a) in &all {        let now_a = normalize_phone(a).unwrap_or_else(|| panic!("{a} is a number"));        for &(group_b, b) in &all {            let now_b = normalize_phone(b).unwrap();            if before(a).is_some() && before(a) == before(b) {                assert_eq!(now_a, now_b, "{a} and {b} matched before");            }            assert_eq!(group_a == group_b, now_a == now_b, "{a} ({now_a}) and {b} ({now_b})");        }    }}/// A number stored without its country code — a trunk-0 national/// ("09876543210") or a bare national ("9876543210") — does NOT match the/// same number arriving with its code, and did not under the old normaliser/// either: there is no country to read it in. (Coordinator's question 1, 22/// Oct 2026; what folds them is the region hint, row 4.)#[test]fn a_national_number_stored_without_its_code_matches_neither_before_nor_now() {    let incoming = "+91 98765 43210";    for stored in ["09876543210", "9876543210"] {        assert_ne!(normalize_phone(incoming), normalize_phone(stored), "{stored}");        assert_ne!(before(incoming), before(stored), "{stored}");        // Each still matches its own spelling exactly.        assert_eq!(normalize_phone(stored).as_deref(), Some(stored));    }}