Skip to content

Commit

Permalink
fix(dtt): 🐛 removes the code duplication and keeps the tests clean an…
Browse files Browse the repository at this point in the history
…d maintainable
  • Loading branch information
sebastienrousseau committed Sep 2, 2024
1 parent 7ed92c5 commit 19db4fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
33 changes: 15 additions & 18 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use time::{
/// A structure representing a date and time with timezone information.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct DateTime {
/// datetime: The date and time in UTC.
/// The date and time in UTC.
pub datetime: PrimitiveDateTime,
/// offset: The timezone offset in UTC.
/// The timezone offset in UTC.
pub offset: UtcOffset,
}

Expand Down Expand Up @@ -716,8 +716,17 @@ impl DateTime {
self.offset
}

/// Returns the local time zone offset of the `DateTime` instance.
pub fn now(&self) -> Self {
/// Returns the current `DateTime` instance.
///
/// # Examples
///
/// ```
/// use dtt::datetime::DateTime;
///
/// let dt = DateTime::now();
/// println!("Current time: {}", dt);
/// ```
pub fn now() -> Self {
Self::new()
}

Expand Down Expand Up @@ -1083,18 +1092,6 @@ impl DateTime {
/// # Returns
///
/// * `Result<Self, DateTimeError>` - A new `DateTime` instance with the months added, or an error if the operation fails.
/// Adds a specified number of months to the `DateTime` instance.
///
/// This method handles edge cases such as month-end dates and leap years.
/// It also correctly handles negative month additions (subtractions).
///
/// # Arguments
///
/// * `months` - The number of months to add (can be negative for subtraction).
///
/// # Returns
///
/// * `Result<Self, DateTimeError>` - A new `DateTime` instance with the months added, or an error if the operation fails.
pub fn add_months(
&self,
months: i32,
Expand Down Expand Up @@ -1400,7 +1397,7 @@ impl Hash for DateTime {
}
}

/// Helper function to determine the number of days in a given month and year
/// Helper function to determine the number of days in a given month and year.
fn days_in_month(year: i32, month: u8) -> Result<u8, DateTimeError> {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => Ok(31),
Expand All @@ -1410,7 +1407,7 @@ fn days_in_month(year: i32, month: u8) -> Result<u8, DateTimeError> {
}
}

/// Helper function to determine if a year is a leap year
/// Helper function to determine if a year is a leap year.
fn is_leap_year(year: i32) -> bool {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
44 changes: 19 additions & 25 deletions tests/test_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,35 @@ mod tests {
cmd.output().expect("Failed to execute command")
}

// Helper function to run tests with the DTT test mode and verify the result
fn run_and_verify_test_mode(is_test_mode: bool, should_fail: bool) {
let output = run_dtt_with_test_mode(is_test_mode);

// Assert that the command execution matches the expected result
assert_eq!(output.status.success(), !should_fail);

// If the test is expected to fail, verify the error message
if should_fail {
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains("Error running dtt: Simulated error")
);
}
}

#[test]
fn test_run_with_dtt_test_mode() {
let output = run_dtt_with_test_mode(true);

// Assert that the command execution was not successful
assert!(!output.status.success());

// Assert that the error message was printed to stderr
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Error running dtt: Simulated error"));
run_and_verify_test_mode(true, true);
}

#[test]
fn test_run_without_dtt_test_mode() {
let output = run_dtt_with_test_mode(false);

// Assert that the command execution was successful
assert!(output.status.success());

// Assert that the welcome messages were printed to stdout
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(stdout.contains("Welcome to `DTT` 👋!"));
assert!(stdout.contains("A Rust library for parsing, validating, manipulating, and formatting dates and times."));
run_and_verify_test_mode(false, false);
}

#[test]
fn test_main() {
// Test calling the `run_dtt_with_test_mode()` function directly with test mode enabled
let output = run_dtt_with_test_mode(true);

// Assert that the command execution was not successful
assert!(!output.status.success());

// Assert that the error message was printed to stderr
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Error running dtt: Simulated error"));
run_and_verify_test_mode(true, true);
}
}

0 comments on commit 19db4fc

Please sign in to comment.