Skip to content

Commit 49f0f26

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 8a9dafe commit 49f0f26

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
@@ -1428,3 +1428,31 @@ fn test_format_upper_c_locale_expansion() {
14281428
out.contains("MON") && out.contains("JAN")
14291429
});
14301430
}
1431+
1432+
#[test]
1433+
#[cfg(any(
1434+
target_os = "linux",
1435+
target_vendor = "apple",
1436+
target_os = "freebsd",
1437+
target_os = "netbsd",
1438+
target_os = "openbsd",
1439+
target_os = "dragonfly"
1440+
))]
1441+
fn test_format_upper_c_locale_expansion_french() {
1442+
let result = new_ucmd!()
1443+
.env("LC_ALL", "fr_FR.UTF-8")
1444+
.arg("-d")
1445+
.arg("2024-01-01T13:00:00")
1446+
.arg("+%^c")
1447+
.run();
1448+
1449+
let stdout = result.stdout_str();
1450+
1451+
// Should have 13:00 (not 1:00)
1452+
assert!(
1453+
stdout.contains("13:00"),
1454+
"French locale should show 13:00 for 1 PM, got: {stdout}"
1455+
);
1456+
1457+
assert_eq!(stdout.to_uppercase(), stdout, "Output should be uppercase");
1458+
}

0 commit comments

Comments
 (0)