diff --git a/libimagentrydatetime/Cargo.toml b/libimagentrydatetime/Cargo.toml index ec3b281d..2b311de7 100644 --- a/libimagentrydatetime/Cargo.toml +++ b/libimagentrydatetime/Cargo.toml @@ -19,6 +19,9 @@ toml-query = "0.1" lazy_static = "0.2" toml = "0.4" +[dev-dependencies] +is-match = "0.1" + [dependencies.libimagerror] path = "../libimagerror" diff --git a/libimagentrydatetime/src/lib.rs b/libimagentrydatetime/src/lib.rs index 0e1a4006..fb0d73fa 100644 --- a/libimagentrydatetime/src/lib.rs +++ b/libimagentrydatetime/src/lib.rs @@ -26,6 +26,9 @@ extern crate toml; extern crate libimagstore; extern crate libimagutil; +#[cfg(test)] +#[macro_use] extern crate is_match; + pub mod datepath; pub mod datetime; pub mod error; diff --git a/libimagentrydatetime/src/range.rs b/libimagentrydatetime/src/range.rs index 017ee2fc..3a94faff 100644 --- a/libimagentrydatetime/src/range.rs +++ b/libimagentrydatetime/src/range.rs @@ -66,3 +66,46 @@ impl DateTimeRange { } +#[cfg(test)] +mod tests { + + use chrono::naive::datetime::NaiveDateTime; + use chrono::naive::date::NaiveDate; + use chrono::naive::time::NaiveTime; + + use super::DateTimeRange; + + #[test] + fn test_new_returns_error_if_start_after_end_date() { + let start = NaiveDateTime::new( + NaiveDate::from_ymd(2000, 02, 02), + NaiveTime::from_hms(12, 00, 02) + ); + + let end = NaiveDateTime::new( + NaiveDate::from_ymd(2000, 02, 02), + NaiveTime::from_hms(12, 00, 01) + ); + + let res = DateTimeRange::new(start, end); + + assert!(res.is_err()); + } + + #[test] + fn test_new_returns_ok_if_start_is_before_end() { + let start = NaiveDateTime::new( + NaiveDate::from_ymd(2000, 02, 02), + NaiveTime::from_hms(12, 00, 01) + ); + + let end = NaiveDateTime::new( + NaiveDate::from_ymd(2000, 02, 02), + NaiveTime::from_hms(12, 00, 02) + ); + + let res = DateTimeRange::new(start, end); + + assert!(res.is_ok()); + } +}