Skip to content

Commit

Permalink
Move pattern canonicalize code to datagen test; suppress 2-digit year…
Browse files Browse the repository at this point in the history
… diffs
  • Loading branch information
sffc committed Nov 28, 2024
1 parent 4a5e3c8 commit 3d2c3fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
27 changes: 7 additions & 20 deletions components/datetime/src/provider/pattern/reference/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ pub struct Pattern {
}

impl Pattern {
/// Convert a [`Pattern`] to a vector of pattern items.
///
/// The [`Pattern`] can be restored via the `From` impl.
pub fn into_items(self) -> Vec<PatternItem> {
self.items
}

#[cfg(feature = "datagen")]
pub(crate) fn items(&self) -> &[PatternItem] {
&self.items
Expand All @@ -39,26 +46,6 @@ impl Pattern {
pub(crate) fn to_runtime_pattern(&self) -> runtime::Pattern<'static> {
runtime::Pattern::from(self)
}

/// Replace fields with their equivalent canonical forms.
///
/// For example, replace "GGG" with "G".
#[cfg(feature = "datagen")]
pub fn canonicalize(&mut self) {
use crate::fields::{Field, FieldLength, FieldSymbol};

for item in self.items.iter_mut() {
match item {
PatternItem::Field(ref mut field @ Field {
symbol: FieldSymbol::Era,
length: FieldLength::Three
}) => {
field.length = FieldLength::One;
}
_ => {}
}
}
}
}

impl From<Vec<PatternItem>> for Pattern {
Expand Down
30 changes: 28 additions & 2 deletions provider/source/src/datetime/neo_skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,32 @@ mod date_skeleton_consistency_tests {
length: &'a str,
}

fn canonicalize_pattern(pattern: &mut reference::Pattern) {
use icu::datetime::fields::{Field, FieldLength, FieldSymbol};
use icu::datetime::provider::pattern::PatternItem;

let mut items = core::mem::take(pattern).into_items();
for item in items.iter_mut() {
match item {
PatternItem::Field(ref mut field @ Field {
symbol: FieldSymbol::Era,
length: FieldLength::Three
}) => {
field.length = FieldLength::One;
}
// For now, ignore differences between 'y' and 'yy'
PatternItem::Field(ref mut field @ Field {
length: FieldLength::Two,
..
}) => {
field.length = FieldLength::One;
}
_ => {}
}
}
*pattern = items.into();
}

/// Returns whether the check was successful.
fn check_single_pattern(data: TestCaseFixedArgs, info: TestCaseInfo) -> bool {
let parsed_skeleton: reference::Pattern = info.skeleton.parse().unwrap();
Expand All @@ -590,10 +616,10 @@ mod date_skeleton_consistency_tests {

// Canonicalize the two patterns to make comparison easier
let mut selected_pattern = reference::Pattern::from(&selected_pattern);
selected_pattern.canonicalize();
canonicalize_pattern(&mut selected_pattern);
let selected_pattern = runtime::Pattern::from(&selected_pattern).to_string();
let mut expected_pattern: reference::Pattern = info.pattern.parse().unwrap();
expected_pattern.canonicalize();
canonicalize_pattern(&mut expected_pattern);
let expected_pattern = runtime::Pattern::from(&expected_pattern).to_string();

if expected_pattern != selected_pattern {
Expand Down

0 comments on commit 3d2c3fe

Please sign in to comment.