Skip to content

Add tests for era index #6518

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 1 commit into from
May 2, 2025
Merged
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
154 changes: 84 additions & 70 deletions components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,19 +1571,20 @@ mod tests {
#[track_caller]
fn single_test_roundtrip(
calendar: Ref<AnyCalendar>,
era: Option<&str>,
era: Option<(&str, Option<u8>)>,
year: i32,
month_code: &str,
day: u8,
) {
let month = types::MonthCode(month_code.parse().expect("month code must parse"));

let date = Date::try_new_from_codes(era, year, month, day, calendar).unwrap_or_else(|e| {
panic!(
"Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}",
calendar.debug_name(),
)
});
let date = Date::try_new_from_codes(era.map(|x| x.0), year, month, day, calendar)
.unwrap_or_else(|e| {
panic!(
"Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}",
calendar.debug_name(),
)
});

let roundtrip_year = date.year();
// FIXME: these APIs should be improved
Expand All @@ -1598,6 +1599,19 @@ mod tests {
calendar.debug_name()
);

if let Some((era_code, era_index)) = era {
let roundtrip_era_year = date.year().era().expect("year type should be era");
assert_eq!(
(era_code, era_index),
(
roundtrip_era_year.era.as_str(),
roundtrip_era_year.era_index
),
"Failed to roundtrip era for calendar {}",
calendar.debug_name()
)
}

let iso = date.to_iso();
let reconstructed = Date::new_from_iso(iso, calendar);
assert_eq!(
Expand All @@ -1609,15 +1623,15 @@ mod tests {
#[track_caller]
fn single_test_error(
calendar: Ref<AnyCalendar>,
era: Option<&str>,
era: Option<(&str, Option<u8>)>,
year: i32,
month_code: &str,
day: u8,
error: DateError,
) {
let month = types::MonthCode(month_code.parse().expect("month code must parse"));

let date = Date::try_new_from_codes(era, year, month, day, calendar);
let date = Date::try_new_from_codes(era.map(|x| x.0), year, month, day, calendar);
assert_eq!(
date,
Err(error),
Expand Down Expand Up @@ -1663,38 +1677,38 @@ mod tests {
let persian = Ref(&persian);
let roc = Ref(&roc);

single_test_roundtrip(buddhist, Some("be"), 100, "M03", 1);
single_test_roundtrip(buddhist, Some(("be", Some(0))), 100, "M03", 1);
single_test_roundtrip(buddhist, None, 2000, "M03", 1);
single_test_roundtrip(buddhist, Some("be"), -100, "M03", 1);
single_test_roundtrip(buddhist, Some(("be", Some(0))), -100, "M03", 1);
single_test_error(
buddhist,
Some("be"),
Some(("be", Some(0))),
100,
"M13",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
);

single_test_roundtrip(coptic, Some("am"), 100, "M03", 1);
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, "M03", 1);
single_test_roundtrip(coptic, None, 2000, "M03", 1);
single_test_roundtrip(coptic, Some("am"), -99, "M03", 1);
single_test_roundtrip(coptic, Some("am"), 100, "M13", 1);
single_test_roundtrip(coptic, Some(("am", Some(0))), -99, "M03", 1);
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, "M13", 1);
single_test_error(
coptic,
Some("am"),
Some(("am", Some(0))),
100,
"M14",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
);

single_test_roundtrip(ethiopian, Some("am"), 100, "M03", 1);
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 100, "M03", 1);
single_test_roundtrip(ethiopian, None, 2000, "M03", 1);
single_test_roundtrip(ethiopian, Some("am"), 2000, "M13", 1);
single_test_roundtrip(ethiopian, Some("aa"), 5400, "M03", 1);
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 2000, "M13", 1);
single_test_roundtrip(ethiopian, Some(("aa", Some(0))), 5400, "M03", 1);
single_test_error(
ethiopian,
Some("am"),
Some(("am", Some(0))),
0,
"M03",
1,
Expand All @@ -1707,7 +1721,7 @@ mod tests {
);
single_test_error(
ethiopian,
Some("aa"),
Some(("aa", Some(0))),
5600,
"M03",
1,
Expand All @@ -1720,31 +1734,31 @@ mod tests {
);
single_test_error(
ethiopian,
Some("am"),
Some(("am", Some(0))),
100,
"M14",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
);

single_test_roundtrip(ethioaa, Some("aa"), 7000, "M13", 1);
single_test_roundtrip(ethioaa, Some(("aa", Some(0))), 7000, "M13", 1);
single_test_roundtrip(ethioaa, None, 7000, "M13", 1);
single_test_roundtrip(ethioaa, Some("aa"), 100, "M03", 1);
single_test_roundtrip(ethioaa, Some(("aa", Some(0))), 100, "M03", 1);
single_test_error(
ethiopian,
Some("aa"),
Some(("aa", Some(0))),
100,
"M14",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
);

single_test_roundtrip(gregorian, Some("ce"), 100, "M03", 1);
single_test_roundtrip(gregorian, Some(("ce", Some(1))), 100, "M03", 1);
single_test_roundtrip(gregorian, None, 2000, "M03", 1);
single_test_roundtrip(gregorian, Some("bce"), 100, "M03", 1);
single_test_roundtrip(gregorian, Some(("bce", Some(0))), 100, "M03", 1);
single_test_error(
gregorian,
Some("ce"),
Some(("ce", Some(1))),
0,
"M03",
1,
Expand All @@ -1757,7 +1771,7 @@ mod tests {
);
single_test_error(
gregorian,
Some("bce"),
Some(("bce", Some(0))),
0,
"M03",
1,
Expand All @@ -1771,20 +1785,20 @@ mod tests {

single_test_error(
gregorian,
Some("bce"),
Some(("bce", Some(0))),
100,
"M13",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
);

single_test_roundtrip(indian, Some("saka"), 100, "M03", 1);
single_test_roundtrip(indian, Some(("saka", Some(0))), 100, "M03", 1);
single_test_roundtrip(indian, None, 2000, "M12", 1);
single_test_roundtrip(indian, None, -100, "M03", 1);
single_test_roundtrip(indian, Some("saka"), 0, "M03", 1);
single_test_roundtrip(indian, Some(("saka", Some(0))), 0, "M03", 1);
single_test_error(
indian,
Some("saka"),
Some(("saka", Some(0))),
100,
"M13",
1,
Expand Down Expand Up @@ -1815,15 +1829,15 @@ mod tests {
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M00L"))),
);

single_test_roundtrip(japanese, Some("reiwa"), 3, "M03", 1);
single_test_roundtrip(japanese, Some("heisei"), 6, "M12", 1);
single_test_roundtrip(japanese, Some("meiji"), 10, "M03", 1);
single_test_roundtrip(japanese, Some("ce"), 1000, "M03", 1);
single_test_roundtrip(japanese, Some(("reiwa", None)), 3, "M03", 1);
single_test_roundtrip(japanese, Some(("heisei", None)), 6, "M12", 1);
single_test_roundtrip(japanese, Some(("meiji", None)), 10, "M03", 1);
single_test_roundtrip(japanese, Some(("ce", None)), 1000, "M03", 1);
single_test_roundtrip(japanese, None, 1000, "M03", 1);
single_test_roundtrip(japanese, Some("bce"), 10, "M03", 1);
single_test_roundtrip(japanese, Some(("bce", None)), 10, "M03", 1);
single_test_error(
japanese,
Some("ce"),
Some(("ce", None)),
0,
"M03",
1,
Expand All @@ -1836,7 +1850,7 @@ mod tests {
);
single_test_error(
japanese,
Some("bce"),
Some(("bce", Some(0))),
0,
"M03",
1,
Expand All @@ -1850,22 +1864,22 @@ mod tests {

single_test_error(
japanese,
Some("reiwa"),
Some(("reiwa", None)),
2,
"M13",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
);

single_test_roundtrip(japanext, Some("reiwa"), 3, "M03", 1);
single_test_roundtrip(japanext, Some("heisei"), 6, "M12", 1);
single_test_roundtrip(japanext, Some("meiji"), 10, "M03", 1);
single_test_roundtrip(japanext, Some("tenpyokampo-749"), 1, "M04", 20);
single_test_roundtrip(japanext, Some("ce"), 100, "M03", 1);
single_test_roundtrip(japanext, Some("bce"), 10, "M03", 1);
single_test_roundtrip(japanext, Some(("reiwa", None)), 3, "M03", 1);
single_test_roundtrip(japanext, Some(("heisei", None)), 6, "M12", 1);
single_test_roundtrip(japanext, Some(("meiji", None)), 10, "M03", 1);
single_test_roundtrip(japanext, Some(("tenpyokampo-749", None)), 1, "M04", 20);
single_test_roundtrip(japanext, Some(("ce", None)), 100, "M03", 1);
single_test_roundtrip(japanext, Some(("bce", None)), 10, "M03", 1);
single_test_error(
japanext,
Some("ce"),
Some(("ce", None)),
0,
"M03",
1,
Expand All @@ -1878,7 +1892,7 @@ mod tests {
);
single_test_error(
japanext,
Some("bce"),
Some(("bce", Some(0))),
0,
"M03",
1,
Expand All @@ -1892,83 +1906,83 @@ mod tests {

single_test_error(
japanext,
Some("reiwa"),
Some(("reiwa", None)),
2,
"M13",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
);

single_test_roundtrip(persian, Some("ap"), 477, "M03", 1);
single_test_roundtrip(persian, Some(("ap", Some(0))), 477, "M03", 1);
single_test_roundtrip(persian, None, 2083, "M07", 21);
single_test_roundtrip(persian, Some("ap"), 1600, "M12", 20);
single_test_roundtrip(persian, Some(("ap", Some(0))), 1600, "M12", 20);
single_test_error(
persian,
Some("ap"),
Some(("ap", Some(0))),
100,
"M9",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
);

single_test_roundtrip(hebrew, Some("am"), 5773, "M03", 1);
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5773, "M03", 1);
single_test_roundtrip(hebrew, None, 4993, "M07", 21);
single_test_roundtrip(hebrew, Some("am"), 5012, "M12", 20);
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5012, "M12", 20);
single_test_error(
hebrew,
Some("am"),
Some(("am", Some(0))),
100,
"M9",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
);

single_test_roundtrip(roc, Some("minguo"), 10, "M05", 3);
single_test_roundtrip(roc, Some("minguo-qian"), 15, "M01", 10);
single_test_roundtrip(roc, Some(("minguo", Some(1))), 10, "M05", 3);
single_test_roundtrip(roc, Some(("minguo-qian", Some(0))), 15, "M01", 10);
single_test_roundtrip(roc, None, 100, "M10", 30);

single_test_roundtrip(hijri_simulated, Some("ah"), 477, "M03", 1);
single_test_roundtrip(hijri_simulated, Some(("ah", Some(0))), 477, "M03", 1);
single_test_roundtrip(hijri_simulated, None, 2083, "M07", 21);
single_test_roundtrip(hijri_simulated, Some("ah"), 1600, "M12", 20);
single_test_roundtrip(hijri_simulated, Some(("ah", Some(0))), 1600, "M12", 20);
single_test_error(
hijri_simulated,
Some("ah"),
Some(("ah", Some(0))),
100,
"M9",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
);

single_test_roundtrip(hijri_civil, Some("ah"), 477, "M03", 1);
single_test_roundtrip(hijri_civil, Some(("ah", Some(0))), 477, "M03", 1);
single_test_roundtrip(hijri_civil, None, 2083, "M07", 21);
single_test_roundtrip(hijri_civil, Some("ah"), 1600, "M12", 20);
single_test_roundtrip(hijri_civil, Some(("ah", Some(0))), 1600, "M12", 20);
single_test_error(
hijri_civil,
Some("ah"),
Some(("ah", Some(0))),
100,
"M9",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
);

single_test_roundtrip(hijri_umm_al_qura, Some("ah"), 477, "M03", 1);
single_test_roundtrip(hijri_umm_al_qura, Some(("ah", Some(0))), 477, "M03", 1);
single_test_roundtrip(hijri_umm_al_qura, None, 2083, "M07", 21);
single_test_roundtrip(hijri_umm_al_qura, Some("ah"), 1600, "M12", 20);
single_test_roundtrip(hijri_umm_al_qura, Some(("ah", Some(0))), 1600, "M12", 20);
single_test_error(
hijri_umm_al_qura,
Some("ah"),
Some(("ah", Some(0))),
100,
"M9",
1,
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
);

single_test_roundtrip(hijri_astronomical, Some("ah"), 477, "M03", 1);
single_test_roundtrip(hijri_astronomical, Some(("ah", Some(0))), 477, "M03", 1);
single_test_roundtrip(hijri_astronomical, None, 2083, "M07", 21);
single_test_roundtrip(hijri_astronomical, Some("ah"), 1600, "M12", 20);
single_test_roundtrip(hijri_astronomical, Some(("ah", Some(0))), 1600, "M12", 20);
single_test_error(
hijri_astronomical,
Some("ah"),
Some(("ah", Some(0))),
100,
"M9",
1,
Expand Down