Debian Codegraph @

third_party/rust/icu_locid/tests/langid.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_locid::{subtags, LanguageIdentifier, ParserError};
11
12 type Result = std::result::Result<LanguageIdentifier, ParserError>;
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(ref o) = &test.input {
19 if o.field_type == "Locale" {
20 continue;
21 }
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: ParserError = 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.clear();
83 assert_eq!(lang, subtags::Language::UND);
84 assert!(lang.is_empty());
85
86 assert_writeable_eq!(lang, "und");
87 }
88
89 #[test]
90 fn test_langid_subtag_region() {
91 let region: subtags::Region = "en".parse().expect("Failed to parse a region.");
92 assert_eq!(region.as_str(), "EN");
93 assert_writeable_eq!(region, "EN");
94 }
95
96 #[test]
97 fn test_langid_subtag_script() {
98 let script: subtags::Script = "Latn".parse().expect("Failed to parse a script.");
99 assert_eq!(script.as_str(), "Latn");
100 assert_writeable_eq!(script, "Latn");
101 }
102
103 #[test]
104 fn test_langid_subtag_variant() {
105 let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant.");
106 assert_eq!(variant.as_str(), "macos");
107 assert_writeable_eq!(variant, "macos");
108 }
109
110 #[test]
111 fn test_langid_subtag_variants() {
112 let variant: subtags::Variant = "macos".parse().expect("Failed to parse a variant.");
113 let mut variants = subtags::Variants::from_vec_unchecked(vec![variant]);
114 assert_eq!(variants.first(), Some(&variant));
115 variants.clear();
116 assert_eq!(variants.len(), 0);
117 }
118
119 #[test]
120 fn test_langid_normalizing_eq_str() {
121 let tests: Vec<fixtures::LocaleTest> =
122 serde_json::from_str(include_str!("fixtures/langid.json"))
123 .expect("Failed to read a fixture");
124 for test in tests {
125 let parsed: LanguageIdentifier = test.input.try_into().expect("Parsing failed.");
126 assert!(parsed.normalizing_eq(&parsed.write_to_string()));
127 }
128
129 // Check that trailing characters are not ignored
130 let lang: LanguageIdentifier = "en".parse().expect("Parsing failed.");
131 assert!(!lang.normalizing_eq("en-US"));
132 }
133
134 #[test]
135 fn test_langid_strict_cmp() {
136 let tests: Vec<fixtures::LocaleTest> =
137 serde_json::from_str(include_str!("fixtures/langid.json"))
138 .expect("Failed to read a fixture");
139 let bcp47_strings = tests
140 .iter()
141 .map(|t| match t.input {
142 fixtures::LocaleInfo::String(ref s) => s.as_str(),
143 _ => panic!("Invalid fixture"),
144 })
145 .collect::<Vec<&str>>();
146 for a in bcp47_strings.iter() {
147 for b in bcp47_strings.iter() {
148 let a_langid = a
149 .parse::<LanguageIdentifier>()
150 .expect("Invalid BCP-47 in fixture");
151 let a_normalized = a_langid.write_to_string();
152 let string_cmp = a_normalized.as_bytes().cmp(b.as_bytes());
153 let test_cmp = a_langid.strict_cmp(b.as_bytes());
154 assert_eq!(string_cmp, test_cmp, "{a:?}/{b:?}");
155 }
156 }
157 }