diff --git a/components/calendar/src/tests/mod.rs b/components/calendar/src/tests/mod.rs index 7cac2a901f8..b753567880b 100644 --- a/components/calendar/src/tests/mod.rs +++ b/components/calendar/src/tests/mod.rs @@ -4,3 +4,4 @@ mod continuity_test; mod extrema; +mod not_enough_fields; diff --git a/components/calendar/src/tests/not_enough_fields.rs b/components/calendar/src/tests/not_enough_fields.rs new file mode 100644 index 00000000000..ce365a29b2d --- /dev/null +++ b/components/calendar/src/tests/not_enough_fields.rs @@ -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:?}" + ); + } + } + } +}