Skip to content

Commit c9f16f6

Browse files
test(dtt): ✅ add unit tests for macros.rs
1 parent 4f27938 commit c9f16f6

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

benches/criterion.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information.
4+
15
#![allow(missing_docs)]
26
use criterion::{
37
black_box, criterion_group, criterion_main, Criterion,

examples/dtt.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information.
4+
15
#![allow(missing_docs)]
26

37
use self::dtt::DateTime;

src/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
///
4343
/// A new `DateTime` instance.
4444
///
45+
/// # Examples
46+
/// ```
47+
/// use dtt::dtt_now;
48+
/// let now = dtt_now!();
49+
/// println!("Current date and time: {}", now.iso_8601);
50+
/// ```
4551
#[macro_export]
4652
macro_rules! dtt_now {
4753
() => {{

tests/test_lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information.
4+
15
#[cfg(test)]
26

37
mod tests {

tests/test_macros.rs

+67
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information.
4+
15
#[cfg(test)]
26
mod tests {
7+
use dtt::is_valid;
38
use dtt::DateTime;
49
use dtt::*;
510

@@ -276,4 +281,66 @@ mod tests {
276281
// Pass an input that cannot be parsed as an i32 to trigger the Err(_) branch.
277282
assert!(!integer("not_a_number"));
278283
}
284+
285+
#[test]
286+
fn test_dtt_now() {
287+
use dtt::dtt_now;
288+
let now = dtt_now!();
289+
println!("Current date and time: {}", now.iso_8601);
290+
}
291+
292+
#[test]
293+
fn test_dtt_parse() {
294+
let input = "2022-01-01T12:00:00+01:00";
295+
match dtt_parse!(input) {
296+
Ok(date) => {
297+
assert_eq!(date.year, 2022);
298+
assert_eq!(date.month.parse::<u8>().unwrap(), 1);
299+
assert_eq!(date.day, 1);
300+
assert_eq!(date.hour, 12);
301+
assert_eq!(date.minute, 0);
302+
assert_eq!(date.second, 0);
303+
}
304+
Err(err) => panic!("Parsing failed: {}", err),
305+
}
306+
}
307+
308+
#[test]
309+
fn test_dtt_add_days() {
310+
let dt = DateTime::parse("2023-01-01T12:00:00+00:00").unwrap();
311+
let future_date = dtt_add_days!(dt, 5);
312+
assert_eq!(future_date.day, 6);
313+
}
314+
315+
#[test]
316+
fn test_dtt_diff_hours() {
317+
let dt1 = "2023-01-01T12:00:00+00:00";
318+
let dt2 = "2023-01-02T12:00:00+00:00";
319+
let hours_difference = dtt_diff_hours!(dt1, dt2);
320+
assert!(hours_difference > 23.to_string());
321+
}
322+
323+
#[test]
324+
fn test_dtt_diff_minutes() {
325+
let dt1 = "2023-01-01T12:00:00+00:00";
326+
let dt2 = "2023-01-02T12:00:00+00:00";
327+
let minutes_difference = dtt_diff_minutes!(dt1, dt2);
328+
assert!(minutes_difference > 60.to_string());
329+
}
330+
331+
#[test]
332+
fn test_dtt_diff_seconds() {
333+
let dt1 = "1640995200"; // Unix timestamp for 2023-01-01T12:00:00Z
334+
let dt2 = "1640995230"; // Unix timestamp for 2023-01-01T12:00:30Z
335+
let seconds_difference = dtt_diff_seconds!(dt1, dt2);
336+
assert_eq!(seconds_difference, "30");
337+
}
338+
339+
#[test]
340+
fn test_dtt_diff_days() {
341+
let dt1 = "1640995200"; // Unix timestamp for 2023-01-01T00:00:00Z
342+
let dt2 = "1641081600"; // Unix timestamp for 2023-01-02T00:00:00Z
343+
let days_difference = dtt_diff_days!(dt1, dt2);
344+
assert_eq!(days_difference, 1);
345+
}
279346
}

tests/test_main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information.
4+
15
#[cfg(test)]
26
mod tests {
37
use assert_cmd::prelude::*;

0 commit comments

Comments
 (0)