From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/icu_locid/tests/locale.rs | 120 +++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 third_party/rust/icu_locid/tests/locale.rs (limited to 'third_party/rust/icu_locid/tests/locale.rs') diff --git a/third_party/rust/icu_locid/tests/locale.rs b/third_party/rust/icu_locid/tests/locale.rs new file mode 100644 index 0000000000..638db41383 --- /dev/null +++ b/third_party/rust/icu_locid/tests/locale.rs @@ -0,0 +1,120 @@ +// 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 writeable::*; + +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_writeable_eq!(input, 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_writeable_eq!(input, 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_writeable_eq!(locale, "und"); +} + +#[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 path = "./tests/fixtures/canonicalize.json"; + let data = helpers::read_fixture(path).expect("Failed to read a fixture"); + + test_langid_fixtures(data); +} + +#[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.write_to_string())); + } + + // 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.write_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