summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordfhoughton <dfhoughton@gmail.com>2019-02-03 09:39:24 -0500
committerdfhoughton <dfhoughton@gmail.com>2019-02-03 09:39:24 -0500
commitdc9a0fd4150ffe97f34303c4401490c00b7d640f (patch)
tree1be58f46f0d34173d4f1ede7c04c7974144342d2 /tests
parent2a9d017742aaceb4c61ca4d567c00e2855541002 (diff)
downloadtwo-timer-dc9a0fd4150ffe97f34303c4401490c00b7d640f.zip
added relative displacement pattern
Diffstat (limited to 'tests')
-rw-r--r--tests/tests.rs65
1 files changed, 64 insertions, 1 deletions
diff --git a/tests/tests.rs b/tests/tests.rs
index 51d466c..67d9fe5 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -1331,4 +1331,67 @@ fn midnight() {
assert!(false, "didn't match");
}
}
-} \ No newline at end of file
+}
+
+#[derive(Debug)]
+enum Period {
+ Week,
+ Day,
+ Hour,
+ Minute,
+ Second,
+}
+
+#[test]
+fn displacement() {
+ let displacements = [
+ ("week", Period::Week),
+ ("day", Period::Day),
+ ("hour", Period::Hour),
+ ("minute", Period::Minute),
+ ("second", Period::Second),
+ ];
+ let now = NaiveDate::from_ymd(1969, 5, 10).and_hms(0, 0, 0);
+ for (phrase, period) in displacements.iter() {
+ for n in [1, 2, 3].iter() {
+ let phrase = if *n == 1 {
+ String::from(*phrase)
+ } else {
+ String::from(*phrase) + "s"
+ };
+ let (displacement1, displacement2) = match period {
+ Period::Week => (Duration::weeks(*n), Duration::weeks(1)),
+ Period::Day => (Duration::days(*n), Duration::days(1)),
+ Period::Hour => (Duration::hours(*n), Duration::hours(1)),
+ Period::Minute => (Duration::minutes(*n), Duration::minutes(1)),
+ _ => (Duration::seconds(*n), Duration::seconds(1)),
+ };
+ let d1 = now - displacement1;
+ let d2 = d1 + displacement2;
+ let expression = format!("{} {} ago", n, phrase);
+ match parse(&expression, Some(Config::new().now(now))) {
+ Ok((start, end, _)) => {
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+ }
+ Err(e) => {
+ println!("{:?}", e);
+ assert!(false, "didn't match");
+ }
+ }
+ let d1 = now + displacement1;
+ let d2 = d1 + displacement2;
+ let expression = format!("{} {} from now", n, phrase);
+ match parse(&expression, Some(Config::new().now(now))) {
+ Ok((start, end, _)) => {
+ assert_eq!(d1, start);
+ assert_eq!(d2, end);
+ }
+ Err(e) => {
+ println!("{:?}", e);
+ assert!(false, "didn't match");
+ }
+ }
+ }
+ }
+}