Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/calendar/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

mod continuity_test;
mod extrema;
mod not_enough_fields;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: crate/src/tests is a weird thing specifically in icu_calendar for tests that need to access pub(crate) items. Most tests should go into crate/tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I made this choice because the test gets much more ugly because of non_exhaustive.

237 changes: 237 additions & 0 deletions components/calendar/src/tests/not_enough_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// 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/main/LICENSE ).

use crate::options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow};
use crate::types::{DateFields, MonthCode};
use crate::Date;
use crate::DateError;

#[test]
fn test_from_fields_not_enough_fields() {
// Pick a sufficiently complex calendar.
let calendar = crate::cal::Hebrew::new();

let big_i32 = Some(i32::MAX);
let big_u8 = Some(u8::MAX);
let small_u8 = Some(1);
let small_i32 = Some(5000);
let valid_month_code = MonthCode::new_normal(1);
let invalid_month_code = MonthCode::new_normal(99);

// We want to ensure that most NotEnoughFields cases return NotEnoughFields
// even when we're providing out-of-range values, so that
// this produces TypeError in Temporal as opposed to RangeError.

for overflow in [Overflow::Reject, Overflow::Constrain] {
for missing_fields in [MissingFieldsStrategy::Reject, MissingFieldsStrategy::Ecma] {
let options = DateFromFieldsOptions {
overflow: Some(overflow),
missing_fields_strategy: Some(missing_fields),
};

// No month data always errors
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: big_i32,
extended_year: None,
ordinal_month: None,
month_code: None,
day: small_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: small_i32,
extended_year: None,
ordinal_month: None,
month_code: None,
day: big_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: big_i32,
ordinal_month: None,
month_code: None,
day: small_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);

// Insufficient era-year data always errors
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: None,
extended_year: None,
ordinal_month: big_u8,
month_code: None,
day: small_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: None,
extended_year: None,
ordinal_month: small_u8,
month_code: None,
day: big_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);

// No year info errors for ordinal months regardless of missing fields strategy
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: None,
ordinal_month: small_u8,
month_code: None,
day: big_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: None,
ordinal_month: big_u8,
month_code: None,
day: small_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
if missing_fields != MissingFieldsStrategy::Ecma {
// No year info errors only when there is no missing fields strategy
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: None,
ordinal_month: None,
month_code: valid_month_code,
day: big_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: None,
ordinal_month: None,
month_code: invalid_month_code,
day: small_u8,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);

// No day info errors only when there is no missing field strategy
assert_eq!(
Date::try_from_fields(
DateFields {
era: None,
era_year: None,
extended_year: big_i32,
ordinal_month: small_u8,
month_code: None,
day: None,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: big_i32,
extended_year: None,
ordinal_month: small_u8,
month_code: None,
day: None,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
assert_eq!(
Date::try_from_fields(
DateFields {
era: Some("hebrew"),
era_year: small_i32,
extended_year: None,
ordinal_month: big_u8,
month_code: None,
day: None,
},
options,
calendar
),
Err(DateError::NotEnoughFields),
"Test with {options:?}"
);
}
}
}
}