diff --git a/src/datetime.rs b/src/datetime.rs index 24e3b9e..0c0a336 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -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, } @@ -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() } @@ -1083,18 +1092,6 @@ impl DateTime { /// # Returns /// /// * `Result` - 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` - A new `DateTime` instance with the months added, or an error if the operation fails. pub fn add_months( &self, months: i32, @@ -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 { match month { 1 | 3 | 5 | 7 | 8 | 10 | 12 => Ok(31), @@ -1410,7 +1407,7 @@ fn days_in_month(year: i32, month: u8) -> Result { } } -/// 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) } diff --git a/tests/test_main.rs b/tests/test_main.rs index f28c2c8..79c7d01 100644 --- a/tests/test_main.rs +++ b/tests/test_main.rs @@ -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); } }