Skip to content

Commit 04a64b6

Browse files
authored
Add tests for era index (#6518)
1 parent 5cfff24 commit 04a64b6

File tree

1 file changed

+84
-70
lines changed

1 file changed

+84
-70
lines changed

components/calendar/src/any_calendar.rs

+84-70
Original file line numberDiff line numberDiff line change
@@ -1571,19 +1571,20 @@ mod tests {
15711571
#[track_caller]
15721572
fn single_test_roundtrip(
15731573
calendar: Ref<AnyCalendar>,
1574-
era: Option<&str>,
1574+
era: Option<(&str, Option<u8>)>,
15751575
year: i32,
15761576
month_code: &str,
15771577
day: u8,
15781578
) {
15791579
let month = types::MonthCode(month_code.parse().expect("month code must parse"));
15801580

1581-
let date = Date::try_new_from_codes(era, year, month, day, calendar).unwrap_or_else(|e| {
1582-
panic!(
1583-
"Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}",
1584-
calendar.debug_name(),
1585-
)
1586-
});
1581+
let date = Date::try_new_from_codes(era.map(|x| x.0), year, month, day, calendar)
1582+
.unwrap_or_else(|e| {
1583+
panic!(
1584+
"Failed to construct date for {} with {era:?}, {year}, {month}, {day}: {e:?}",
1585+
calendar.debug_name(),
1586+
)
1587+
});
15871588

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

1602+
if let Some((era_code, era_index)) = era {
1603+
let roundtrip_era_year = date.year().era().expect("year type should be era");
1604+
assert_eq!(
1605+
(era_code, era_index),
1606+
(
1607+
roundtrip_era_year.era.as_str(),
1608+
roundtrip_era_year.era_index
1609+
),
1610+
"Failed to roundtrip era for calendar {}",
1611+
calendar.debug_name()
1612+
)
1613+
}
1614+
16011615
let iso = date.to_iso();
16021616
let reconstructed = Date::new_from_iso(iso, calendar);
16031617
assert_eq!(
@@ -1609,15 +1623,15 @@ mod tests {
16091623
#[track_caller]
16101624
fn single_test_error(
16111625
calendar: Ref<AnyCalendar>,
1612-
era: Option<&str>,
1626+
era: Option<(&str, Option<u8>)>,
16131627
year: i32,
16141628
month_code: &str,
16151629
day: u8,
16161630
error: DateError,
16171631
) {
16181632
let month = types::MonthCode(month_code.parse().expect("month code must parse"));
16191633

1620-
let date = Date::try_new_from_codes(era, year, month, day, calendar);
1634+
let date = Date::try_new_from_codes(era.map(|x| x.0), year, month, day, calendar);
16211635
assert_eq!(
16221636
date,
16231637
Err(error),
@@ -1663,38 +1677,38 @@ mod tests {
16631677
let persian = Ref(&persian);
16641678
let roc = Ref(&roc);
16651679

1666-
single_test_roundtrip(buddhist, Some("be"), 100, "M03", 1);
1680+
single_test_roundtrip(buddhist, Some(("be", Some(0))), 100, "M03", 1);
16671681
single_test_roundtrip(buddhist, None, 2000, "M03", 1);
1668-
single_test_roundtrip(buddhist, Some("be"), -100, "M03", 1);
1682+
single_test_roundtrip(buddhist, Some(("be", Some(0))), -100, "M03", 1);
16691683
single_test_error(
16701684
buddhist,
1671-
Some("be"),
1685+
Some(("be", Some(0))),
16721686
100,
16731687
"M13",
16741688
1,
16751689
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
16761690
);
16771691

1678-
single_test_roundtrip(coptic, Some("am"), 100, "M03", 1);
1692+
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, "M03", 1);
16791693
single_test_roundtrip(coptic, None, 2000, "M03", 1);
1680-
single_test_roundtrip(coptic, Some("am"), -99, "M03", 1);
1681-
single_test_roundtrip(coptic, Some("am"), 100, "M13", 1);
1694+
single_test_roundtrip(coptic, Some(("am", Some(0))), -99, "M03", 1);
1695+
single_test_roundtrip(coptic, Some(("am", Some(0))), 100, "M13", 1);
16821696
single_test_error(
16831697
coptic,
1684-
Some("am"),
1698+
Some(("am", Some(0))),
16851699
100,
16861700
"M14",
16871701
1,
16881702
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
16891703
);
16901704

1691-
single_test_roundtrip(ethiopian, Some("am"), 100, "M03", 1);
1705+
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 100, "M03", 1);
16921706
single_test_roundtrip(ethiopian, None, 2000, "M03", 1);
1693-
single_test_roundtrip(ethiopian, Some("am"), 2000, "M13", 1);
1694-
single_test_roundtrip(ethiopian, Some("aa"), 5400, "M03", 1);
1707+
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 2000, "M13", 1);
1708+
single_test_roundtrip(ethiopian, Some(("aa", Some(0))), 5400, "M03", 1);
16951709
single_test_error(
16961710
ethiopian,
1697-
Some("am"),
1711+
Some(("am", Some(0))),
16981712
0,
16991713
"M03",
17001714
1,
@@ -1707,7 +1721,7 @@ mod tests {
17071721
);
17081722
single_test_error(
17091723
ethiopian,
1710-
Some("aa"),
1724+
Some(("aa", Some(0))),
17111725
5600,
17121726
"M03",
17131727
1,
@@ -1720,31 +1734,31 @@ mod tests {
17201734
);
17211735
single_test_error(
17221736
ethiopian,
1723-
Some("am"),
1737+
Some(("am", Some(0))),
17241738
100,
17251739
"M14",
17261740
1,
17271741
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
17281742
);
17291743

1730-
single_test_roundtrip(ethioaa, Some("aa"), 7000, "M13", 1);
1744+
single_test_roundtrip(ethioaa, Some(("aa", Some(0))), 7000, "M13", 1);
17311745
single_test_roundtrip(ethioaa, None, 7000, "M13", 1);
1732-
single_test_roundtrip(ethioaa, Some("aa"), 100, "M03", 1);
1746+
single_test_roundtrip(ethioaa, Some(("aa", Some(0))), 100, "M03", 1);
17331747
single_test_error(
17341748
ethiopian,
1735-
Some("aa"),
1749+
Some(("aa", Some(0))),
17361750
100,
17371751
"M14",
17381752
1,
17391753
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M14"))),
17401754
);
17411755

1742-
single_test_roundtrip(gregorian, Some("ce"), 100, "M03", 1);
1756+
single_test_roundtrip(gregorian, Some(("ce", Some(1))), 100, "M03", 1);
17431757
single_test_roundtrip(gregorian, None, 2000, "M03", 1);
1744-
single_test_roundtrip(gregorian, Some("bce"), 100, "M03", 1);
1758+
single_test_roundtrip(gregorian, Some(("bce", Some(0))), 100, "M03", 1);
17451759
single_test_error(
17461760
gregorian,
1747-
Some("ce"),
1761+
Some(("ce", Some(1))),
17481762
0,
17491763
"M03",
17501764
1,
@@ -1757,7 +1771,7 @@ mod tests {
17571771
);
17581772
single_test_error(
17591773
gregorian,
1760-
Some("bce"),
1774+
Some(("bce", Some(0))),
17611775
0,
17621776
"M03",
17631777
1,
@@ -1771,20 +1785,20 @@ mod tests {
17711785

17721786
single_test_error(
17731787
gregorian,
1774-
Some("bce"),
1788+
Some(("bce", Some(0))),
17751789
100,
17761790
"M13",
17771791
1,
17781792
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
17791793
);
17801794

1781-
single_test_roundtrip(indian, Some("saka"), 100, "M03", 1);
1795+
single_test_roundtrip(indian, Some(("saka", Some(0))), 100, "M03", 1);
17821796
single_test_roundtrip(indian, None, 2000, "M12", 1);
17831797
single_test_roundtrip(indian, None, -100, "M03", 1);
1784-
single_test_roundtrip(indian, Some("saka"), 0, "M03", 1);
1798+
single_test_roundtrip(indian, Some(("saka", Some(0))), 0, "M03", 1);
17851799
single_test_error(
17861800
indian,
1787-
Some("saka"),
1801+
Some(("saka", Some(0))),
17881802
100,
17891803
"M13",
17901804
1,
@@ -1815,15 +1829,15 @@ mod tests {
18151829
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M00L"))),
18161830
);
18171831

1818-
single_test_roundtrip(japanese, Some("reiwa"), 3, "M03", 1);
1819-
single_test_roundtrip(japanese, Some("heisei"), 6, "M12", 1);
1820-
single_test_roundtrip(japanese, Some("meiji"), 10, "M03", 1);
1821-
single_test_roundtrip(japanese, Some("ce"), 1000, "M03", 1);
1832+
single_test_roundtrip(japanese, Some(("reiwa", None)), 3, "M03", 1);
1833+
single_test_roundtrip(japanese, Some(("heisei", None)), 6, "M12", 1);
1834+
single_test_roundtrip(japanese, Some(("meiji", None)), 10, "M03", 1);
1835+
single_test_roundtrip(japanese, Some(("ce", None)), 1000, "M03", 1);
18221836
single_test_roundtrip(japanese, None, 1000, "M03", 1);
1823-
single_test_roundtrip(japanese, Some("bce"), 10, "M03", 1);
1837+
single_test_roundtrip(japanese, Some(("bce", None)), 10, "M03", 1);
18241838
single_test_error(
18251839
japanese,
1826-
Some("ce"),
1840+
Some(("ce", None)),
18271841
0,
18281842
"M03",
18291843
1,
@@ -1836,7 +1850,7 @@ mod tests {
18361850
);
18371851
single_test_error(
18381852
japanese,
1839-
Some("bce"),
1853+
Some(("bce", Some(0))),
18401854
0,
18411855
"M03",
18421856
1,
@@ -1850,22 +1864,22 @@ mod tests {
18501864

18511865
single_test_error(
18521866
japanese,
1853-
Some("reiwa"),
1867+
Some(("reiwa", None)),
18541868
2,
18551869
"M13",
18561870
1,
18571871
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
18581872
);
18591873

1860-
single_test_roundtrip(japanext, Some("reiwa"), 3, "M03", 1);
1861-
single_test_roundtrip(japanext, Some("heisei"), 6, "M12", 1);
1862-
single_test_roundtrip(japanext, Some("meiji"), 10, "M03", 1);
1863-
single_test_roundtrip(japanext, Some("tenpyokampo-749"), 1, "M04", 20);
1864-
single_test_roundtrip(japanext, Some("ce"), 100, "M03", 1);
1865-
single_test_roundtrip(japanext, Some("bce"), 10, "M03", 1);
1874+
single_test_roundtrip(japanext, Some(("reiwa", None)), 3, "M03", 1);
1875+
single_test_roundtrip(japanext, Some(("heisei", None)), 6, "M12", 1);
1876+
single_test_roundtrip(japanext, Some(("meiji", None)), 10, "M03", 1);
1877+
single_test_roundtrip(japanext, Some(("tenpyokampo-749", None)), 1, "M04", 20);
1878+
single_test_roundtrip(japanext, Some(("ce", None)), 100, "M03", 1);
1879+
single_test_roundtrip(japanext, Some(("bce", None)), 10, "M03", 1);
18661880
single_test_error(
18671881
japanext,
1868-
Some("ce"),
1882+
Some(("ce", None)),
18691883
0,
18701884
"M03",
18711885
1,
@@ -1878,7 +1892,7 @@ mod tests {
18781892
);
18791893
single_test_error(
18801894
japanext,
1881-
Some("bce"),
1895+
Some(("bce", Some(0))),
18821896
0,
18831897
"M03",
18841898
1,
@@ -1892,83 +1906,83 @@ mod tests {
18921906

18931907
single_test_error(
18941908
japanext,
1895-
Some("reiwa"),
1909+
Some(("reiwa", None)),
18961910
2,
18971911
"M13",
18981912
1,
18991913
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M13"))),
19001914
);
19011915

1902-
single_test_roundtrip(persian, Some("ap"), 477, "M03", 1);
1916+
single_test_roundtrip(persian, Some(("ap", Some(0))), 477, "M03", 1);
19031917
single_test_roundtrip(persian, None, 2083, "M07", 21);
1904-
single_test_roundtrip(persian, Some("ap"), 1600, "M12", 20);
1918+
single_test_roundtrip(persian, Some(("ap", Some(0))), 1600, "M12", 20);
19051919
single_test_error(
19061920
persian,
1907-
Some("ap"),
1921+
Some(("ap", Some(0))),
19081922
100,
19091923
"M9",
19101924
1,
19111925
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
19121926
);
19131927

1914-
single_test_roundtrip(hebrew, Some("am"), 5773, "M03", 1);
1928+
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5773, "M03", 1);
19151929
single_test_roundtrip(hebrew, None, 4993, "M07", 21);
1916-
single_test_roundtrip(hebrew, Some("am"), 5012, "M12", 20);
1930+
single_test_roundtrip(hebrew, Some(("am", Some(0))), 5012, "M12", 20);
19171931
single_test_error(
19181932
hebrew,
1919-
Some("am"),
1933+
Some(("am", Some(0))),
19201934
100,
19211935
"M9",
19221936
1,
19231937
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
19241938
);
19251939

1926-
single_test_roundtrip(roc, Some("minguo"), 10, "M05", 3);
1927-
single_test_roundtrip(roc, Some("minguo-qian"), 15, "M01", 10);
1940+
single_test_roundtrip(roc, Some(("minguo", Some(1))), 10, "M05", 3);
1941+
single_test_roundtrip(roc, Some(("minguo-qian", Some(0))), 15, "M01", 10);
19281942
single_test_roundtrip(roc, None, 100, "M10", 30);
19291943

1930-
single_test_roundtrip(hijri_simulated, Some("ah"), 477, "M03", 1);
1944+
single_test_roundtrip(hijri_simulated, Some(("ah", Some(0))), 477, "M03", 1);
19311945
single_test_roundtrip(hijri_simulated, None, 2083, "M07", 21);
1932-
single_test_roundtrip(hijri_simulated, Some("ah"), 1600, "M12", 20);
1946+
single_test_roundtrip(hijri_simulated, Some(("ah", Some(0))), 1600, "M12", 20);
19331947
single_test_error(
19341948
hijri_simulated,
1935-
Some("ah"),
1949+
Some(("ah", Some(0))),
19361950
100,
19371951
"M9",
19381952
1,
19391953
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
19401954
);
19411955

1942-
single_test_roundtrip(hijri_civil, Some("ah"), 477, "M03", 1);
1956+
single_test_roundtrip(hijri_civil, Some(("ah", Some(0))), 477, "M03", 1);
19431957
single_test_roundtrip(hijri_civil, None, 2083, "M07", 21);
1944-
single_test_roundtrip(hijri_civil, Some("ah"), 1600, "M12", 20);
1958+
single_test_roundtrip(hijri_civil, Some(("ah", Some(0))), 1600, "M12", 20);
19451959
single_test_error(
19461960
hijri_civil,
1947-
Some("ah"),
1961+
Some(("ah", Some(0))),
19481962
100,
19491963
"M9",
19501964
1,
19511965
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
19521966
);
19531967

1954-
single_test_roundtrip(hijri_umm_al_qura, Some("ah"), 477, "M03", 1);
1968+
single_test_roundtrip(hijri_umm_al_qura, Some(("ah", Some(0))), 477, "M03", 1);
19551969
single_test_roundtrip(hijri_umm_al_qura, None, 2083, "M07", 21);
1956-
single_test_roundtrip(hijri_umm_al_qura, Some("ah"), 1600, "M12", 20);
1970+
single_test_roundtrip(hijri_umm_al_qura, Some(("ah", Some(0))), 1600, "M12", 20);
19571971
single_test_error(
19581972
hijri_umm_al_qura,
1959-
Some("ah"),
1973+
Some(("ah", Some(0))),
19601974
100,
19611975
"M9",
19621976
1,
19631977
DateError::UnknownMonthCode(MonthCode(tinystr!(4, "M9"))),
19641978
);
19651979

1966-
single_test_roundtrip(hijri_astronomical, Some("ah"), 477, "M03", 1);
1980+
single_test_roundtrip(hijri_astronomical, Some(("ah", Some(0))), 477, "M03", 1);
19671981
single_test_roundtrip(hijri_astronomical, None, 2083, "M07", 21);
1968-
single_test_roundtrip(hijri_astronomical, Some("ah"), 1600, "M12", 20);
1982+
single_test_roundtrip(hijri_astronomical, Some(("ah", Some(0))), 1600, "M12", 20);
19691983
single_test_error(
19701984
hijri_astronomical,
1971-
Some("ah"),
1985+
Some(("ah", Some(0))),
19721986
100,
19731987
"M9",
19741988
1,

0 commit comments

Comments
 (0)