From 4547b622d8d29df964fa2914213088b148c498fc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:32 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/icu_locid/tests/fixtures/canonicalize.json | 18 ++ .../tests/fixtures/invalid-extensions.json | 112 ++++++++ vendor/icu_locid/tests/fixtures/invalid.json | 85 ++++++ vendor/icu_locid/tests/fixtures/langid.json | 167 ++++++++++++ vendor/icu_locid/tests/fixtures/locale.json | 298 +++++++++++++++++++++ vendor/icu_locid/tests/fixtures/mod.rs | 260 ++++++++++++++++++ vendor/icu_locid/tests/helpers/mod.rs | 15 ++ vendor/icu_locid/tests/langid.rs | 157 +++++++++++ vendor/icu_locid/tests/locale.rs | 122 +++++++++ 9 files changed, 1234 insertions(+) create mode 100644 vendor/icu_locid/tests/fixtures/canonicalize.json create mode 100644 vendor/icu_locid/tests/fixtures/invalid-extensions.json create mode 100644 vendor/icu_locid/tests/fixtures/invalid.json create mode 100644 vendor/icu_locid/tests/fixtures/langid.json create mode 100644 vendor/icu_locid/tests/fixtures/locale.json create mode 100644 vendor/icu_locid/tests/fixtures/mod.rs create mode 100644 vendor/icu_locid/tests/helpers/mod.rs create mode 100644 vendor/icu_locid/tests/langid.rs create mode 100644 vendor/icu_locid/tests/locale.rs (limited to 'vendor/icu_locid/tests') diff --git a/vendor/icu_locid/tests/fixtures/canonicalize.json b/vendor/icu_locid/tests/fixtures/canonicalize.json new file mode 100644 index 000000000..b20e64c4b --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/canonicalize.json @@ -0,0 +1,18 @@ +[ + { + "input": "Pl", + "output": "pl" + }, + { + "input": "eN-uS", + "output": "en-US" + }, + { + "input": "ZH_hans_hK", + "output": "zh-Hans-HK" + }, + { + "input": "en-scouse-fonipa", + "output": "en-fonipa-scouse" + } +] diff --git a/vendor/icu_locid/tests/fixtures/invalid-extensions.json b/vendor/icu_locid/tests/fixtures/invalid-extensions.json new file mode 100644 index 000000000..a5f3a923d --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/invalid-extensions.json @@ -0,0 +1,112 @@ +[ + { + "input": { + "type": "Locale", + "identifier": "pl-US-x-waytoolongkey" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pl-US-x-@A_3" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pl-US-t-h0" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pl-US-t-h0-x-foo" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pl-US-t-h0" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-t-m0" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-t-m0-n0-mixed" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "da-u" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "da-u--" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "da-u-t-latn" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "cmn-hans-cn-u-u" + }, + "output": { + "error": "InvalidExtension", + "text": "Invalid subtag" + } + } +] diff --git a/vendor/icu_locid/tests/fixtures/invalid.json b/vendor/icu_locid/tests/fixtures/invalid.json new file mode 100644 index 000000000..d44007596 --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/invalid.json @@ -0,0 +1,85 @@ +[ + { + "input": "a1a", + "output": { + "error": "InvalidLanguage", + "text": "The given language subtag is invalid" + } + }, + { + "input": "Arab-US", + "output": { + "error": "InvalidLanguage", + "text": "The given language subtag is invalid" + } + }, + { + "input": "", + "output": { + "error": "InvalidLanguage", + "text": "The given language subtag is invalid" + } + }, + { + "input": "pl-DSDAFAFDF", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": "pl-Latn-$1231", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": "pl-Latn-US-$1231", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": "pl-Latn-12", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": "pl-Latn-a12", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": "pl-Latn-US-3_dd", + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pl-Latn-US-variant-h0-hybrid" + }, + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-variant-emodeng-emodeng" + }, + "output": { + "error": "InvalidSubtag", + "text": "Invalid subtag" + } + } +] diff --git a/vendor/icu_locid/tests/fixtures/langid.json b/vendor/icu_locid/tests/fixtures/langid.json new file mode 100644 index 000000000..31740d99a --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/langid.json @@ -0,0 +1,167 @@ +[ + { + "input": "en", + "output": { + "type": "LanguageIdentifier", + "language": "en" + } + }, + { + "input": "lij", + "output": { + "type": "LanguageIdentifier", + "language": "lij" + } + }, + { + "input": "en-Latn", + "output": { + "type": "LanguageIdentifier", + "language": "en", + "script": "Latn" + } + }, + { + "input": "lij-Arab", + "output": { + "type": "LanguageIdentifier", + "language": "lij", + "script": "Arab" + } + }, + { + "input": "en-Latn-US", + "output": { + "type": "LanguageIdentifier", + "language": "en", + "script": "Latn", + "region": "US" + } + }, + { + "input": "lij-Arab-FA", + "output": { + "type": "LanguageIdentifier", + "language": "lij", + "script": "Arab", + "region": "FA" + } + }, + { + "input": "en-Latn-US-windows", + "output": { + "type": "LanguageIdentifier", + "language": "en", + "script": "Latn", + "region": "US", + "variants": ["windows"] + } + }, + { + "input": "lij-Arab-FA-linux", + "output": { + "type": "LanguageIdentifier", + "language": "lij", + "script": "Arab", + "region": "FA", + "variants": ["linux"] + } + }, + { + "input": "lij-Arab-FA-linux-nedis", + "output": { + "type": "LanguageIdentifier", + "language": "lij", + "script": "Arab", + "region": "FA", + "variants": ["linux", "nedis"] + } + }, + { + "input": "EN-latn-us", + "output": { + "type": "LanguageIdentifier", + "language": "en", + "script": "Latn", + "region": "US" + } + }, + { + "input": "sl-nedis", + "output": { + "type": "LanguageIdentifier", + "language": "sl", + "variants": ["nedis"] + } + }, + { + "input": "de-CH-1996", + "output": { + "type": "LanguageIdentifier", + "language": "de", + "region": "CH", + "variants": ["1996"] + } + }, + { + "input": "sr-Latn", + "output": { + "type": "LanguageIdentifier", + "language": "sr", + "script": "Latn" + } + }, + { + "input": "es-419", + "output": { + "type": "LanguageIdentifier", + "language": "es", + "region": "419" + } + }, + { + "input": "und-Latn-US", + "output": { + "type": "LanguageIdentifier", + "script": "Latn", + "region": "US" + } + }, + { + "input": "und", + "output": { + "type": "LanguageIdentifier" + } + }, + { + "input": "und-Latn", + "output": { + "type": "LanguageIdentifier", + "script": "Latn" + } + }, + { + "input": "pl-macos-Windows-nedis-aRabic", + "output": { + "type": "LanguageIdentifier", + "language": "pl", + "variants": ["arabic", "macos", "nedis", "windows"] + } + }, + { + "input": "und-Latn-macos", + "output": { + "type": "LanguageIdentifier", + "script": "Latn", + "variants": ["macos"] + } + }, + { + "input": "und-Latn-312", + "output": { + "type": "LanguageIdentifier", + "script": "Latn", + "region": "312" + } + } +] diff --git a/vendor/icu_locid/tests/fixtures/locale.json b/vendor/icu_locid/tests/fixtures/locale.json new file mode 100644 index 000000000..e87699351 --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/locale.json @@ -0,0 +1,298 @@ +[ + { + "input": { + "type": "Locale", + "identifier": "en-u-hc-h12" + }, + "output": { + "type": "Locale", + "language": "en", + "extensions": { + "unicode": { + "keywords": { + "hc": "h12" + } + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-u-hc-h23" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "unicode": { + "keywords": { + "hc": "h23" + } + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-u-foo" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "unicode": { + "attributes": [ + "foo" + ] + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-u-hc-h23-ca-islamic-civil-ss-true" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "unicode": { + "keywords": { + "hc": "h23", + "ca": "islamic-civil", + "ss": "true" + } + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-t-pl-Latn-DE" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "transform": { + "tlang": "pl-Latn-DE" + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-x-private-foobar" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "private": ["private", "foobar"] + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-t-h0-hybrid-k0-platform-s0-true" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "transform": { + "tfields": { + "h0": "hybrid", + "k0": "platform", + "s0": "true" + } + } + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-t-es-AR-x-foo" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "transform": { + "tlang": "es-AR" + }, + "private": ["foo"] + } + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-US-u-ca-buddhist-hc-h12-t-es-AR-h0-hybrid-x-private-foobar" + }, + "output": { + "type": "Locale", + "language": "en", + "region": "US", + "extensions": { + "unicode": { + "keywords": { + "ca": "buddhist", + "hc": "h12" + } + }, + "transform": { + "tlang": "es-AR", + "tfields": { + "h0": "hybrid" + } + }, + "private": ["private", "foobar"] + } + } + }, + { + "input": { + "type": "Locale", + "language": "es", + "region": "MX", + "extensions": { + "unicode": { + "keywords": { + "ca": "islamic", + "co": "search", + "nu": "roman" + } + } + } + }, + "output": { + "type": "Locale", + "identifier": "es-MX-u-ca-islamic-co-search-nu-roman" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-u-kn" + }, + "output": { + "type": "Locale", + "identifier": "und-u-kn" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-u-kn-ca-calendar" + }, + "output": { + "type": "Locale", + "identifier": "und-u-ca-calendar-kn" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-u-kn-nu-arab" + }, + "output": { + "type": "Locale", + "identifier": "und-u-kn-nu-arab" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-t-m0-true" + }, + "output": { + "type": "Locale", + "identifier": "und-t-m0-true" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-t-m0-true-n0-mixed" + }, + "output": { + "type": "Locale", + "identifier": "und-t-m0-true-n0-mixed" + } + }, + { + "input": { + "type": "Locale", + "identifier": "und-t-m0-true-c0-mixed" + }, + "output": { + "type": "Locale", + "identifier": "und-t-c0-mixed-m0-true" + } + }, + { + "input": { + "type": "Locale", + "identifier": "da-u-ca-gregory-ca-buddhist" + }, + "output": { + "type": "Locale", + "identifier": "da-u-ca-gregory" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pt-u-attr2-attr1-ca-gregory" + }, + "output": { + "type": "Locale", + "identifier": "pt-u-attr1-attr2-ca-gregory" + } + }, + { + "input": { + "type": "Locale", + "identifier": "pt-u-attr1-attr2-attr1-ca-gregory" + }, + "output": { + "type": "Locale", + "identifier": "pt-u-attr1-attr2-ca-gregory" + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-a-not-assigned" + }, + "output": { + "type": "Locale", + "identifier": "en-a-not-assigned" + } + }, + { + "input": { + "type": "Locale", + "identifier": "en-w-bar-u-foo-a-bar-x-u-foo" + }, + "output": { + "type": "Locale", + "identifier": "en-a-bar-u-foo-w-bar-x-u-foo" + } + } +] diff --git a/vendor/icu_locid/tests/fixtures/mod.rs b/vendor/icu_locid/tests/fixtures/mod.rs new file mode 100644 index 000000000..b688632ba --- /dev/null +++ b/vendor/icu_locid/tests/fixtures/mod.rs @@ -0,0 +1,260 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use std::collections::HashMap; +use std::convert::{TryFrom, TryInto}; + +use icu_locid::extensions::private; +use icu_locid::extensions::transform; +use icu_locid::extensions::unicode; +use icu_locid::extensions::Extensions; +use icu_locid::{subtags, LanguageIdentifier, Locale, ParserError}; +use serde::Deserialize; + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleIdentifier { + #[serde(rename = "type")] + pub field_type: String, + pub identifier: String, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleExtensionUnicode { + #[serde(default)] + keywords: HashMap>, + #[serde(default)] + attributes: Vec, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleExtensionTransform { + tlang: Option, + #[serde(default)] + tfields: HashMap>, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleExtensions { + unicode: Option, + transform: Option, + #[serde(default)] + private: Vec, + _other: Option, +} + +impl TryFrom for Extensions { + type Error = ParserError; + + fn try_from(input: LocaleExtensions) -> Result { + let mut ext = Extensions::default(); + if let Some(unicode) = input.unicode { + ext.unicode.keywords = unicode + .keywords + .iter() + .map(|(k, v)| { + ( + unicode::Key::try_from_bytes(k.as_bytes()).expect("Parsing key failed."), + v.as_ref().map_or( + unicode::Value::try_from_bytes(b"").expect("Failed to parse Value"), + |v| { + unicode::Value::try_from_bytes(v.as_bytes()) + .expect("Parsing type failed.") + }, + ), + ) + }) + .collect(); + let v: Vec = unicode + .attributes + .iter() + .map(|v| { + unicode::Attribute::try_from_bytes(v.as_bytes()) + .expect("Parsing attribute failed.") + }) + .collect(); + ext.unicode.attributes = unicode::Attributes::from_vec_unchecked(v); + } + if let Some(transform) = input.transform { + ext.transform.fields = transform + .tfields + .iter() + .map(|(k, v)| { + ( + transform::Key::try_from_bytes(k.as_bytes()).expect("Parsing key failed."), + v.as_ref() + .map(|v| { + transform::Value::try_from_bytes(v.as_bytes()) + .expect("Parsing value failed.") + }) + .expect("Value cannot be empty."), + ) + }) + .collect(); + + if let Some(tlang) = transform.tlang { + ext.transform.lang = Some(tlang.parse().expect("Failed to parse tlang.")); + } + } + let v: Vec = input + .private + .iter() + .map(|v| private::Subtag::try_from_bytes(v.as_bytes()).expect("Failed to add field.")) + .collect(); + ext.private = private::Private::from_vec_unchecked(v); + Ok(ext) + } +} + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleSubtags { + #[serde(rename = "type")] + pub field_type: String, + pub language: Option, + pub script: Option, + pub region: Option, + #[serde(default)] + pub variants: Vec, + pub extensions: Option, +} + +#[derive(Debug, Deserialize, Clone)] +pub struct LocaleError { + pub error: String, + pub text: String, +} + +#[derive(Debug, Deserialize, Clone)] +#[serde(untagged)] +#[allow(clippy::large_enum_variant)] // test code +pub enum LocaleInfo { + String(String), + Error(LocaleError), + Identifier(LocaleIdentifier), + Object(LocaleSubtags), +} + +impl TryFrom for LanguageIdentifier { + type Error = ParserError; + + fn try_from(input: LocaleInfo) -> Result { + match input { + LocaleInfo::String(s) => s.parse(), + LocaleInfo::Error(e) => Err(e.into()), + LocaleInfo::Identifier(ident) => ident.try_into(), + LocaleInfo::Object(o) => o.try_into(), + } + } +} + +impl TryFrom for Locale { + type Error = ParserError; + + fn try_from(input: LocaleInfo) -> Result { + match input { + LocaleInfo::String(s) => s.parse(), + LocaleInfo::Error(e) => Err(e.into()), + LocaleInfo::Identifier(ident) => ident.try_into(), + LocaleInfo::Object(o) => o.try_into(), + } + } +} + +impl TryFrom for LanguageIdentifier { + type Error = ParserError; + + fn try_from(input: LocaleIdentifier) -> Result { + LanguageIdentifier::try_from_locale_bytes(input.identifier.as_bytes()) + } +} + +impl TryFrom for Locale { + type Error = ParserError; + + fn try_from(input: LocaleIdentifier) -> Result { + Locale::try_from_bytes(input.identifier.as_bytes()) + } +} + +impl TryFrom for LanguageIdentifier { + type Error = ParserError; + + fn try_from(subtags: LocaleSubtags) -> Result { + let language = if let Some(lang) = subtags.language { + lang.parse().expect("Failed to parse language subtag") + } else { + subtags::Language::default() + }; + let script = subtags + .script + .map(|s| s.parse().expect("Failed to parse script subtag.")); + let region = subtags + .region + .map(|s| s.parse().expect("Failed to parse region subtag.")); + let variants = subtags + .variants + .iter() + .map(|v| v.parse().expect("Failed to parse variant subtag.")) + .collect::>(); + Ok(LanguageIdentifier { + language, + script, + region, + variants: subtags::Variants::from_vec_unchecked(variants), + }) + } +} + +impl TryFrom for Locale { + type Error = ParserError; + + fn try_from(subtags: LocaleSubtags) -> Result { + let language = if let Some(lang) = subtags.language { + lang.parse().expect("Failed to parse language subtag") + } else { + subtags::Language::default() + }; + let script = subtags + .script + .map(|s| s.parse().expect("Failed to parse script subtag.")); + let region = subtags + .region + .map(|s| s.parse().expect("Failed to parse region subtag.")); + let variants = subtags + .variants + .iter() + .map(|v| v.parse().expect("Failed to parse variant subtag.")) + .collect::>(); + let extensions = if let Some(e) = subtags.extensions { + e.try_into().expect("Failed to parse extensions.") + } else { + Extensions::default() + }; + Ok(Locale { + id: LanguageIdentifier { + language, + script, + region, + variants: subtags::Variants::from_vec_unchecked(variants), + }, + extensions, + }) + } +} + +impl From for ParserError { + fn from(e: LocaleError) -> Self { + match e.error.as_str() { + "InvalidLanguage" => ParserError::InvalidLanguage, + "InvalidSubtag" => ParserError::InvalidSubtag, + "InvalidExtension" => ParserError::InvalidExtension, + _ => unreachable!("Unknown error name"), + } + } +} + +#[derive(Debug, Deserialize)] +pub struct LocaleTest { + pub input: LocaleInfo, + pub output: LocaleInfo, +} diff --git a/vendor/icu_locid/tests/helpers/mod.rs b/vendor/icu_locid/tests/helpers/mod.rs new file mode 100644 index 000000000..d250c510c --- /dev/null +++ b/vendor/icu_locid/tests/helpers/mod.rs @@ -0,0 +1,15 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use std::fs::File; +use std::io::{BufReader, Error}; + +pub fn read_fixture(path: &str) -> Result +where + T: serde::de::DeserializeOwned, +{ + let file = File::open(path)?; + let reader = BufReader::new(file); + Ok(serde_json::from_reader(reader)?) +} diff --git a/vendor/icu_locid/tests/langid.rs b/vendor/icu_locid/tests/langid.rs new file mode 100644 index 000000000..96d022a9b --- /dev/null +++ b/vendor/icu_locid/tests/langid.rs @@ -0,0 +1,157 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +mod fixtures; +mod helpers; + +use std::convert::TryInto; + +use icu_locid::{subtags, LanguageIdentifier, ParserError}; + +type Result = std::result::Result; + +fn test_langid_fixtures(tests: Vec) { + for test in tests { + match test.output { + fixtures::LocaleInfo::String(s) => { + if let fixtures::LocaleInfo::Object(ref o) = &test.input { + if o.field_type == "Locale" { + continue; + } + } + let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); + assert_eq!(input.to_string(), s); + } + fixtures::LocaleInfo::Error(err) => { + let err: ParserError = err.into(); + let input: Result = test.input.try_into(); + assert_eq!(input, Err(err)); + } + fixtures::LocaleInfo::Identifier(ident) => { + let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); + let output: LanguageIdentifier = ident.try_into().expect("Parsing failed."); + assert_eq!(input, output); + } + fixtures::LocaleInfo::Object(o) => { + let input: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); + let output: LanguageIdentifier = o.try_into().expect("Parsing failed."); + assert_eq!(input, output); + } + } + } +} + +#[test] +fn test_langid_parsing() { + let path = "./tests/fixtures/langid.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_langid_invalid() { + let path = "./tests/fixtures/invalid.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_langid_canonicalize() { + let path = "./tests/fixtures/canonicalize.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_langid_from_locale() { + let path = "./tests/fixtures/locale.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_langid_subtag_language() { + let mut lang: subtags::Language = "en".parse().expect("Failed to parse a language."); + assert_eq!(lang.as_str(), "en"); + + lang.clear(); + assert_eq!(lang, subtags::Language::UND); + assert!(lang.is_empty()); + + assert_eq!(lang.to_string(), "und"); +} + +#[test] +fn test_langid_subtag_region() { + let region: subtags::Region = "en".parse().expect("Failed to parse a region."); + assert_eq!(region.as_str(), "EN"); + assert_eq!(region.to_string(), "EN"); +} + +#[test] +fn test_langid_subtag_script() { + let script: subtags::Script = "Latn".parse().expect("Failed to parse a script."); + assert_eq!(script.as_str(), "Latn"); + assert_eq!(script.to_string(), "Latn"); +} + +#[test] +fn test_langid_subtag_variant() { + let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant."); + assert_eq!(variant.as_str(), "macos"); + assert_eq!(variant.to_string(), "macos"); +} + +#[test] +fn test_langid_subtag_variants() { + let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant."); + let mut variants = subtags::Variants::from_vec_unchecked(vec![variant]); + assert_eq!(variants.get(0), Some(&variant)); + variants.clear(); + assert_eq!(variants.len(), 0); +} + +#[test] +fn test_langid_normalizing_eq_str() { + let path = "./tests/fixtures/langid.json"; + let tests: Vec = + helpers::read_fixture(path).expect("Failed to read a fixture"); + for test in tests { + let parsed: LanguageIdentifier = test.input.try_into().expect("Parsing failed."); + assert!(parsed.normalizing_eq(parsed.to_string().as_str())); + } + + // Check that trailing characters are not ignored + let lang: LanguageIdentifier = "en".parse().expect("Parsing failed."); + assert!(!lang.normalizing_eq("en-US")); +} + +#[test] +fn test_langid_strict_cmp() { + let path = "./tests/fixtures/langid.json"; + let tests: Vec = + helpers::read_fixture(path).expect("Failed to read a fixture"); + let bcp47_strings = tests + .iter() + .map(|t| match t.input { + fixtures::LocaleInfo::String(ref s) => s.as_str(), + _ => panic!("Invalid fixture"), + }) + .collect::>(); + for a in bcp47_strings.iter() { + for b in bcp47_strings.iter() { + let a_langid = a + .parse::() + .expect("Invalid BCP-47 in fixture"); + let a_normalized = a_langid.to_string(); + let string_cmp = a_normalized.as_bytes().cmp(b.as_bytes()); + let test_cmp = a_langid.strict_cmp(b.as_bytes()); + assert_eq!(string_cmp, test_cmp, "{:?}/{:?}", a, b); + } + } +} diff --git a/vendor/icu_locid/tests/locale.rs b/vendor/icu_locid/tests/locale.rs new file mode 100644 index 000000000..37c43181e --- /dev/null +++ b/vendor/icu_locid/tests/locale.rs @@ -0,0 +1,122 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +mod fixtures; +mod helpers; + +use std::convert::TryInto; + +use icu_locid::{LanguageIdentifier, Locale, ParserError}; + +type Result = std::result::Result; + +fn test_langid_fixtures(tests: Vec) { + for test in tests { + match test.output { + fixtures::LocaleInfo::String(s) => { + let input: Locale = test.input.try_into().expect("Parsing failed."); + assert_eq!(input.to_string(), s); + } + fixtures::LocaleInfo::Error(err) => { + let err: ParserError = err.into(); + let input: Result = test.input.try_into(); + assert_eq!(input, Err(err)); + } + fixtures::LocaleInfo::Identifier(ident) => { + let input: Locale = test.input.try_into().expect("Parsing failed."); + let output: Locale = ident.clone().try_into().expect("Parsing failed."); + assert_eq!(input, output); + assert_eq!(input.to_string(), ident.identifier); + } + fixtures::LocaleInfo::Object(o) => { + let input: Locale = test.input.try_into().expect("Parsing failed."); + let output: Locale = o.try_into().expect("Parsing failed."); + assert_eq!(input, output); + } + } + } +} + +#[test] +fn test_locale_parsing() { + let path = "./tests/fixtures/locale.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_langid_invalid() { + let path = "./tests/fixtures/invalid-extensions.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[test] +fn test_locale_is_empty() { + let locale: Locale = Locale::default(); + assert!(locale.extensions.is_empty()); + assert_eq!(locale.to_string(), "und".to_string()); +} + +#[test] +fn test_locale_conversions() { + let locale: Locale = Locale::default(); + let langid: LanguageIdentifier = locale.clone().into(); + let locale2: Locale = langid.into(); + assert_eq!(locale, locale2); +} + +#[test] +fn test_locale_canonicalize() { + let locale: Locale = "En-latn-US-MacOS" + .parse() + .expect("Failed to parse a locale."); + assert_eq!( + locale.to_string(), + Locale::canonicalize("eN-latN-uS-macOS").unwrap() + ); +} + +#[test] +fn test_locale_normalizing_eq_str() { + let path = "./tests/fixtures/locale.json"; + let tests: Vec = + helpers::read_fixture(path).expect("Failed to read a fixture"); + for test in tests { + let parsed: Locale = test.input.try_into().expect("Parsing failed."); + assert!(parsed.normalizing_eq(parsed.to_string().as_str())); + } + + // Check that trailing characters are not ignored + let locale: Locale = "en".parse().expect("Parsing failed."); + assert!(!locale.normalizing_eq("en-US")); +} + +#[test] +fn test_locale_strict_cmp() { + let path = "./tests/fixtures/locale.json"; + let tests: Vec = + helpers::read_fixture(path).expect("Failed to read a fixture"); + let bcp47_strings = tests + .iter() + .map(|t| match t.input { + fixtures::LocaleInfo::Identifier(ref s) => s.identifier.as_str(), + _ => match t.output { + fixtures::LocaleInfo::Identifier(ref s) => s.identifier.as_str(), + _ => panic!("No string in fixture input or output: {:?}", t), + }, + }) + .collect::>(); + for a in bcp47_strings.iter() { + for b in bcp47_strings.iter() { + let a_langid = a.parse::().expect("Invalid BCP-47 in fixture"); + let a_normalized = a_langid.to_string(); + let string_cmp = a_normalized.as_bytes().cmp(b.as_bytes()); + let test_cmp = a_langid.strict_cmp(b.as_bytes()); + assert_eq!(string_cmp, test_cmp, "{:?}/{:?}", a, b); + } + } +} -- cgit v1.2.3