Skip to content

Commit f29fbde

Browse files
committed
refactor(date): extract helper functions for date/time component checks in tests
- Add `has_date_component` and `has_time_component` functions to reduce duplication - Refactor test assertions to use these helpers for better readability and maintainability - Ensure locale format validations check for comprehensive date, time, and timezone components
1 parent 36e8048 commit f29fbde

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/uu/date/src/locale.rs

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

115+
fn has_date_component(format: &str) -> bool {
116+
format.contains("%a")
117+
|| format.contains("%A")
118+
|| format.contains("%b")
119+
|| format.contains("%B")
120+
|| format.contains("%d")
121+
|| format.contains("%e")
122+
|| format.contains("%m")
123+
|| format.contains("%Y")
124+
|| format.contains("%y")
125+
|| format.contains("%F")
126+
|| format.contains("%D")
127+
|| format.contains("%v")
128+
|| format.contains("%x")
129+
|| format.contains("%c")
130+
}
131+
132+
fn has_time_component(format: &str) -> bool {
133+
format.contains("%H")
134+
|| format.contains("%I")
135+
|| format.contains("%k")
136+
|| format.contains("%l")
137+
|| format.contains("%M")
138+
|| format.contains("%S")
139+
|| format.contains("%r")
140+
|| format.contains("%R")
141+
|| format.contains("%T")
142+
|| format.contains("%X")
143+
|| format.contains("%c")
144+
}
145+
115146
#[test]
116147
fn test_locale_detection() {
117148
// Just verify the function doesn't panic
@@ -121,10 +152,18 @@ mod tests {
121152
#[test]
122153
fn test_default_format_contains_valid_codes() {
123154
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
155+
assert!(
156+
has_date_component(format),
157+
"Format should contain date components"
158+
);
159+
assert!(
160+
has_time_component(format),
161+
"Format should contain time components"
162+
);
163+
assert!(
164+
format.contains("%Z") || format.contains("%z"),
165+
"Format should contain timezone indicator"
166+
);
128167
}
129168

130169
#[test]
@@ -136,24 +175,16 @@ mod tests {
136175
assert!(!format.is_empty(), "Locale format should not be empty");
137176

138177
// 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");
178+
assert!(
179+
has_date_component(format),
180+
"Format should contain date components"
181+
);
146182

147183
// 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");
184+
assert!(
185+
has_time_component(format),
186+
"Format should contain time components"
187+
);
157188
}
158189

159190
#[test]

0 commit comments

Comments
 (0)