Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/time_stdlib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,107 @@ fn test_native_create_time_invalid_values() {
let result = native_create_time(args);
assert!(result.is_err(), "create_time should fail with second >= 60");
}

use chrono::{Local, NaiveDate};
use std::rc::Rc;
use wfl::stdlib::time::{native_add_days, native_create_date, native_days_between, native_today};

Comment on lines +92 to +95

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports are introduced mid-file and duplicate the existing wfl::stdlib::time import. For consistency with other test files, consider consolidating these use statements at the top of the module (and merging the native_create_time import with the other native_* imports).

Copilot uses AI. Check for mistakes.
#[test]
fn test_native_today() {
let expected = Local::now().date_naive();
let result = native_today(vec![]);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, expected);
Comment on lines +98 to +102

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_native_today can be flaky if it runs across a local midnight boundary: expected is captured before calling native_today(), which itself calls Local::now(). If the date flips between those two calls, the assertion will fail intermittently. Consider capturing before and after around the call and asserting the returned date matches either (or otherwise making the comparison robust to midnight rollover).

Suggested change
let expected = Local::now().date_naive();
let result = native_today(vec![]);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, expected);
// Capture the date immediately before calling native_today
let before = Local::now().date_naive();
let result = native_today(vec![]);
assert!(result.is_ok());
// Capture the date immediately after calling native_today
let after = Local::now().date_naive();
if let Value::Date(d) = result.unwrap() {
// Allow for the possibility that midnight passed between the calls
assert!(
*d == before || *d == after,
"native_today returned {d}, which is not equal to either the before ({before}) or after ({after}) date"
);

Copilot uses AI. Check for mistakes.
} else {
panic!("Expected Date variant");
}
}
Comment on lines +96 to +106

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Race condition at midnight can cause flaky test.

Both the test (line 98) and native_today (internally) call Local::now().date_naive() separately. If the test runs exactly at midnight, these calls may return different dates, causing spurious failures.

🛡️ Suggested fix: bracket the call with before/after timestamps
 #[test]
 fn test_native_today() {
-    let expected = Local::now().date_naive();
+    let before = Local::now().date_naive();
     let result = native_today(vec![]);
+    let after = Local::now().date_naive();
     assert!(result.is_ok());
     if let Value::Date(d) = result.unwrap() {
-        assert_eq!(*d, expected);
+        assert!(
+            *d == before || *d == after,
+            "Expected date to be {before} or {after}, got {d}"
+        );
     } else {
         panic!("Expected Date variant");
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[test]
fn test_native_today() {
let expected = Local::now().date_naive();
let result = native_today(vec![]);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, expected);
} else {
panic!("Expected Date variant");
}
}
#[test]
fn test_native_today() {
let before = Local::now().date_naive();
let result = native_today(vec![]);
let after = Local::now().date_naive();
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert!(
*d == before || *d == after,
"Expected date to be {before} or {after}, got {d}"
);
} else {
panic!("Expected Date variant");
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/time_stdlib_test.rs` around lines 96 - 106, The test test_native_today
has a race at midnight because it calls Local::now().date_naive() separately
from native_today; fix by bracketing the native_today call with two timestamps:
capture before = Local::now().date_naive(), call result = native_today(vec![]),
then after = Local::now().date_naive(); assert result is Ok(Value::Date(d)) and
that *d equals either before or after (handle the midnight boundary when before
!= after). Reference test_native_today, native_today, Local::now().date_naive(),
and Value::Date to locate and update the assertions accordingly.


#[test]
fn test_native_create_date_valid() {
let args = vec![
Value::Number(2023.0),
Value::Number(10.0),
Value::Number(25.0),
];
let result = native_create_date(args);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, NaiveDate::from_ymd_opt(2023, 10, 25).unwrap());
} else {
panic!("Expected Date variant");
}
}

#[test]
fn test_native_create_date_invalid_month() {
let args = vec![
Value::Number(2023.0),
Value::Number(13.0), // Invalid month
Value::Number(25.0),
];
let result = native_create_date(args);
assert!(result.is_err());
}

#[test]
fn test_native_create_date_invalid_day() {
let args = vec![
Value::Number(2023.0),
Value::Number(10.0),
Value::Number(32.0), // Invalid day
];
let result = native_create_date(args);
assert!(result.is_err());

let args_zero = vec![
Value::Number(2023.0),
Value::Number(10.0),
Value::Number(0.0), // Invalid day (too low)
];
let result_zero = native_create_date(args_zero);
assert!(result_zero.is_err());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

#[test]
fn test_native_add_days() {
let start_date = Rc::new(NaiveDate::from_ymd_opt(2023, 10, 25).unwrap());
let args = vec![Value::Date(start_date), Value::Number(5.0)];
let result = native_add_days(args);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, NaiveDate::from_ymd_opt(2023, 10, 30).unwrap());
} else {
panic!("Expected Date variant");
}
}

#[test]
fn test_native_add_days_negative() {
let start_date = Rc::new(NaiveDate::from_ymd_opt(2023, 10, 25).unwrap());
let args = vec![Value::Date(start_date), Value::Number(-5.0)];
let result = native_add_days(args);
assert!(result.is_ok());
if let Value::Date(d) = result.unwrap() {
assert_eq!(*d, NaiveDate::from_ymd_opt(2023, 10, 20).unwrap());
} else {
panic!("Expected Date variant");
}
}

#[test]
fn test_native_days_between() {
let date1 = Rc::new(NaiveDate::from_ymd_opt(2023, 10, 20).unwrap());
let date2 = Rc::new(NaiveDate::from_ymd_opt(2023, 10, 25).unwrap());

// date1 is older, date2 is newer
let args = vec![Value::Date(date1), Value::Date(date2)];
let result = native_days_between(args);
assert!(result.is_ok());
if let Value::Number(n) = result.unwrap() {
assert_eq!(n, 5.0);
} else {
panic!("Expected Number variant");
}
}
Loading