Skip to content

Commit

Permalink
Refactor DayPeriod patterns tests to integration tests
Browse files Browse the repository at this point in the history
- Converts `DayPeriod` pattern tests to be integration tests.
  - Tests no longer direclty use the private `write_pattern()`.
  - Tests now mutate the `DatesV1` struct to the desired pattern,
    using `DateTimeFormat` to format the custom patterns.
  • Loading branch information
nordzilla committed Jan 14, 2021
1 parent 1578fea commit 2e710b3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 81 deletions.
28 changes: 26 additions & 2 deletions components/datetime/src/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).
#[cfg(test)]
mod tests;

use crate::date::{self, DateTimeType};
use crate::error::DateTimeFormatError;
Expand Down Expand Up @@ -161,3 +159,29 @@ where
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_format_number() {
let values = &[2, 20, 201, 2017, 20173];
let samples = &[
(FieldLength::One, ["2", "20", "201", "2017", "20173"]),
(FieldLength::TwoDigit, ["02", "20", "01", "17", "73"]),
(
FieldLength::Abbreviated,
["002", "020", "201", "2017", "20173"],
),
(FieldLength::Wide, ["0002", "0020", "0201", "2017", "20173"]),
];
for (length, expected) in samples {
for (value, expected) in values.iter().zip(expected) {
let mut s = String::new();
format_number(&mut s, *value, *length).unwrap();
assert_eq!(s, *expected);
}
}
}
}
76 changes: 0 additions & 76 deletions components/datetime/src/format/tests/mod.rs

This file was deleted.

65 changes: 63 additions & 2 deletions components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).
mod fixtures;
mod patterns;

use icu_datetime::date::MockDateTime;
use icu_datetime::DateTimeFormat;
use std::fmt::Write;
use icu_datetime::{date::MockDateTime, DateTimeFormatOptions};
use icu_locid::LanguageIdentifier;
use icu_provider::{
struct_provider::StructProvider,
structs::dates::{gregory::DatesV1, key::GREGORY_V1},
DataProvider, DataRequest, ResourceOptions, ResourcePath,
};
use std::{borrow::Cow, fmt::Write};

#[test]
fn test_fixtures() {
Expand Down Expand Up @@ -33,3 +40,57 @@ fn test_fixtures() {
assert_eq!(s, fx.output.value);
}
}

#[test]
fn test_dayperiod_patterns() {
use patterns::structs::Expectation;
let provider = icu_testdata::get_provider();
for test in patterns::get_tests("dayperiods").unwrap().0 {
let langid: LanguageIdentifier = test.locale.parse().unwrap();
let mut data: Cow<DatesV1> = provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: GREGORY_V1,
options: ResourceOptions {
variant: None,
langid: Some(langid.clone()),
},
},
})
.unwrap()
.take_payload()
.unwrap();
for test_case in &test.test_cases {
for dt_input in &test_case.date_times {
let date_time: MockDateTime = dt_input.parse().unwrap();
for Expectation { patterns, expected } in &test_case.expectations {
for pattern_input in patterns {
*data.to_mut().patterns.time.long.to_mut() = String::from(pattern_input);
*data.to_mut().patterns.date_time.long.to_mut() = String::from("{0}");
let provider = StructProvider {
key: GREGORY_V1,
data: data.as_ref(),
};
let dtf = DateTimeFormat::try_new(
langid.clone(),
&provider,
&DateTimeFormatOptions::default(),
)
.unwrap();
assert_eq!(
dtf.format(&date_time).to_string(),
*expected,
"\n\
locale: `{}`,\n\
datetime: `{}`,\n\
pattern: `{}`",
langid,
dt_input,
pattern_input,
);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fs::File;
use std::io::BufReader;

pub fn get_tests(name: &str) -> std::io::Result<structs::Tests> {
let file = File::open(format!("./src/format/tests/patterns/tests/{}.json", name))?;
let file = File::open(format!("./tests/patterns/tests/{}.json", name))?;
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}

0 comments on commit 2e710b3

Please sign in to comment.