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 ).
8 use std
::convert
::TryInto
;
11 use icu_locid
::{LanguageIdentifier, Locale, ParserError}
;
13 type Result
= std
::result
::Result
<Locale
, ParserError
>;
15 fn test_langid_fixtures(tests
: Vec
<fixtures
::LocaleTest
>) {
18 fixtures
::LocaleInfo
::String(s
) => {
19 let input
: Locale
= test
.input
.try_into().expect("Parsing failed.");
20 assert_writeable_eq
!(input
, s
);
22 fixtures
::LocaleInfo
::Error(err
) => {
23 let err
: ParserError
= err
.into();
24 let input
: Result
= test
.input
.try_into();
25 assert_eq
!(input
, Err(err
));
27 fixtures
::LocaleInfo
::Identifier(ident
) => {
28 let input
: Locale
= test
.input
.try_into().expect("Parsing failed.");
29 let output
: Locale
= ident
.clone().try_into().expect("Parsing failed.");
30 assert_eq
!(input
, output
);
31 assert_writeable_eq
!(input
, ident
.identifier
);
33 fixtures
::LocaleInfo
::Object(o
) => {
34 let input
: Locale
= test
.input
.try_into().expect("Parsing failed.");
35 let output
: Locale
= o
.try_into().expect("Parsing failed.");
36 assert_eq
!(input
, output
);
43 fn test_locale_parsing() {
44 let path
= "./tests/fixtures/locale.json";
45 let data
= helpers
::read_fixture(path
).expect("Failed to read a fixture");
47 test_langid_fixtures(data
);
51 fn test_langid_invalid() {
52 let path
= "./tests/fixtures/invalid-extensions.json";
53 let data
= helpers
::read_fixture(path
).expect("Failed to read a fixture");
55 test_langid_fixtures(data
);
59 fn test_locale_is_empty() {
60 let locale
: Locale
= Locale
::default();
61 assert
!(locale
.extensions
.is_empty());
62 assert_writeable_eq
!(locale
, "und");
66 fn test_locale_conversions() {
67 let locale
: Locale
= Locale
::default();
68 let langid
: LanguageIdentifier
= locale
.clone().into();
69 let locale2
: Locale
= langid
.into();
70 assert_eq
!(locale
, locale2
);
74 fn test_locale_canonicalize() {
75 let locale
: Locale
= "En-latn-US-MacOS"
77 .expect("Failed to parse a locale.");
78 assert_writeable_eq
!(locale
, Locale
::canonicalize("eN-latN-uS-macOS").unwrap());
82 fn test_locale_normalizing_eq_str() {
83 let path
= "./tests/fixtures/locale.json";
84 let tests
: Vec
<fixtures
::LocaleTest
> =
85 helpers
::read_fixture(path
).expect("Failed to read a fixture");
87 let parsed
: Locale
= test
.input
.try_into().expect("Parsing failed.");
88 assert
!(parsed
.normalizing_eq(&parsed
.write_to_string()));
91 // Check that trailing characters are not ignored
92 let locale
: Locale
= "en".parse().expect("Parsing failed.");
93 assert
!(!locale
.normalizing_eq("en-US"));
97 fn test_locale_strict_cmp() {
98 let path
= "./tests/fixtures/locale.json";
99 let tests
: Vec
<fixtures
::LocaleTest
> =
100 helpers
::read_fixture(path
).expect("Failed to read a fixture");
101 let bcp47_strings
= tests
103 .map(|t
| match t
.input
{
104 fixtures
::LocaleInfo
::Identifier(ref s
) => s
.identifier
.as_str(),
105 _
=> match t
.output
{
106 fixtures
::LocaleInfo
::Identifier(ref s
) => s
.identifier
.as_str(),
107 _
=> panic
!("No string in fixture input or output: {t:?}"),
110 .collect
::<Vec
<&str>>();
111 for a
in bcp47_strings
.iter() {
112 for b
in bcp47_strings
.iter() {
113 let a_langid
= a
.parse
::<Locale
>().expect("Invalid BCP-47 in fixture");
114 let a_normalized
= a_langid
.write_to_string();
115 let string_cmp
= a_normalized
.as_bytes().cmp(b
.as_bytes());
116 let test_cmp
= a_langid
.strict_cmp(b
.as_bytes());
117 assert_eq
!(string_cmp
, test_cmp
, "{a:?}/{b:?}");