Skip to content

Commit 28f125c

Browse files
authored
Add docs tests for SubdivisionId and preferences types (#7347)
Split from #7335
1 parent 844910b commit 28f125c

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

components/locale_core/src/extensions/unicode/subdivision.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl SubdivisionSuffix {
7474
/// subtags::region,
7575
/// };
7676
///
77+
/// // "zzzz" means "unknown subdivision"
7778
/// let ss = subdivision_suffix!("zzzz");
7879
/// let region = region!("gb");
7980
///
@@ -114,6 +115,48 @@ impl SubdivisionId {
114115

115116
/// A constructor which takes a str slice, parses it and
116117
/// produces a well-formed [`SubdivisionId`].
118+
///
119+
/// # Examples
120+
///
121+
/// ```
122+
/// use icu::locale::extensions::unicode::SubdivisionId;
123+
/// use writeable::assert_writeable_eq;
124+
///
125+
/// let subdivision = SubdivisionId::try_from_str("gbeng").unwrap();
126+
///
127+
/// assert_writeable_eq!(subdivision, "gbeng");
128+
/// assert_writeable_eq!(subdivision.region, "GB");
129+
/// assert_writeable_eq!(subdivision.suffix, "eng");
130+
/// ```
131+
///
132+
/// When the value can't be parsed:
133+
///
134+
/// ```
135+
/// use icu::locale::extensions::unicode::SubdivisionId;
136+
/// use icu::locale::ParseError;
137+
///
138+
/// // Value is too short
139+
/// assert!(matches!(
140+
/// SubdivisionId::try_from_str("zz"),
141+
/// Err(ParseError::InvalidExtension),
142+
/// ));
143+
///
144+
/// // Value is too long
145+
/// assert!(matches!(
146+
/// SubdivisionId::try_from_str("abcdefg"),
147+
/// Err(ParseError::InvalidExtension),
148+
/// ));
149+
///
150+
/// // Value does not start with a valid region code
151+
/// assert!(matches!(
152+
/// SubdivisionId::try_from_str("a0zzzz"),
153+
/// Err(ParseError::InvalidExtension),
154+
/// ));
155+
/// assert!(matches!(
156+
/// SubdivisionId::try_from_str("0azzzz"),
157+
/// Err(ParseError::InvalidExtension),
158+
/// ));
159+
/// ```
117160
#[inline]
118161
pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
119162
Self::try_from_utf8(s.as_bytes())

components/locale_core/src/preferences/extensions/unicode/keywords/region_override.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ struct_keyword!(
1010
/// A Region Override specifies an alternate region to use for obtaining certain region-specific default values.
1111
///
1212
/// The valid values are listed in [LDML](https://unicode.org/reports/tr35/#RegionOverride).
13+
///
14+
/// # Examples
15+
///
16+
/// ```
17+
/// use icu::locale::locale;
18+
/// use icu::locale::extensions::unicode::key;
19+
/// use icu::locale::preferences::extensions::unicode::keywords::RegionOverride;
20+
/// use writeable::assert_writeable_eq;
21+
///
22+
/// // American English with British user preferences
23+
/// let locale = locale!("en-US-u-rg-gbzzzz");
24+
///
25+
/// let normal_region = locale.id.region.unwrap();
26+
/// let rg_extension_value = locale.extensions.unicode.keywords.get(&key!("rg")).unwrap();
27+
/// let region_override = RegionOverride::try_from(rg_extension_value.clone()).unwrap();
28+
///
29+
/// assert_writeable_eq!(normal_region, "US");
30+
/// assert_writeable_eq!(region_override.region, "GB");
31+
/// assert_writeable_eq!(region_override.suffix, "zzzz");
32+
/// ```
1333
[Copy]
1434
RegionOverride,
1535
"rg",

components/locale_core/src/preferences/extensions/unicode/keywords/regional_subdivision.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,40 @@ struct_keyword!(
1313
/// A Unicode Subdivision Identifier defines a regional subdivision used for locales.
1414
///
1515
/// The valid values are listed in [LDML](https://unicode.org/reports/tr35/#UnicodeSubdivisionIdentifier).
16+
///
17+
/// # Examples
18+
///
19+
/// ```
20+
/// use icu::locale::locale;
21+
/// use icu::locale::extensions::unicode::key;
22+
/// use icu::locale::preferences::extensions::unicode::keywords::RegionalSubdivision;
23+
/// use writeable::assert_writeable_eq;
24+
///
25+
/// // Texan English
26+
/// let locale = locale!("en-US-u-sd-ustx");
27+
///
28+
/// let normal_region = locale.id.region.unwrap();
29+
/// let sd_extension_value = locale.extensions.unicode.keywords.get(&key!("sd")).unwrap();
30+
/// let regional_subdivision = RegionalSubdivision::try_from(sd_extension_value.clone()).unwrap();
31+
///
32+
/// assert_writeable_eq!(normal_region, "US");
33+
/// assert_writeable_eq!(regional_subdivision.region, "US");
34+
/// assert_writeable_eq!(regional_subdivision.suffix, "tx");
35+
/// ```
36+
///
37+
/// The subdivision "true" is not supported; see [CLDR-19163](https://unicode-org.atlassian.net/browse/CLDR-19163):
38+
///
39+
/// ```
40+
/// use icu::locale::extensions::unicode::value;
41+
/// use icu::locale::preferences::extensions::unicode::keywords::RegionalSubdivision;
42+
/// use icu::locale::preferences::extensions::unicode::errors::PreferencesParseError;
43+
/// use writeable::assert_writeable_eq;
44+
///
45+
/// assert!(matches!(
46+
/// RegionalSubdivision::try_from(value!("true")),
47+
/// Err(PreferencesParseError::InvalidKeywordValue),
48+
/// ));
49+
/// ```
1650
[Copy]
1751
RegionalSubdivision,
1852
"sd",

0 commit comments

Comments
 (0)