Skip to content

Commit

Permalink
test: date and duration 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 8154a14 commit 4d5d46e
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ ci = ["github"]
# The installers to generate for each app
installers = ["shell", "powershell", "homebrew", "msi"]
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]
targets = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
"x86_64-pc-windows-msvc",
]
# Publish jobs to run in CI
pr-run-mode = "plan"
# A GitHub repo to push Homebrew formulas to
Expand Down
142 changes: 141 additions & 1 deletion crates/time/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_derive::{Deserialize, Serialize};

use crate::{date_time::PaceDateTime, error::PaceTimeErrorKind};

/// `PaceDate`: {0}
/// {0}
#[derive(
Debug,
Serialize,
Expand Down Expand Up @@ -104,3 +104,143 @@ impl std::ops::Deref for PaceDate {
&self.0
}
}

#[cfg(test)]
mod tests {

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

use super::*;

#[test]
fn test_pace_date_from_str_passes() -> Result<()> {
let date = PaceDate::from_str("2021-01-01")?;
assert_eq!(
date.0,
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?
);

Ok(())
}

#[test]
fn test_pace_date_from_str_fails() {
let date = PaceDate::from_str("2021-01-32");
assert!(date.is_err());
}

#[test]
fn test_pace_date_try_from_ymd_passes() -> Result<()> {
let date = PaceDate::try_from((2021, 1, 1))?;
assert_eq!(
date.0,
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?
);

Ok(())
}

#[test]
fn test_pace_date_try_from_fails() {
let date = PaceDate::try_from((2021, 1, 32));
assert!(date.is_err());
}

#[test]
fn test_pace_date_is_future_is_false_passes() -> Result<()> {
let date = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
assert!(!date.is_future());

Ok(())
}

#[test]
fn test_pace_date_is_future_is_true_passes() -> Result<()> {
let date = PaceDate::new(NaiveDate::from_ymd_opt(2079, 1, 2).ok_or_eyre("Invalid date")?);
assert!(date.is_future());

Ok(())
}

#[test]
fn test_pace_date_with_start_passes() {
let date = PaceDate::with_start();
assert_eq!(date.0, NaiveDate::default());
}

#[test]
fn test_pace_date_default_passes() {
let date = PaceDate::default();
assert_eq!(date.0, Local::now().naive_local().date());
}

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

let date = PaceDate::from(date_time);
assert_eq!(
date.0,
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?
);

Ok(())
}

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

let date = PaceDate::from(&date_time);
assert_eq!(
date.0,
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?
);

Ok(())
}

#[test]
fn test_pace_date_deref_passes() -> Result<()> {
let date = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
assert_eq!(
*date,
NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?
);

Ok(())
}

#[test]
fn test_pace_date_display_passes() -> Result<()> {
let date = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
assert_eq!(format!("{date}"), "2021-01-01");

Ok(())
}

#[test]
fn test_pace_date_eq_passes() -> Result<()> {
let date1 = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
let date2 = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
assert_eq!(date1, date2);

Ok(())
}

#[test]
fn test_pace_date_ne_passes() -> Result<()> {
let date1 = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 1).ok_or_eyre("Invalid date")?);
let date2 = PaceDate::new(NaiveDate::from_ymd_opt(2021, 1, 2).ok_or_eyre("Invalid date")?);
assert_ne!(date1, date2);

Ok(())
}
}
68 changes: 66 additions & 2 deletions crates/time/src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ mod tests {

use super::*;

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

#[test]
fn test_duration_to_str_passes() {
Expand Down Expand Up @@ -369,4 +369,68 @@ mod tests {

assert!(duration.is_err());
}

#[test]
fn test_pace_duration_as_secs_passes() {
let duration = PaceDuration::new(1);

assert_eq!(duration.as_secs(), 1);
}

#[test]
fn test_pace_duration_as_duration_passes() {
let duration = PaceDuration::new(1);

assert_eq!(duration.as_duration(), Duration::from_secs(1));
}

#[test]
fn test_pace_duration_as_minutes_passes() {
let error_margin = f64::EPSILON;
let duration = PaceDuration::new(60);

assert!((duration.as_minutes() - 1.0).abs() < error_margin);
}

#[test]
fn test_pace_duration_as_hours_passes() {
let error_margin = f64::EPSILON;
let duration = PaceDuration::new(3600);

assert!((duration.as_hours() - 1.0).abs() < error_margin);
}

#[test]
fn test_pace_duration_as_days_passes() {
let error_margin = f64::EPSILON;

let duration = PaceDuration::new(86_400);

assert!((duration.as_days() - 1.0).abs() < error_margin);
}

#[test]
fn test_pace_duration_as_weeks_passes() {
let error_margin = f64::EPSILON;
let duration = PaceDuration::new(604_800);

assert!((duration.as_weeks() - 1.0).abs() < error_margin);
}

#[test]
fn test_pace_duration_from_seconds_passes() {
let duration = PaceDuration::from_seconds(1);

assert_eq!(duration, PaceDuration::new(1));
}

#[test]
fn test_pace_duration_try_from_passes() -> Result<()> {
let duration = TimeDelta::try_seconds(1).ok_or_eyre("Invalid time delta.")?;
let result = PaceDuration::try_from(duration)?;

assert_eq!(result, PaceDuration::new(1));

Ok(())
}
}
1 change: 1 addition & 0 deletions crates/time/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::NaiveDate;

#[cfg(feature = "clap")]
use clap::{Parser, ValueEnum};

use getset::{Getters, MutGetters, Setters};
use typed_builder::TypedBuilder;

Expand Down

0 comments on commit 4d5d46e

Please sign in to comment.