Skip to content

Commit f3ab2c5

Browse files
committed
date: address review comments (docs and tests)
- Add documentation for distribute_flag helper. - Add unit tests for flag distribution and locale expansion. - Add integration test for locale expansion with uppercase flag.
1 parent db165e9 commit f3ab2c5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/uu/date/src/locale.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ cfg_langinfo! {
135135
}
136136
}
137137

138+
/// Distributes the given flag to all format specifiers in the string.
139+
///
140+
/// For example, if the format string is "%a %b" and the flag is '^',
141+
/// the result will be "%^a %^b". Literal percent signs ("%%") are skipped
142+
/// and left as is.
138143
fn distribute_flag(fmt: &str, flag: char) -> String {
139144
let mut res = String::with_capacity(fmt.len() * 2);
140145
let mut chars = fmt.chars().peekable();
@@ -367,4 +372,43 @@ mod tests {
367372
);
368373
}
369374
}
375+
376+
#[test]
377+
#[cfg(any(
378+
target_os = "linux",
379+
target_vendor = "apple",
380+
target_os = "freebsd",
381+
target_os = "netbsd",
382+
target_os = "openbsd",
383+
target_os = "dragonfly"
384+
))]
385+
fn test_distribute_flag() {
386+
// Standard distribution
387+
assert_eq!(distribute_flag("%a %b", '^'), "%^a %^b");
388+
// Ignore literals
389+
assert_eq!(distribute_flag("foo %a bar", '_'), "foo %_a bar");
390+
// Skip escaped percent signs
391+
assert_eq!(distribute_flag("%% %a", '^'), "%% %^a");
392+
// Handle flags that might already exist
393+
assert_eq!(distribute_flag("%_a", '^'), "%^_a");
394+
}
395+
396+
#[test]
397+
#[cfg(any(
398+
target_os = "linux",
399+
target_vendor = "apple",
400+
target_os = "freebsd",
401+
target_os = "netbsd",
402+
target_os = "openbsd",
403+
target_os = "dragonfly"
404+
))]
405+
fn test_expand_locale_format_basic() {
406+
let format = "%^x";
407+
let expanded = expand_locale_format(format).to_string().to_lowercase();
408+
409+
assert_ne!(expanded, "%^x", "Should have expanded %^x");
410+
assert!(expanded.contains("%^d"), "Should contain %^d");
411+
assert!(expanded.contains("%^m"), "Should contain %^m");
412+
assert!(expanded.contains("%^y"), "Should contain %^y");
413+
}
370414
}

tests/by-util/test_date.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,3 +1521,31 @@ fn test_format_upper_c_locale_expansion() {
15211521
out.contains("MON") && out.contains("JAN")
15221522
});
15231523
}
1524+
1525+
#[test]
1526+
#[cfg(any(
1527+
target_os = "linux",
1528+
target_vendor = "apple",
1529+
target_os = "freebsd",
1530+
target_os = "netbsd",
1531+
target_os = "openbsd",
1532+
target_os = "dragonfly"
1533+
))]
1534+
fn test_format_upper_c_locale_expansion_french() {
1535+
let result = new_ucmd!()
1536+
.env("LC_ALL", "fr_FR.UTF-8")
1537+
.arg("-d")
1538+
.arg("2024-01-01T13:00:00")
1539+
.arg("+%^c")
1540+
.run();
1541+
1542+
let stdout = result.stdout_str();
1543+
1544+
// Should have 13:00 (not 1:00)
1545+
assert!(
1546+
stdout.contains("13:00"),
1547+
"French locale should show 13:00 for 1 PM, got: {stdout}"
1548+
);
1549+
1550+
assert_eq!(stdout.to_uppercase(), stdout, "Output should be uppercase");
1551+
}

0 commit comments

Comments
 (0)