Debian Codegraph @

debian/rust-vendor/icu_calendar/src/tests/month.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 #![allow(clippy::field_reassign_with_default)] // use public API
6
7 use crate::Date;
8 use crate::error::{DateFromFieldsError, MonthCodeParseError};
9 use crate::options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow};
10 use crate::preferences::CalendarAlgorithm;
11 use crate::types::{DateFields, Month, MonthCode};
12
13 static INVALID_SYNTAX: &[&str] = &[
14 "M", "M0", "M1", "01L", "L01", "M001", "M110", "MxxL", "m01", "M02l",
15 ];
16
17 static NOT_IN_ANY_CALENDAR: &[&str] = &["M00", "M14", "M99", "M13L"];
18
19 static CHINESE_ONLY: &[&str] = &[
20 "M01L", "M02L", "M03L", "M04L", "M06L", "M07L", "M08L", "M09L", "M10L", "M11L", "M12L",
21 ];
22
23 static CHINESE_HEBREW: &[&str] = &["M05L"];
24
25 static COPTIC_ONLY: &[&str] = &["M13"];
26
27 static UNIVERSAL_MONTH_CODES: &[&str] = &[
28 "M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09", "M10", "M11", "M12",
29 ];
30
31 #[test]
32 fn test_month_parsing() {
33 for &code in INVALID_SYNTAX {
34 let result = Month::try_from_str(code);
35 assert_eq!(
36 result.unwrap_err(),
37 MonthCodeParseError::InvalidSyntax,
38 "Should have failed with InvalidSyntax: {code}"
39 );
40 }
41
42 let valid_syntax = UNIVERSAL_MONTH_CODES
43 .iter()
44 .chain(NOT_IN_ANY_CALENDAR.iter())
45 .chain(CHINESE_ONLY.iter())
46 .chain(CHINESE_HEBREW.iter())
47 .chain(COPTIC_ONLY.iter())
48 .copied();
49
50 for code in valid_syntax {
51 let result = Month::try_from_str(code);
52 assert!(result.is_ok(), "Should have succeeded to parse: {code}");
53 let month = result.unwrap();
54 let number = month.number();
55 let is_leap = month.is_leap();
56
57 let expected_number = code[1..3].parse::<u8>().unwrap();
58 let expected_leap = code.ends_with('L');
59
60 assert_eq!(number, expected_number, "Wrong number for {code}");
61 assert_eq!(is_leap, expected_leap, "Wrong leap status for {code}");
62
63 // Test roundtrip
64 assert_eq!(
65 month.code(),
66 MonthCode(code.parse().unwrap()),
67 "Roundtrip failed for {code}"
68 );
69 }
70 }
71
72 crate::tests::test_all_cals!(
73 fn test_month_fields<C: Calendar + Copy>(cal: C) {
74 let mut valid_month_codes = UNIVERSAL_MONTH_CODES.to_vec();
75 let mut invalid_month_codes = NOT_IN_ANY_CALENDAR.to_vec();
76
77 let cal_alg = cal.calendar_algorithm();
78
79 if matches!(
80 cal_alg,
81 Some(CalendarAlgorithm::Chinese | CalendarAlgorithm::Dangi)
82 ) {
83 valid_month_codes.extend_from_slice(CHINESE_ONLY);
84 } else {
85 invalid_month_codes.extend_from_slice(CHINESE_ONLY);
86 }
87
88 if matches!(
89 cal_alg,
90 Some(CalendarAlgorithm::Chinese | CalendarAlgorithm::Dangi | CalendarAlgorithm::Hebrew)
91 ) {
92 valid_month_codes.extend_from_slice(CHINESE_HEBREW);
93 } else {
94 invalid_month_codes.extend_from_slice(CHINESE_HEBREW);
95 }
96
97 if matches!(
98 cal_alg,
99 Some(
100 CalendarAlgorithm::Coptic
101 | CalendarAlgorithm::Ethiopic
102 | CalendarAlgorithm::Ethioaa
103 )
104 ) {
105 valid_month_codes.extend_from_slice(COPTIC_ONLY);
106 } else {
107 invalid_month_codes.extend_from_slice(COPTIC_ONLY);
108 }
109
110 // Test with full dates
111 for extended_year in -100..100 {
112 let options = DateFromFieldsOptions::default();
113 let mut fields = DateFields::default();
114 fields.extended_year = Some(extended_year);
115 fields.day = Some(1);
116 for month_code in valid_month_codes.iter() {
117 fields.month = Some(Month::try_from_str(month_code).unwrap());
118 match Date::try_from_fields(fields, options, cal) {
119 Ok(_) => (),
120 Err(DateFromFieldsError::MonthNotInYear) => (),
121 Err(e) => {
122 panic!(
123 "Should have succeeded, but failed: {extended_year} {month_code} {e:?}"
124 );
125 }
126 }
127 }
128 for month_code in invalid_month_codes.iter() {
129 let month = Month::try_from_str(month_code).unwrap();
130 fields.month = Some(month);
131 let result = Date::try_from_fields(fields, options, cal);
132 match result {
133 Err(DateFromFieldsError::MonthNotInCalendar) => (),
134 Ok(_) => {
135 panic!("Should have failed, but succeeded: {extended_year} {month_code}");
136 }
137 Err(e) => {
138 panic!("Failed with wrong error: {extended_year} {month_code} {e:?}");
139 }
140 }
141 }
142 }
143
144 // Test with reference year
145 let mut fields = DateFields::default();
146 fields.day = Some(1);
147 let mut options = DateFromFieldsOptions::default();
148 options.missing_fields_strategy = Some(MissingFieldsStrategy::Ecma);
149 options.overflow = Some(Overflow::Constrain);
150 for month_code in valid_month_codes.iter() {
151 fields.month = Some(Month::try_from_str(month_code).unwrap());
152 match Date::try_from_fields(fields, options, cal) {
153 Ok(_) => (),
154 Err(e) => {
155 panic!(
156 "Should have succeeded, but failed: {month_code} {e:?} (reference year)"
157 );
158 }
159 }
160 }
161 for month_code in invalid_month_codes.iter() {
162 fields.month = Some(Month::try_from_str(month_code).unwrap());
163 match Date::try_from_fields(fields, options, cal) {
164 Err(DateFromFieldsError::MonthNotInCalendar) => (),
165 Ok(_) => {
166 panic!("Should have failed, but succeeded: {month_code} (reference year)");
167 }
168 Err(e) => {
169 panic!("Failed with wrong error: {month_code} {e:?} (reference year)");
170 }
171 }
172 }
173 }
174 );