Skip to content

Commit 127865e

Browse files
committed
Some basic tests on ParsedDate.shift()
1 parent b082079 commit 127865e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ authors = ["Steve Cook <[email protected]>"]
77
chrono = "0.3"
88
nom = "^2.1"
99
time = "^0.1.36"
10+
11+
[[bin]]
12+
name = "main"
13+
doc = false

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,28 @@ mod tests {
116116
assert_eq!(date, ParsedDate::from_ymd(2017, 1, 1));
117117
date.shift(Period::Day, -1);
118118
assert_eq!(date, ParsedDate::from_ymd(2016, 12, 31));
119+
date.shift(Period::Week, 2);
120+
assert_eq!(date, ParsedDate::from_ymd(2017, 1, 14));
121+
date.shift(Period::Month, 2);
122+
assert_eq!(date, ParsedDate::from_ymd(2017, 3, 14));
123+
date.shift(Period::Month, -1);
124+
assert_eq!(date, ParsedDate::from_ymd(2017, 2, 14));
125+
date.shift(Period::Week, -2);
126+
assert_eq!(date, ParsedDate::from_ymd(2017, 1, 31));
127+
// Adding a month that shifts you into a non-existent
128+
// day gives you the last day of the next month.
129+
date.shift(Period::Month, 1);
130+
assert_eq!(date, ParsedDate::from_ymd(2017, 2, 28));
131+
date.shift(Period::Year, 1);
132+
assert_eq!(date, ParsedDate::from_ymd(2018, 2, 28));
133+
date.shift(Period::Year, -1);
134+
assert_eq!(date, ParsedDate::from_ymd(2017, 2, 28));
135+
}
136+
137+
#[test]
138+
fn date_shift_leap_year() {
139+
let mut date = ParsedDate::from_ymd(2016, 2, 29);
140+
date.shift(Period::Year, 1);
141+
assert_eq!(date, ParsedDate::from_ymd(2017, 3, 1));
119142
}
120143
}

0 commit comments

Comments
 (0)