use super::LanguageTag;
use crate::WritingSystem;
#[test]
fn the_reduction_keeps_the_language_and_the_script_and_nothing_else() {
for (tag, language, script) in [
("en", "en", None),
("en-US", "en", None),
("en-Latn-US", "en", Some("Latn")),
("en-Latn-US-posix", "en", Some("Latn")),
("zh", "zh", None),
("zh-Hant", "zh", Some("Hant")),
("zh-Hant-TW", "zh", Some("Hant")),
("de-CH-1901", "de", None),
("sr-Cyrl-RS", "sr", Some("Cyrl")),
("klingon", "klingon", None),
] {
let reduced = LanguageTag::parse(tag).unwrap_or_else(|| panic!("{tag} is well formed"));
assert_eq!(reduced.language(), language, "at {tag}");
assert_eq!(reduced.script_subtag(), script, "at {tag}");
}
}
#[test]
fn an_extlang_is_skipped_and_the_script_after_it_still_reads() {
let reduced = LanguageTag::parse("zh-cmn-Hans-CN").expect("well formed");
assert_eq!(reduced.language(), "zh");
assert_eq!(reduced.script_subtag(), Some("Hans"));
let deep = LanguageTag::parse("zh-aaa-bbb-ccc-Hant").expect("well formed");
assert_eq!(deep.script_subtag(), Some("Hant"));
}
#[test]
fn only_a_short_primary_takes_an_extlang() {
let reduced = LanguageTag::parse("klingon-abc-Latn").expect("well formed");
assert_eq!(reduced.language(), "klingon");
assert_eq!(reduced.script_subtag(), None);
}
#[test]
fn both_separators_read() {
let dashed = LanguageTag::parse("zh-Hant-TW").expect("well formed");
let scored = LanguageTag::parse("zh_Hant_TW").expect("well formed");
assert_eq!(dashed.language(), scored.language());
assert_eq!(dashed.script_subtag(), scored.script_subtag());
}
#[test]
fn a_tag_with_no_readable_primary_subtag_is_refused() {
for tag in [
"",
"x",
"x-private",
"i-klingon",
"1",
"12",
"e",
"toolongsubtag",
"zh1",
"-zh",
] {
assert!(LanguageTag::parse(tag).is_none(), "{tag} should not parse");
}
}
#[test]
fn an_unregistered_subtag_still_parses() {
for tag in ["qaa", "zzz", "sw", "xyz"] {
assert!(LanguageTag::parse(tag).is_some(), "{tag} should parse");
}
}
#[test]
fn a_script_subtag_reads_through_the_segmenters_vocabulary() {
assert_eq!(
LanguageTag::parse("en-Latn-US").unwrap().script(),
Some(WritingSystem::Latn)
);
assert_eq!(LanguageTag::parse("en-US").unwrap().script(), None);
assert_eq!(
LanguageTag::parse("zh-Hant").unwrap().script(),
Some(WritingSystem::new("hant"))
);
assert!(
LanguageTag::parse("zh-Hant")
.unwrap()
.script()
.unwrap()
.is_other()
);
}
#[test]
fn matching_folds_case_and_separators_but_not_the_scripts_presence() {
let traditional = LanguageTag::parse("zh-Hant-TW").expect("well formed");
assert!(traditional.matches("zh-Hant"));
assert!(traditional.matches("zh_hant"));
assert!(traditional.matches("ZH-HANT"));
assert!(
!traditional.matches("zh"),
"an absent script is not a wildcard"
);
assert!(!traditional.matches("zh-Hans"));
assert!(!traditional.matches("ja"));
let plain = LanguageTag::parse("en-GB").expect("well formed");
assert!(plain.matches("en"));
assert!(plain.matches("EN"));
assert!(!plain.matches("en-Latn"));
assert!(!plain.matches("x-private"));
assert!(!plain.matches(""));
}