]> git.proxmox.com Git - rustc.git/blob - vendor/icu_locid/tests/locale.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / vendor / icu_locid / tests / locale.rs
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 mod helpers;
7
8 use std::convert::TryInto;
9 use writeable::*;
10
11 use icu_locid::{LanguageIdentifier, Locale, ParserError};
12
13 type Result = std::result::Result<Locale, ParserError>;
14
15 fn test_langid_fixtures(tests: Vec<fixtures::LocaleTest>) {
16 for test in tests {
17 match test.output {
18 fixtures::LocaleInfo::String(s) => {
19 let input: Locale = test.input.try_into().expect("Parsing failed.");
20 assert_writeable_eq!(input, s);
21 }
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));
26 }
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);
32 }
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);
37 }
38 }
39 }
40 }
41
42 #[test]
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");
46
47 test_langid_fixtures(data);
48 }
49
50 #[test]
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");
54
55 test_langid_fixtures(data);
56 }
57
58 #[test]
59 fn test_locale_is_empty() {
60 let locale: Locale = Locale::default();
61 assert!(locale.extensions.is_empty());
62 assert_writeable_eq!(locale, "und");
63 }
64
65 #[test]
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);
71 }
72
73 #[test]
74 fn test_locale_canonicalize() {
75 let locale: Locale = "En-latn-US-MacOS"
76 .parse()
77 .expect("Failed to parse a locale.");
78 assert_writeable_eq!(locale, Locale::canonicalize("eN-latN-uS-macOS").unwrap());
79 }
80
81 #[test]
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");
86 for test in tests {
87 let parsed: Locale = test.input.try_into().expect("Parsing failed.");
88 assert!(parsed.normalizing_eq(&parsed.write_to_string()));
89 }
90
91 // Check that trailing characters are not ignored
92 let locale: Locale = "en".parse().expect("Parsing failed.");
93 assert!(!locale.normalizing_eq("en-US"));
94 }
95
96 #[test]
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
102 .iter()
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:?}"),
108 },
109 })
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:?}");
118 }
119 }
120 }