Skip to content

Commit

Permalink
test: time unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 22, 2024
1 parent 4d5d46e commit 571bfa6
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions crates/time/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,90 @@ impl std::ops::Deref for PaceTime {
&self.0
}
}

#[cfg(test)]
mod tests {

use chrono::NaiveDate;
use eyre::{OptionExt, Result};

use super::*;

#[test]
fn test_from_pace_date_time() -> Result<()> {
let date_time = PaceDateTime::try_from((
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date.")?,
NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?,
))?;

let time = PaceTime::from(date_time);

assert_eq!(
time,
PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?)
);

Ok(())
}

#[test]
fn test_from_pace_date_time_ref() -> Result<()> {
let date_time = PaceDateTime::try_from((
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date.")?,
NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?,
))?;

let time = PaceTime::from(&date_time);

assert_eq!(
time,
PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?)
);

Ok(())
}

#[test]
fn test_from_naive_time() -> Result<()> {
let time = PaceTime::from(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?);

assert_eq!(
time,
PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?)
);

Ok(())
}

#[test]
fn test_deref() -> Result<()> {
let time = PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?);

assert_eq!(
*time,
NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?
);

Ok(())
}

#[test]
fn test_eq() -> Result<()> {
let time = PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?);
let other_time = PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?);

assert_eq!(time, other_time);

Ok(())
}

#[test]
fn test_ord() -> Result<()> {
let time = PaceTime(NaiveTime::from_hms_opt(12, 0, 0).ok_or_eyre("Invalid time.")?);
let other_time = PaceTime(NaiveTime::from_hms_opt(12, 1, 0).ok_or_eyre("Invalid time.")?);

assert!(time < other_time);

Ok(())
}
}

0 comments on commit 571bfa6

Please sign in to comment.