Skip to content

Commit 933541a

Browse files
committed
test(date): validate locale formats via runtime expansion
Replace literal format code checks with validation of expanded output. Fixes test failures on macOS 15.7.2 where nl_langinfo returns composite format codes instead of explicit ones. Fixes #9654
1 parent 502f3b1 commit 933541a

File tree

1 file changed

+74
-23
lines changed

1 file changed

+74
-23
lines changed

src/uu/date/src/locale.rs

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ mod tests {
112112
cfg_langinfo! {
113113
use super::*;
114114

115+
/// Helper function to expand a format string with a known test date
116+
///
117+
/// Uses a fixed test date: Monday, January 15, 2024, 14:30:45 UTC
118+
/// This allows us to validate format strings by checking their expanded output
119+
/// rather than looking for literal format codes.
120+
fn expand_format_with_test_date(format: &str) -> String {
121+
use jiff::civil::date;
122+
use jiff::fmt::strtime;
123+
124+
// Create test timestamp: Monday, January 15, 2024, 14:30:45 UTC
125+
let test_date = match date(2024, 1, 15).at(14, 30, 45, 0).in_tz("UTC") {
126+
Ok(zoned) => zoned,
127+
Err(_) => return String::new(),
128+
};
129+
130+
// Expand the format string with the test date
131+
match strtime::format(format, &test_date) {
132+
Ok(expanded) => expanded,
133+
Err(_) => String::new(),
134+
}
135+
}
136+
115137
#[test]
116138
fn test_locale_detection() {
117139
// Just verify the function doesn't panic
@@ -121,10 +143,30 @@ mod tests {
121143
#[test]
122144
fn test_default_format_contains_valid_codes() {
123145
let format = get_locale_default_format();
124-
assert!(format.contains("%a")); // abbreviated weekday
125-
assert!(format.contains("%b")); // abbreviated month
126-
assert!(format.contains("%Y") || format.contains("%y")); // year (4-digit or 2-digit)
127-
assert!(format.contains("%Z")); // timezone
146+
let expanded = expand_format_with_test_date(&format);
147+
148+
// Verify expanded output contains expected components
149+
// Test date: Monday, January 15, 2024, 14:30:45
150+
assert!(
151+
expanded.contains("Mon") || expanded.contains("Monday"),
152+
"Expanded format should contain weekday name, got: {}", expanded
153+
);
154+
155+
assert!(
156+
expanded.contains("Jan") || expanded.contains("January"),
157+
"Expanded format should contain month name, got: {}", expanded
158+
);
159+
160+
assert!(
161+
expanded.contains("2024") || expanded.contains("24"),
162+
"Expanded format should contain year, got: {}", expanded
163+
);
164+
165+
// Keep literal %Z check - this is enforced by ensure_timezone_in_format()
166+
assert!(
167+
format.contains("%Z"),
168+
"Format string must contain %Z timezone (enforced by ensure_timezone_in_format)"
169+
);
128170
}
129171

130172
#[test]
@@ -135,25 +177,34 @@ mod tests {
135177
// The format should not be empty
136178
assert!(!format.is_empty(), "Locale format should not be empty");
137179

138-
// Should contain date/time components
139-
let has_date_component = format.contains("%a")
140-
|| format.contains("%A")
141-
|| format.contains("%b")
142-
|| format.contains("%B")
143-
|| format.contains("%d")
144-
|| format.contains("%e");
145-
assert!(has_date_component, "Format should contain date components");
146-
147-
// Should contain time component (hour)
148-
let has_time_component = format.contains("%H")
149-
|| format.contains("%I")
150-
|| format.contains("%k")
151-
|| format.contains("%l")
152-
|| format.contains("%r")
153-
|| format.contains("%R")
154-
|| format.contains("%T")
155-
|| format.contains("%X");
156-
assert!(has_time_component, "Format should contain time components");
180+
let expanded = expand_format_with_test_date(&format);
181+
182+
// Verify expanded output contains date components
183+
// Test date: Monday, January 15, 2024
184+
let has_date_component = expanded.contains("15") // day
185+
|| expanded.contains("Jan") // month name
186+
|| expanded.contains("January") // full month
187+
|| expanded.contains("Mon") // weekday
188+
|| expanded.contains("Monday"); // full weekday
189+
190+
assert!(
191+
has_date_component,
192+
"Expanded format should contain date components, got: {}", expanded
193+
);
194+
195+
// Verify expanded output contains time components
196+
// Test time: 14:30:45
197+
let has_time_component = expanded.contains("14") // 24-hour
198+
|| expanded.contains("02") // 12-hour
199+
|| expanded.contains("30") // minutes
200+
|| expanded.contains(":") // time separator
201+
|| expanded.contains("PM") // AM/PM indicator
202+
|| expanded.contains("pm");
203+
204+
assert!(
205+
has_time_component,
206+
"Expanded format should contain time components, got: {}", expanded
207+
);
157208
}
158209

159210
#[test]

0 commit comments

Comments
 (0)