-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for DayPeirod AmPm and NoonMidnight patterns
- 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
Showing
6 changed files
with
388 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.