| 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 patterns; |
| 7 |
|
| 8 |
use fixtures::TestOutputItem; |
| 9 |
use icu_calendar::AnyCalendarKind; |
| 10 |
use icu_calendar::cal::*; |
| 11 |
use icu_datetime::fieldsets::enums::*; |
| 12 |
use icu_datetime::scaffold::CldrCalendar; |
| 13 |
use icu_datetime::{ |
| 14 |
DateTimeFormatter, FixedCalendarDateTimeFormatter, pattern::DateTimePattern, |
| 15 |
pattern::FixedCalendarDateTimeNames, |
| 16 |
}; |
| 17 |
use icu_datetime::{ |
| 18 |
DateTimeFormatterPreferences, |
| 19 |
preferences::{CalendarAlgorithm, HijriCalendarAlgorithm}, |
| 20 |
}; |
| 21 |
use icu_locale_core::{Locale, locale}; |
| 22 |
use icu_provider::prelude::*; |
| 23 |
use icu_time::{ |
| 24 |
DateTime, TimeZoneInfo, ZonedDateTime, |
| 25 |
zone::{IanaParser, UtcOffset}, |
| 26 |
}; |
| 27 |
use patterns::{ |
| 28 |
dayperiods::{DayPeriodExpectation, DayPeriodTests}, |
| 29 |
time_zones::TimeZoneTests, |
| 30 |
}; |
| 31 |
use writeable::{assert_try_writeable_eq, assert_writeable_eq}; |
| 32 |
|
| 33 |
fn test_fixture(fixture_name: &str, file: &str) { |
| 34 |
for fx in serde_json::from_str::<fixtures::Fixture>(file) |
| 35 |
.expect("Unable to get fixture.") |
| 36 |
.0 |
| 37 |
{ |
| 38 |
let field_set = match fx.input.options.semantic { |
| 39 |
Some(semantic) => semantic.build_composite_datetime().unwrap(), |
| 40 |
None => { |
| 41 |
eprintln!("Warning: Skipping test with no semantic skeleton: {fx:?}"); |
| 42 |
continue; |
| 43 |
} |
| 44 |
}; |
| 45 |
let input = &fx.input.value; |
| 46 |
|
| 47 |
let description = fx |
| 48 |
.description |
| 49 |
.map(|d| format!("\n test: {d:?}\n file: {fixture_name}.json\n")) |
| 50 |
.unwrap_or_else(|| format!("\n file: {fixture_name}.json\n")); |
| 51 |
|
| 52 |
for (locale, expected) in fx.output.values { |
| 53 |
let locale = |
| 54 |
Locale::try_from_str(&locale).expect("Expected parseable locale in fixture"); |
| 55 |
let prefs = DateTimeFormatterPreferences::from(&locale); |
| 56 |
|
| 57 |
match prefs |
| 58 |
.calendar_algorithm |
| 59 |
.unwrap_or(CalendarAlgorithm::Gregory) |
| 60 |
{ |
| 61 |
CalendarAlgorithm::Buddhist => assert_fixture_element( |
| 62 |
prefs, |
| 63 |
Buddhist, |
| 64 |
input, |
| 65 |
&expected, |
| 66 |
field_set, |
| 67 |
&description, |
| 68 |
), |
| 69 |
CalendarAlgorithm::Chinese => assert_fixture_element( |
| 70 |
prefs, |
| 71 |
ChineseTraditional::new(), |
| 72 |
input, |
| 73 |
&expected, |
| 74 |
field_set, |
| 75 |
&description, |
| 76 |
), |
| 77 |
CalendarAlgorithm::Coptic => { |
| 78 |
assert_fixture_element(prefs, Coptic, input, &expected, field_set, &description) |
| 79 |
} |
| 80 |
CalendarAlgorithm::Dangi => assert_fixture_element( |
| 81 |
prefs, |
| 82 |
KoreanTraditional::new(), |
| 83 |
input, |
| 84 |
&expected, |
| 85 |
field_set, |
| 86 |
&description, |
| 87 |
), |
| 88 |
CalendarAlgorithm::Ethiopic => assert_fixture_element( |
| 89 |
prefs, |
| 90 |
Ethiopian::new(), |
| 91 |
input, |
| 92 |
&expected, |
| 93 |
field_set, |
| 94 |
&description, |
| 95 |
), |
| 96 |
CalendarAlgorithm::Ethioaa => assert_fixture_element( |
| 97 |
prefs, |
| 98 |
Ethiopian::new_with_era_style(EthiopianEraStyle::AmeteAlem), |
| 99 |
input, |
| 100 |
&expected, |
| 101 |
field_set, |
| 102 |
&description, |
| 103 |
), |
| 104 |
CalendarAlgorithm::Gregory => assert_fixture_element( |
| 105 |
prefs, |
| 106 |
Gregorian, |
| 107 |
input, |
| 108 |
&expected, |
| 109 |
field_set, |
| 110 |
&description, |
| 111 |
), |
| 112 |
CalendarAlgorithm::Hebrew => { |
| 113 |
assert_fixture_element(prefs, Hebrew, input, &expected, field_set, &description) |
| 114 |
} |
| 115 |
CalendarAlgorithm::Indian => { |
| 116 |
assert_fixture_element(prefs, Indian, input, &expected, field_set, &description) |
| 117 |
} |
| 118 |
CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Civil)) => { |
| 119 |
assert_fixture_element( |
| 120 |
prefs, |
| 121 |
Hijri::new_tabular( |
| 122 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 123 |
hijri::TabularAlgorithmEpoch::Friday, |
| 124 |
), |
| 125 |
input, |
| 126 |
&expected, |
| 127 |
field_set, |
| 128 |
&description, |
| 129 |
) |
| 130 |
} |
| 131 |
CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Tbla)) => { |
| 132 |
assert_fixture_element( |
| 133 |
prefs, |
| 134 |
Hijri::new_tabular( |
| 135 |
hijri::TabularAlgorithmLeapYears::TypeII, |
| 136 |
hijri::TabularAlgorithmEpoch::Thursday, |
| 137 |
), |
| 138 |
input, |
| 139 |
&expected, |
| 140 |
field_set, |
| 141 |
&description, |
| 142 |
) |
| 143 |
} |
| 144 |
CalendarAlgorithm::Hijri(Some(HijriCalendarAlgorithm::Umalqura)) => { |
| 145 |
assert_fixture_element( |
| 146 |
prefs, |
| 147 |
Hijri::new_umm_al_qura(), |
| 148 |
input, |
| 149 |
&expected, |
| 150 |
field_set, |
| 151 |
&description, |
| 152 |
) |
| 153 |
} |
| 154 |
CalendarAlgorithm::Japanese => assert_fixture_element( |
| 155 |
prefs, |
| 156 |
Japanese::new(), |
| 157 |
input, |
| 158 |
&expected, |
| 159 |
field_set, |
| 160 |
&description, |
| 161 |
), |
| 162 |
CalendarAlgorithm::Persian => assert_fixture_element( |
| 163 |
prefs, |
| 164 |
Persian, |
| 165 |
input, |
| 166 |
&expected, |
| 167 |
field_set, |
| 168 |
&description, |
| 169 |
), |
| 170 |
CalendarAlgorithm::Roc => { |
| 171 |
assert_fixture_element(prefs, Roc, input, &expected, field_set, &description) |
| 172 |
} |
| 173 |
_ => panic!("datetime test does not support locale {prefs:?}"), |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
fn assert_fixture_element<C>( |
| 180 |
prefs: DateTimeFormatterPreferences, |
| 181 |
calendar: C, |
| 182 |
input: &str, |
| 183 |
expected: &TestOutputItem, |
| 184 |
field_set: CompositeDateTimeFieldSet, |
| 185 |
description: &str, |
| 186 |
) where |
| 187 |
C: icu_calendar::Calendar + CldrCalendar + icu_calendar::IntoAnyCalendar + Clone, |
| 188 |
icu_datetime::provider::Baked: DataProvider<<C as CldrCalendar>::YearNamesV1>, |
| 189 |
icu_datetime::provider::Baked: DataProvider<<C as CldrCalendar>::MonthNamesV1>, |
| 190 |
icu_datetime::provider::Baked: DataProvider<<C as CldrCalendar>::SkeletaV1>, |
| 191 |
{ |
| 192 |
let iso_input = DateTime::try_from_str(input, Iso).unwrap(); |
| 193 |
let input = DateTime::try_from_str(input, calendar).unwrap(); |
| 194 |
|
| 195 |
let input = ZonedDateTime { |
| 196 |
date: input.date.clone(), |
| 197 |
time: input.time, |
| 198 |
zone: TimeZoneInfo::utc(), |
| 199 |
}; |
| 200 |
let any_input = ZonedDateTime { |
| 201 |
date: input.date.clone().to_any(), |
| 202 |
time: input.time, |
| 203 |
zone: TimeZoneInfo::utc(), |
| 204 |
}; |
| 205 |
let iso_any_input = ZonedDateTime { |
| 206 |
date: iso_input.date.to_any(), |
| 207 |
time: iso_input.time, |
| 208 |
zone: TimeZoneInfo::utc(), |
| 209 |
}; |
| 210 |
|
| 211 |
let dtf = FixedCalendarDateTimeFormatter::try_new(prefs, field_set).expect(description); |
| 212 |
let any_dtf = DateTimeFormatter::try_new(prefs, field_set).expect(description); |
| 213 |
|
| 214 |
let output = dtf.format(&input); |
| 215 |
assert_writeable_eq!(output, expected.expectation(), "{}", description); |
| 216 |
let pattern = output.pattern(); |
| 217 |
|
| 218 |
if let Some(expected_pattern) = expected.pattern() { |
| 219 |
assert_writeable_eq!(pattern, expected_pattern); |
| 220 |
} |
| 221 |
|
| 222 |
if matches!( |
| 223 |
input.date.calendar().kind(), |
| 224 |
AnyCalendarKind::HijriSimulatedMecca |
| 225 |
) { |
| 226 |
// Not supported with FormattableAnyCalendar |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
let any_output = any_dtf.format_same_calendar(&any_input).unwrap(); |
| 231 |
assert_writeable_eq!( |
| 232 |
any_output, |
| 233 |
expected.expectation(), |
| 234 |
"(DateTimeFormatter) {}", |
| 235 |
description |
| 236 |
); |
| 237 |
assert_eq!(pattern, any_output.pattern()); |
| 238 |
|
| 239 |
let iso_any_output = any_dtf.format(&iso_any_input); |
| 240 |
assert_writeable_eq!( |
| 241 |
iso_any_output, |
| 242 |
expected.expectation(), |
| 243 |
"(DateTimeFormatter iso conversion) {}", |
| 244 |
description |
| 245 |
); |
| 246 |
assert_eq!(pattern, iso_any_output.pattern()); |
| 247 |
} |
| 248 |
|
| 249 |
fn test_fixture_with_time_zones(fixture_name: &str, file: &str) { |
| 250 |
for fx in serde_json::from_str::<fixtures::Fixture>(file) |
| 251 |
.expect("Unable to get fixture.") |
| 252 |
.0 |
| 253 |
{ |
| 254 |
let fset = match fx.input.options.semantic { |
| 255 |
Some(semantic) => semantic.build_composite().unwrap(), |
| 256 |
None => { |
| 257 |
eprintln!("Warning: Skipping test with no semantic skeleton: {fx:?}"); |
| 258 |
continue; |
| 259 |
} |
| 260 |
}; |
| 261 |
|
| 262 |
let zoned_datetime = |
| 263 |
ZonedDateTime::try_lenient_from_str(&fx.input.value, Gregorian, IanaParser::new()) |
| 264 |
.expect(&fx.input.value); |
| 265 |
|
| 266 |
let description = match fx.description { |
| 267 |
Some(description) => { |
| 268 |
format!("\n test: {description:?}\n file: {fixture_name}.json\n") |
| 269 |
} |
| 270 |
None => format!("\n file: {fixture_name}.json\n"), |
| 271 |
}; |
| 272 |
for (locale, output_value) in fx.output.values { |
| 273 |
let locale: Locale = locale.parse().unwrap(); |
| 274 |
let dtf = { |
| 275 |
FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(locale.into(), fset) |
| 276 |
.unwrap() |
| 277 |
}; |
| 278 |
assert_writeable_eq!( |
| 279 |
dtf.format(&zoned_datetime), |
| 280 |
output_value.expectation(), |
| 281 |
"{}", |
| 282 |
description, |
| 283 |
); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn test_dayperiod_patterns() { |
| 290 |
for test in |
| 291 |
serde_json::from_str::<DayPeriodTests>(include_str!("patterns/tests/dayperiods.json")) |
| 292 |
.unwrap() |
| 293 |
.0 |
| 294 |
{ |
| 295 |
let locale: Locale = test.locale.parse().unwrap(); |
| 296 |
for test_case in &test.test_cases { |
| 297 |
for dt_input in &test_case.datetimes { |
| 298 |
let datetime = DateTime::try_from_str(dt_input, Gregorian).unwrap(); |
| 299 |
for DayPeriodExpectation { patterns, expected } in &test_case.expectations { |
| 300 |
for pattern_input in patterns { |
| 301 |
let parsed_pattern = |
| 302 |
DateTimePattern::try_from_pattern_str(pattern_input).unwrap(); |
| 303 |
let mut pattern_formatter = FixedCalendarDateTimeNames::< |
| 304 |
Gregorian, |
| 305 |
CompositeDateTimeFieldSet, |
| 306 |
>::try_new( |
| 307 |
(&locale).into() |
| 308 |
) |
| 309 |
.unwrap(); |
| 310 |
let formatted_datetime = pattern_formatter |
| 311 |
.include_for_pattern(&parsed_pattern) |
| 312 |
.unwrap() |
| 313 |
.format(&datetime); |
| 314 |
assert_try_writeable_eq!( |
| 315 |
formatted_datetime, |
| 316 |
*expected, |
| 317 |
Ok(()), |
| 318 |
"\n\ |
| 319 |
locale: `{}`,\n\ |
| 320 |
datetime: `{}`,\n\ |
| 321 |
pattern: `{}`", |
| 322 |
locale, |
| 323 |
dt_input, |
| 324 |
pattern_input, |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
#[test] |
| 334 |
fn test_time_zone_format_configs() { |
| 335 |
for test in |
| 336 |
serde_json::from_str::<TimeZoneTests>(include_str!("patterns/tests/time_zones.json")) |
| 337 |
.unwrap() |
| 338 |
.0 |
| 339 |
{ |
| 340 |
let prefs: DateTimeFormatterPreferences = test.locale.parse::<Locale>().unwrap().into(); |
| 341 |
let zoned_datetime = |
| 342 |
ZonedDateTime::try_lenient_from_str(&test.datetime, Gregorian, IanaParser::new()) |
| 343 |
.expect(&test.datetime); |
| 344 |
for (pattern_input, expect) in &test.expectations { |
| 345 |
let Some(skeleton) = patterns::time_zones::pattern_to_semantic_skeleton(pattern_input) |
| 346 |
else { |
| 347 |
continue; |
| 348 |
}; |
| 349 |
let tzf = |
| 350 |
FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(prefs, skeleton).unwrap(); |
| 351 |
assert_writeable_eq!( |
| 352 |
tzf.format(&zoned_datetime.zone), |
| 353 |
*expect, |
| 354 |
"\n\ |
| 355 |
prefs: `{:?}`,\n\ |
| 356 |
datetime: `{}`,\n\ |
| 357 |
config: `{:?}`,\n |
| 358 |
", |
| 359 |
prefs, |
| 360 |
test.datetime, |
| 361 |
pattern_input, |
| 362 |
); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
#[test] |
| 368 |
fn test_time_zone_format_offset_seconds() { |
| 369 |
use icu_datetime::fieldsets::zone::LocalizedOffsetLong; |
| 370 |
|
| 371 |
let tzf = |
| 372 |
FixedCalendarDateTimeFormatter::<(), _>::try_new(locale!("en").into(), LocalizedOffsetLong) |
| 373 |
.unwrap(); |
| 374 |
assert_writeable_eq!( |
| 375 |
tzf.format(&UtcOffset::try_from_seconds(12).unwrap()), |
| 376 |
"GMT+00:00:12", |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn test_time_zone_format_offset_fallback() { |
| 382 |
use icu_datetime::fieldsets::zone::LocalizedOffsetLong; |
| 383 |
|
| 384 |
let tzf = |
| 385 |
FixedCalendarDateTimeFormatter::<(), _>::try_new(locale!("en").into(), LocalizedOffsetLong) |
| 386 |
.unwrap(); |
| 387 |
assert_writeable_eq!( |
| 388 |
tzf.format( |
| 389 |
&IanaParser::new() |
| 390 |
.parse("America/Los_Angeles") |
| 391 |
.with_offset(None) |
| 392 |
), |
| 393 |
"GMT+?", |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn test_time_zone_patterns() { |
| 399 |
for test in |
| 400 |
serde_json::from_str::<TimeZoneTests>(include_str!("patterns/tests/time_zones.json")) |
| 401 |
.unwrap() |
| 402 |
.0 |
| 403 |
{ |
| 404 |
let prefs: DateTimeFormatterPreferences = test.locale.parse::<Locale>().unwrap().into(); |
| 405 |
let zoned_datetime = |
| 406 |
ZonedDateTime::try_lenient_from_str(&test.datetime, Gregorian, IanaParser::new()) |
| 407 |
.expect(&test.datetime); |
| 408 |
|
| 409 |
for (pattern_input, expect) in &test.expectations { |
| 410 |
let parsed_pattern = DateTimePattern::try_from_pattern_str(pattern_input).unwrap(); |
| 411 |
let mut pattern_formatter = |
| 412 |
FixedCalendarDateTimeNames::<Gregorian, ZoneFieldSet>::try_new(prefs).unwrap(); |
| 413 |
let formatted_datetime = pattern_formatter |
| 414 |
.include_for_pattern(&parsed_pattern) |
| 415 |
.unwrap() |
| 416 |
.format(&zoned_datetime); |
| 417 |
assert_writeable_eq!( |
| 418 |
writeable::adapters::LossyWrap(formatted_datetime), |
| 419 |
*expect, |
| 420 |
"\n\ |
| 421 |
prefs: `{:?}`,\n\ |
| 422 |
datetime: `{}`,\n\ |
| 423 |
pattern: `{}`", |
| 424 |
prefs, |
| 425 |
test.datetime, |
| 426 |
pattern_input, |
| 427 |
); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn test_length_fixtures() { |
| 434 |
test_fixture("lengths", include_str!("fixtures/tests/lengths.json")); |
| 435 |
test_fixture_with_time_zones( |
| 436 |
"lengths_with_zones", |
| 437 |
include_str!("fixtures/tests/lengths_with_zones.json"), |
| 438 |
); |
| 439 |
test_fixture_with_time_zones( |
| 440 |
"lengths_with_zones_from_pdt", |
| 441 |
include_str!("fixtures/tests/lengths_with_zones_from_pdt.json"), |
| 442 |
); |
| 443 |
} |
| 444 |
|
| 445 |
#[test] |
| 446 |
fn test_japanese() { |
| 447 |
test_fixture("japanese", include_str!("fixtures/tests/japanese.json")); |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn test_lengths_with_preferences() { |
| 452 |
test_fixture( |
| 453 |
"lengths_with_preferences", |
| 454 |
include_str!("fixtures/tests/lengths_with_preferences.json"), |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
/// Tests simple components. |
| 459 |
#[test] |
| 460 |
fn test_components() { |
| 461 |
test_fixture("components", include_str!("fixtures/tests/components.json")); |
| 462 |
} |
| 463 |
|
| 464 |
/// Tests components configurations that have exact matches to CLDR skeletons. |
| 465 |
#[test] |
| 466 |
fn test_components_exact_matches() { |
| 467 |
test_fixture( |
| 468 |
"components-exact-matches", |
| 469 |
include_str!("fixtures/tests/components-exact-matches.json"), |
| 470 |
); |
| 471 |
} |
| 472 |
|
| 473 |
#[test] |
| 474 |
fn test_components_hour_cycle() { |
| 475 |
test_fixture( |
| 476 |
"components_hour_cycle", |
| 477 |
include_str!("fixtures/tests/components_hour_cycle.json"), |
| 478 |
); |
| 479 |
} |
| 480 |
|
| 481 |
/// Tests that time zones are included, which rely on the append items mechanism. |
| 482 |
#[test] |
| 483 |
fn test_components_with_zones() { |
| 484 |
test_fixture_with_time_zones( |
| 485 |
"components_with_zones", |
| 486 |
include_str!("fixtures/tests/components_with_zones.json"), |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
/// Tests that components can adjust for width differences in the final pattern. |
| 491 |
#[test] |
| 492 |
fn test_components_width_differences() { |
| 493 |
test_fixture( |
| 494 |
"components-width-differences", |
| 495 |
include_str!("fixtures/tests/components-width-differences.json"), |
| 496 |
); |
| 497 |
} |
| 498 |
|
| 499 |
/// Tests that combine components options that don't exactly match a pattern. |
| 500 |
#[test] |
| 501 |
fn test_components_partial_matches() { |
| 502 |
test_fixture( |
| 503 |
"components-partial-matches", |
| 504 |
include_str!("fixtures/tests/components-partial-matches.json"), |
| 505 |
); |
| 506 |
} |
| 507 |
|
| 508 |
/// Tests that components can combine a date skeleton, and a time skeleton. |
| 509 |
#[test] |
| 510 |
fn test_components_combine_datetime() { |
| 511 |
test_fixture( |
| 512 |
"components-combine-datetime", |
| 513 |
include_str!("fixtures/tests/components-combine-datetime.json"), |
| 514 |
); |
| 515 |
} |