Skip to content

Commit ce89e87

Browse files
committed
Add tests to cover the full vs mock cldr coverage
1 parent 6a796e7 commit ce89e87

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

src/negotiate/likely_subtags.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@ static REGION_MATCHING_KEYS: &[&str] = &[
55
];
66

77
pub trait MockLikelySubtags {
8-
fn add_likely_subtags(&mut self) -> bool;
8+
fn add_likely_subtags(&mut self) -> bool;
99
}
1010

1111
impl MockLikelySubtags for LanguageIdentifier {
12-
fn add_likely_subtags(&mut self) -> bool {
13-
let extended = match self.to_string().as_str() {
14-
"en" => "en-Latn-US",
15-
"fr" => "fr-Latn-FR",
16-
"sr" => "sr-Cyrl-SR",
17-
"sr-RU" => "sr-Latn-SR",
18-
"az-IR" => "az-Arab-IR",
19-
"zh-GB" => "zh-Hant-GB",
20-
"zh-US" => "zh-Hant-US",
21-
_ => {
22-
let lang = self.get_language();
12+
fn add_likely_subtags(&mut self) -> bool {
13+
let extended = match self.to_string().as_str() {
14+
"en" => "en-Latn-US",
15+
"fr" => "fr-Latn-FR",
16+
"sr" => "sr-Cyrl-SR",
17+
"sr-RU" => "sr-Latn-SR",
18+
"az-IR" => "az-Arab-IR",
19+
"zh-GB" => "zh-Hant-GB",
20+
"zh-US" => "zh-Hant-US",
21+
_ => {
22+
let lang = self.get_language();
2323

24-
for subtag in REGION_MATCHING_KEYS {
25-
if lang == *subtag {
26-
self.set_region(Some(subtag)).unwrap();
27-
return true;
28-
}
24+
for subtag in REGION_MATCHING_KEYS {
25+
if lang == *subtag {
26+
self.set_region(Some(subtag)).unwrap();
27+
return true;
28+
}
29+
}
30+
return false;
2931
}
30-
return false;
31-
}
32-
};
33-
let langid: LanguageIdentifier = extended.parse().expect("Failed to parse langid.");
34-
self.set_language(Some(langid.get_language())).unwrap();
35-
self.set_script(langid.get_script()).unwrap();
36-
self.set_region(langid.get_region()).unwrap();
37-
return true;
38-
}
32+
};
33+
let langid: LanguageIdentifier = extended.parse().expect("Failed to parse langid.");
34+
self.set_language(Some(langid.get_language())).unwrap();
35+
self.set_script(langid.get_script()).unwrap();
36+
self.set_region(langid.get_region()).unwrap();
37+
return true;
38+
}
3939
}

tests/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ fn langid_matching() {
154154
);
155155
}
156156

157+
#[test]
158+
fn cldr_feature() {
159+
// In this case, the full likelySubtags algorithm knows that `mn` -> `mn-Cyrl`, but
160+
// the mock doesn't.
161+
#[cfg(feature = "cldr")]
162+
assert_eq!(
163+
negotiate_languages(
164+
&[langid!("mn")],
165+
&[langid!("mn-Latn"), langid!("mn-Cyrl")],
166+
None,
167+
NegotiationStrategy::Filtering
168+
),
169+
&[&langid!("mn-Cyrl")]
170+
);
171+
172+
// In result, the mock will just return them in order.
173+
#[cfg(not(feature = "cldr"))]
174+
assert_eq!(
175+
negotiate_languages(
176+
&[langid!("mn")],
177+
&[langid!("mn-Latn"), langid!("mn-Cyrl")],
178+
None,
179+
NegotiationStrategy::Filtering
180+
),
181+
&[&langid!("mn-Latn"), &langid!("mn-Cyrl")]
182+
);
183+
}
184+
157185
#[test]
158186
fn locale_matching() {
159187
let loc_en_us = locale!("en-US-u-hc-h12");

0 commit comments

Comments
 (0)