-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add unit tests for untested stdlib::time native functions
#434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6678a48
61e28e1
45b9ae9
0fb1aea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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" | |
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| #[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.
There was a problem hiding this comment.
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::timeimport. For consistency with other test files, consider consolidating theseusestatements at the top of the module (and merging thenative_create_timeimport with the othernative_*imports).