Skip to content

Commit 2582fed

Browse files
committed
test(date): use parse_date for test helper timestamp
Replace manual jiff::civil::date construction with parse_date in expand_format_with_test_date helper to ensure proper timestamp initialization for strtime formatting. Fixes #9654
1 parent 502f3b1 commit 2582fed

File tree

2 files changed

+73
-23
lines changed

2 files changed

+73
-23
lines changed

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ shortcodes
136136
siginfo
137137
sigusr
138138
strcasecmp
139+
strtime
139140
subcommand
140141
subexpression
141142
submodule

src/uu/date/src/locale.rs

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ 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::fmt::strtime;
122+
123+
// Create test timestamp: Monday, January 15, 2024, 14:30:45 UTC
124+
// Use parse_date to get a proper Zoned timestamp (same as production code)
125+
let test_date = match crate::parse_date("2024-01-15 14:30:45 UTC") {
126+
Ok(zoned) => zoned,
127+
Err(_) => return String::new(),
128+
};
129+
130+
// Expand the format string with the test date
131+
strtime::format(format, &test_date).unwrap_or_default()
132+
}
133+
115134
#[test]
116135
fn test_locale_detection() {
117136
// Just verify the function doesn't panic
@@ -121,10 +140,31 @@ mod tests {
121140
#[test]
122141
fn test_default_format_contains_valid_codes() {
123142
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
143+
144+
let expanded = expand_format_with_test_date(format);
145+
146+
// Verify expanded output contains expected components
147+
// Test date: Monday, January 15, 2024, 14:30:45
148+
assert!(
149+
expanded.contains("Mon") || expanded.contains("Monday"),
150+
"Expanded format should contain weekday name, got: {expanded}"
151+
);
152+
153+
assert!(
154+
expanded.contains("Jan") || expanded.contains("January"),
155+
"Expanded format should contain month name, got: {expanded}"
156+
);
157+
158+
assert!(
159+
expanded.contains("2024") || expanded.contains("24"),
160+
"Expanded format should contain year, got: {expanded}"
161+
);
162+
163+
// Keep literal %Z check - this is enforced by ensure_timezone_in_format()
164+
assert!(
165+
format.contains("%Z"),
166+
"Format string must contain %Z timezone (enforced by ensure_timezone_in_format)"
167+
);
128168
}
129169

130170
#[test]
@@ -135,25 +175,34 @@ mod tests {
135175
// The format should not be empty
136176
assert!(!format.is_empty(), "Locale format should not be empty");
137177

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");
178+
let expanded = expand_format_with_test_date(format);
179+
180+
// Verify expanded output contains date components
181+
// Test date: Monday, January 15, 2024
182+
let has_date_component = expanded.contains("15") // day
183+
|| expanded.contains("Jan") // month name
184+
|| expanded.contains("January") // full month
185+
|| expanded.contains("Mon") // weekday
186+
|| expanded.contains("Monday"); // full weekday
187+
188+
assert!(
189+
has_date_component,
190+
"Expanded format should contain date components, got: {expanded}"
191+
);
192+
193+
// Verify expanded output contains time components
194+
// Test time: 14:30:45
195+
let has_time_component = expanded.contains("14") // 24-hour
196+
|| expanded.contains("02") // 12-hour
197+
|| expanded.contains("30") // minutes
198+
|| expanded.contains(':') // time separator
199+
|| expanded.contains("PM") // AM/PM indicator
200+
|| expanded.contains("pm");
201+
202+
assert!(
203+
has_time_component,
204+
"Expanded format should contain time components, got: {expanded}"
205+
);
157206
}
158207

159208
#[test]

0 commit comments

Comments
 (0)