Debian Codegraph @

debian/rust-vendor/icu_locale_core/tests/locale.rs

raw
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, Locale, ParseError};
11
12 type Result = std::result::Result<Locale, 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 let input: Locale = test.input.try_into().expect("Parsing failed.");
19 assert_writeable_eq!(input, s);
20 }
21 fixtures::LocaleInfo::Error(err) => {
22 let err: ParseError = err.into();
23 let input: Result = test.input.try_into();
24 assert_eq!(input, Err(err));
25 }
26 fixtures::LocaleInfo::Identifier(ident) => {
27 let input: Locale = test.input.try_into().expect("Parsing failed.");
28 let output: Locale = ident.clone().try_into().expect("Parsing failed.");
29 assert_eq!(input, output);
30 assert_writeable_eq!(input, ident.identifier);
31 }
32 fixtures::LocaleInfo::Object(o) => {
33 let input: Locale = test.input.try_into().expect("Parsing failed.");
34 let output: Locale = o.try_into().expect("Parsing failed.");
35 assert_eq!(input, output);
36 }
37 }
38 }
39 }
40
41 #[test]
42 fn test_locale_parsing() {
43 let data = serde_json::from_str(include_str!("fixtures/locale.json"))
44 .expect("Failed to read a fixture");
45
46 test_langid_fixtures(data);
47 }
48
49 #[test]
50 fn test_locale_invalid() {
51 let data = serde_json::from_str(include_str!("fixtures/invalid-extensions.json"))
52 .expect("Failed to read a fixture");
53
54 test_langid_fixtures(data);
55 }
56
57 #[test]
58 fn test_locale_is_empty() {
59 let locale: Locale = Locale::UNKNOWN;
60 assert!(locale.extensions.is_empty());
61 assert_writeable_eq!(locale, "und");
62 }
63
64 #[test]
65 fn test_locale_conversions() {
66 let locale: Locale = Locale::UNKNOWN;
67 let langid: LanguageIdentifier = locale.clone().into();
68 let locale2: Locale = langid.into();
69 assert_eq!(locale, locale2);
70 }
71
72 #[test]
73 fn test_locale_canonicalize() {
74 let data = serde_json::from_str(include_str!("fixtures/canonicalize.json"))
75 .expect("Failed to read a fixture");
76
77 test_langid_fixtures(data);
78 }
79
80 #[test]
81 fn test_locale_normalizing_eq_str() {
82 let tests: Vec<fixtures::LocaleTest> =
83 serde_json::from_str(include_str!("fixtures/locale.json"))
84 .expect("Failed to read a fixture");
85 for test in tests {
86 let parsed: Locale = test.input.try_into().expect("Parsing failed.");
87 assert!(parsed.normalizing_eq(&parsed.write_to_string()));
88 }
89
90 // Check that trailing characters are not ignored
91 let locale: Locale = "en".parse().expect("Parsing failed.");
92 assert!(!locale.normalizing_eq("en-US"));
93 }
94
95 #[test]
96 fn test_locale_strict_cmp() {
97 let tests: Vec<fixtures::LocaleTest> =
98 serde_json::from_str(include_str!("fixtures/locale.json"))
99 .expect("Failed to read a fixture");
100 let bcp47_strings = tests
101 .iter()
102 .map(|t| match t.input {
103 fixtures::LocaleInfo::Identifier(ref s) => s.identifier.as_str(),
104 _ => match t.output {
105 fixtures::LocaleInfo::Identifier(ref s) => s.identifier.as_str(),
106 _ => panic!("No string in fixture input or output: {t:?}"),
107 },
108 })
109 .collect::<Vec<&str>>();
110 for a in bcp47_strings.iter() {
111 for b in bcp47_strings.iter() {
112 let a_langid = a.parse::<Locale>().expect("Invalid BCP-47 in fixture");
113 let a_normalized = a_langid.write_to_string();
114 let string_cmp = a_normalized.as_bytes().cmp(b.as_bytes());
115 let test_cmp = a_langid.strict_cmp(b.as_bytes());
116 assert_eq!(string_cmp, test_cmp, "{a:?}/{b:?}");
117 }
118 }
119 }
120
121 #[test]
122 fn test_locale_total_cmp() {
123 let l_1_bar = "und-1-bar".parse::<Locale>().unwrap();
124 let l_a_foo = "und-a-foo".parse::<Locale>().unwrap();
125 let l_2_baz = "und-2-baz".parse::<Locale>().unwrap();
126
127 assert_eq!(l_1_bar.total_cmp(&l_a_foo), std::cmp::Ordering::Less);
128 assert_eq!(l_1_bar.total_cmp(&l_2_baz), std::cmp::Ordering::Less);
129 assert_eq!(l_2_baz.total_cmp(&l_a_foo), std::cmp::Ordering::Less);
130
131 let l_en_1 = "en-1-ext-value".parse::<Locale>().unwrap();
132 let l_en_a = "en-a-ext-value".parse::<Locale>().unwrap();
133 assert_eq!(l_en_1.total_cmp(&l_en_a), std::cmp::Ordering::Less);
134 }