| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
mod fixtures; |
| 6 |
|
| 7 |
use std::convert::TryInto; |
| 8 |
use writeable::*; |
| 9 |
|
| 10 |
use icu_locale_core::{LanguageIdentifier, ParseError, subtags}; |
| 11 |
|
| 12 |
type Result = std::result::Result<LanguageIdentifier, ParseError>; |
| 13 |
|
| 14 |
fn test_langid_fixtures(tests: Vec<fixtures::LocaleTest>) { |
| 15 |
for test in tests { |
| 16 |
match test.output { |
| 17 |
fixtures::LocaleInfo::String(s) => { |
| 18 |
if let fixtures::LocaleInfo::Object(o) = &test.input |
| 19 |
&& o.field_type == "Locale" |
| 20 |
{ |
| 21 |
continue; |
| 22 |
} |
| 23 |
let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); |
| 24 |
assert_writeable_eq!(input, s); |
| 25 |
} |
| 26 |
fixtures::LocaleInfo::Error(err) => { |
| 27 |
let err: ParseError = err.into(); |
| 28 |
let input: Result = test.input.try_into(); |
| 29 |
assert_eq!(input, Err(err)); |
| 30 |
} |
| 31 |
fixtures::LocaleInfo::Identifier(ident) => { |
| 32 |
let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); |
| 33 |
let output: LanguageIdentifier = ident.try_into().expect("Parsing failed."); |
| 34 |
assert_eq!(input, output); |
| 35 |
} |
| 36 |
fixtures::LocaleInfo::Object(o) => { |
| 37 |
let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); |
| 38 |
let output: LanguageIdentifier = o.try_into().expect("Parsing failed."); |
| 39 |
assert_eq!(input, output); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_langid_parsing() { |
| 47 |
let data = serde_json::from_str(include_str!("fixtures/langid.json")) |
| 48 |
.expect("Failed to read a fixture"); |
| 49 |
|
| 50 |
test_langid_fixtures(data); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_langid_invalid() { |
| 55 |
let data = serde_json::from_str(include_str!("fixtures/invalid.json")) |
| 56 |
.expect("Failed to read a fixture"); |
| 57 |
|
| 58 |
test_langid_fixtures(data); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn test_langid_canonicalize() { |
| 63 |
let data = serde_json::from_str(include_str!("fixtures/canonicalize.json")) |
| 64 |
.expect("Failed to read a fixture"); |
| 65 |
|
| 66 |
test_langid_fixtures(data); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn test_langid_from_locale() { |
| 71 |
let data = serde_json::from_str(include_str!("fixtures/locale.json")) |
| 72 |
.expect("Failed to read a fixture"); |
| 73 |
|
| 74 |
test_langid_fixtures(data); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_langid_subtag_language() { |
| 79 |
let mut lang: subtags::Language = "en".parse().expect("Failed to parse a language."); |
| 80 |
assert_eq!(lang.as_str(), "en"); |
| 81 |
|
| 82 |
lang = subtags::Language::UNKNOWN; |
| 83 |
assert!(lang.is_unknown()); |
| 84 |
|
| 85 |
assert_writeable_eq!(lang, "und"); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_langid_subtag_region() { |
| 90 |
let region: subtags::Region = "en".parse().expect("Failed to parse a region."); |
| 91 |
assert_eq!(region.as_str(), "EN"); |
| 92 |
assert_writeable_eq!(region, "EN"); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn test_langid_subtag_script() { |
| 97 |
let script: subtags::Script = "Latn".parse().expect("Failed to parse a script."); |
| 98 |
assert_eq!(script.as_str(), "Latn"); |
| 99 |
assert_writeable_eq!(script, "Latn"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_langid_subtag_variant() { |
| 104 |
let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant."); |
| 105 |
assert_eq!(variant.as_str(), "macos"); |
| 106 |
assert_writeable_eq!(variant, "macos"); |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_langid_subtag_variants() { |
| 111 |
let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant."); |
| 112 |
let mut variants = subtags::Variants::from_vec_unchecked(vec![variant]); |
| 113 |
assert_eq!(variants.first(), Some(&variant)); |
| 114 |
variants.clear(); |
| 115 |
assert_eq!(variants.len(), 0); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn test_langid_normalizing_eq_str() { |
| 120 |
let tests: Vec<fixtures::LocaleTest> = |
| 121 |
serde_json::from_str(include_str!("fixtures/langid.json")) |
| 122 |
.expect("Failed to read a fixture"); |
| 123 |
for test in tests { |
| 124 |
let parsed: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); |
| 125 |
assert!(parsed.normalizing_eq(&parsed.write_to_string())); |
| 126 |
} |
| 127 |
|
| 128 |
// Check that trailing characters are not ignored |
| 129 |
let lang: LanguageIdentifier = "en".parse().expect("Parsing failed."); |
| 130 |
assert!(!lang.normalizing_eq("en-US")); |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn test_langid_strict_cmp() { |
| 135 |
let tests: Vec<fixtures::LocaleTest> = |
| 136 |
serde_json::from_str(include_str!("fixtures/langid.json")) |
| 137 |
.expect("Failed to read a fixture"); |
| 138 |
let bcp47_strings = tests |
| 139 |
.iter() |
| 140 |
.map(|t| match t.input { |
| 141 |
fixtures::LocaleInfo::String(ref s) => s.as_str(), |
| 142 |
_ => panic!("Invalid fixture"), |
| 143 |
}) |
| 144 |
.collect::<Vec<&str>>(); |
| 145 |
for a in bcp47_strings.iter() { |
| 146 |
for b in bcp47_strings.iter() { |
| 147 |
let a_langid = a |
| 148 |
.parse::<LanguageIdentifier>() |
| 149 |
.expect("Invalid BCP-47 in fixture"); |
| 150 |
let a_normalized = a_langid.write_to_string(); |
| 151 |
let string_cmp = a_normalized.as_bytes().cmp(b.as_bytes()); |
| 152 |
let test_cmp = a_langid.strict_cmp(b.as_bytes()); |
| 153 |
assert_eq!(string_cmp, test_cmp, "{a:?}/{b:?}"); |
| 154 |
} |
| 155 |
} |
| 156 |
} |