Skip to content

Commit d29fd26

Browse files
committed
date: add tests for relative weekdays
1 parent 0ec36be commit d29fd26

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

tests/by-util/test_date.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chrono::{DateTime, Duration, Utc};
1+
use chrono::{Datelike, DateTime, Duration, NaiveTime, Utc};
22
// This file is part of the uutils coreutils package.
33
//
44
// For the full copyright and license information, please view the LICENSE
@@ -397,10 +397,13 @@ fn test_date_string_human() {
397397
"30 minutes ago",
398398
"10 seconds",
399399
"last day",
400+
"last monday",
400401
"last week",
401402
"last month",
402403
"last year",
404+
"this monday",
403405
"next day",
406+
"next monday",
404407
"next week",
405408
"next month",
406409
"next year",
@@ -440,6 +443,34 @@ fn test_negative_offset() {
440443
}
441444
}
442445

446+
#[test]
447+
fn test_relative_weekdays() {
448+
// Truncate time componenet to midnight
449+
let today = Utc::now().with_time(NaiveTime::MIN).unwrap();
450+
// Loop through each day of the week, starting with today
451+
for offset in 0..7 {
452+
for direction in ["last", "this", "next"] {
453+
let weekday = (today + Duration::days(offset)).weekday().to_string().to_lowercase();
454+
new_ucmd!()
455+
.arg("-d")
456+
.arg(format!("{} {}", direction, weekday))
457+
.arg("--rfc-3339=seconds")
458+
.arg("--utc")
459+
.succeeds()
460+
.stdout_str_check(|out| {
461+
let result = DateTime::parse_from_rfc3339(out.trim()).unwrap().to_utc();
462+
let expected = match (direction, offset) {
463+
("last", _) => today - Duration::days(7 - offset),
464+
("this", 0) => today,
465+
("next", 0) => today + Duration::days(7),
466+
_ => today + Duration::days(offset),
467+
};
468+
result == expected
469+
});
470+
}
471+
}
472+
}
473+
443474
#[test]
444475
fn test_invalid_date_string() {
445476
new_ucmd!()
@@ -448,6 +479,13 @@ fn test_invalid_date_string() {
448479
.fails()
449480
.no_stdout()
450481
.stderr_contains("invalid date");
482+
483+
new_ucmd!()
484+
.arg("-d")
485+
.arg("this fooday")
486+
.fails()
487+
.no_stdout()
488+
.stderr_contains("invalid date");
451489
}
452490

453491
#[test]

0 commit comments

Comments
 (0)