Skip to content

Commit

Permalink
Add tests for DayPeirod AmPm and NoonMidnight patterns
Browse files Browse the repository at this point in the history
- Restructures `format` module to use directory structure instead of single file.
- Adds a `format/tests` directory.
- Moves existing local tests from `format.rs` to `format/tests/mod.rs`
- Adds JSON test data for `DayPeriod` patterns.
- Adds JSON-serializable structs for testing formatting patterns.
- Adds test cases for `DayPeriod` patterns.
- Adds parsing test cases for the `b` `DayPeriod` pattern.
  • Loading branch information
nordzilla committed Jan 12, 2021
1 parent 3acd2a4 commit 572aff5
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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;
use crate::fields::{self, FieldLength, FieldSymbol};
Expand Down Expand Up @@ -144,39 +147,17 @@ where
format_number(w, date_time.second().into(), field.length)?
}
FieldSymbol::DayPeriod(period) => {
let symbol =
data.get_symbol_for_day_period(period, field.length, date_time.hour(), date_time.minute());
let symbol = data.get_symbol_for_day_period(
period,
field.length,
date_time.hour(),
date_time.minute(),
);
w.write_str(symbol)?
},
}
},
PatternItem::Literal(l) => w.write_str(l)?,
}
}
Ok(())
}

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

#[test]
fn test_format_numer() {
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: 76 additions & 0 deletions components/datetime/src/format/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 ).
mod patterns;

use super::{format_number, write_pattern};
use crate::{date::MockDateTime, fields::FieldLength, pattern::Pattern};
use icu_locid::LanguageIdentifier;
use icu_provider::{
structs::{self, dates::gregory::DatesV1},
DataProvider, DataRequest, ResourceOptions, ResourcePath,
};
use std::borrow::Cow;

#[test]
fn test_format_numer() {
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);
}
}
}

#[test]
fn test_format_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 data: Cow<DatesV1> = provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: structs::dates::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 {
let pattern = Pattern::from_bytes(pattern_input).unwrap();
let mut actual = String::with_capacity(expected.len());
write_pattern(&pattern, &data, &date_time, &mut actual).unwrap();
assert_eq!(
actual, *expected,
"\n\
locale: `{}`,\n\
datetime: `{}`,\n\
pattern: `{}`",
langid, dt_input, pattern_input,
);
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions components/datetime/src/format/tests/patterns/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 ).
pub mod structs;

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 reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
25 changes: 25 additions & 0 deletions components/datetime/src/format/tests/patterns/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 ).
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tests(pub Vec<Test>);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Test {
pub locale: String,
pub test_cases: Vec<TestCase>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TestCase {
pub date_times: Vec<String>,
pub expectations: Vec<Expectation>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Expectation {
pub patterns: Vec<String>,
pub expected: String,
}
Loading

0 comments on commit 572aff5

Please sign in to comment.