Added tests and fixed timestamp handling
All checks were successful
/ publish (push) Successful in 47s
All checks were successful
/ publish (push) Successful in 47s
This commit is contained in:
parent
5c89c44ff7
commit
e74906e0c4
4 changed files with 164 additions and 34 deletions
113
cmds/timestamps/input_test.go
Normal file
113
cmds/timestamps/input_test.go
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
// Reference time: 2025-11-19T11:07:13Z
|
||||
// Unix: 1763550433
|
||||
refTime := time.Date(2025, 11, 19, 11, 7, 13, 0, time.UTC)
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected func() time.Time
|
||||
desc string
|
||||
}{
|
||||
{
|
||||
input: "1763550433",
|
||||
expected: func() time.Time {
|
||||
return refTime
|
||||
},
|
||||
desc: "Unix timestamp",
|
||||
},
|
||||
{
|
||||
input: "2025-11-19T11:07:13Z",
|
||||
expected: func() time.Time {
|
||||
return refTime
|
||||
},
|
||||
desc: "ISO 8601 UTC",
|
||||
},
|
||||
{
|
||||
input: "2025-11-19T11:07:13",
|
||||
expected: func() time.Time {
|
||||
return time.Date(2025, 11, 19, 11, 7, 13, 0, time.Local)
|
||||
},
|
||||
desc: "ISO 8601 Local",
|
||||
},
|
||||
{
|
||||
input: "2025-11-19 11:07:13Z",
|
||||
expected: func() time.Time {
|
||||
return refTime
|
||||
},
|
||||
desc: "Date Space Time Z",
|
||||
},
|
||||
{
|
||||
input: "2025-11-19",
|
||||
expected: func() time.Time {
|
||||
return time.Date(2025, 11, 19, 0, 0, 0, 0, time.Local)
|
||||
},
|
||||
desc: "Date only Local",
|
||||
},
|
||||
{
|
||||
input: "2025-11-19Z",
|
||||
expected: func() time.Time {
|
||||
return time.Date(2025, 11, 19, 0, 0, 0, 0, time.UTC)
|
||||
},
|
||||
desc: "Date only UTC",
|
||||
},
|
||||
{
|
||||
input: "11:07:13",
|
||||
expected: func() time.Time {
|
||||
now := time.Now()
|
||||
return time.Date(now.Year(), now.Month(), now.Day(), 11, 7, 13, 0, time.Local)
|
||||
},
|
||||
desc: "Time only Local",
|
||||
},
|
||||
{
|
||||
input: "11:07:13Z",
|
||||
expected: func() time.Time {
|
||||
now := time.Now().In(time.UTC)
|
||||
return time.Date(now.Year(), now.Month(), now.Day(), 11, 7, 13, 0, time.UTC)
|
||||
},
|
||||
desc: "Time only UTC",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
res, err := Parse(tc.input)
|
||||
if err != nil {
|
||||
t.Fatalf("Parse(%q) error: %v", tc.input, err)
|
||||
}
|
||||
got, err := res.Time()
|
||||
if err != nil {
|
||||
t.Fatalf("res.Time() error: %v", err)
|
||||
}
|
||||
|
||||
want := tc.expected()
|
||||
|
||||
// For "Time only" tests, there might be a slight race condition if the day changes between test execution and expected value generation.
|
||||
// But practically it's fine.
|
||||
|
||||
if !got.Truncate(time.Second).Equal(want) {
|
||||
t.Errorf("Parse(%q) = %v, want %v", tc.input, got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("now", func(t *testing.T) {
|
||||
res, err := Parse("now")
|
||||
if err != nil {
|
||||
t.Fatalf("Parse('now') error: %v", err)
|
||||
}
|
||||
got, err := res.Time()
|
||||
if err != nil {
|
||||
t.Fatalf("res.Time() error: %v", err)
|
||||
}
|
||||
if time.Since(got) > time.Second {
|
||||
t.Errorf("Parse('now') = %v, want close to now", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue