-
Notifications
You must be signed in to change notification settings - Fork 226
Add tests for NotEnoughFieldsError priority in Date::from_fields #7098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
|
||
mod continuity_test; | ||
mod extrema; | ||
mod not_enough_fields; | ||
This file contains hidden or 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,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!( | ||
Manishearth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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:?}" | ||
); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inicu_calendar
for tests that need to accesspub(crate)
items. Most tests should go intocrate/tests
.There was a problem hiding this comment.
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.