Skip to content

Commit

Permalink
✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zcubbs committed Aug 15, 2023
1 parent 08a6fbe commit 1bd0e50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: '1.20'
stable: false

- name: Install Task
uses: arduino/setup-task@v1
Expand Down
11 changes: 6 additions & 5 deletions pkg/until/until.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"time"
)

func TimeUntil(t time.Time) string {
now := time.Now()

func TimeUntil(now, t time.Time) string {
// if the time is in the past, return "Time Passed"
if t.Before(now) {
return "Time Passed"
Expand Down Expand Up @@ -35,6 +33,9 @@ func TimeUntil(t time.Time) string {
return "Less than a day remaining"
}

func HasDatePassed(t time.Time) bool {
return time.Now().After(t)
func HasDatePassed(now, t time.Time) bool {
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())

return now.After(t)
}
26 changes: 15 additions & 11 deletions pkg/until/until_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,42 @@ import (
)

func TestTimeUntil(t *testing.T) {
now := time.Date(2023, time.August, 11, 12, 0, 0, 0, time.UTC)
tests := []struct {
time time.Time
expected string
}{
{time.Now().Add(-time.Hour), "Time Passed"},
{time.Now().Add(time.Hour), "Less than a day remaining"},
{time.Now().Add(24 * time.Hour), "1 day(s) remaining"},
{time.Now().Add(7 * 24 * time.Hour), "1 week(s) remaining"},
{time.Now().Add(30 * 24 * time.Hour), "1 month(s) remaining"},
{now.Add(-time.Hour), "Time Passed"},
{now.Add(time.Hour), "Less than a day remaining"},
{now.Add(24 * time.Hour), "1 day(s) remaining"},
{now.Add(7 * 24 * time.Hour), "1 week(s) remaining"},
{now.Add(30 * 24 * time.Hour), "1 month(s) remaining"},
}

for _, tt := range tests {
actual := TimeUntil(tt.time)
actual := TimeUntil(now, tt.time)
if actual != tt.expected {
t.Errorf("TimeUntil(%v) = %v; want %v", tt.time, actual, tt.expected)
t.Errorf("TimeUntil(%v, %v) = %v; want %v", now, tt.time, actual, tt.expected)
}
}
}

func TestHasDatePassed(t *testing.T) {
now := time.Date(2023, time.August, 11, 12, 0, 0, 0, time.UTC)
tests := []struct {
time time.Time
expected bool
}{
{time.Now().Add(-time.Hour * 2), true},
{time.Now().Add(2 * time.Hour), false},
{now.Add(-time.Hour), false}, // Same day, so not "passed"
{now.Add(time.Hour), false}, // Same day, so not "passed"
{now.Add(-25 * time.Hour), true}, // Previous day, so "passed"
{now.Add(25 * time.Hour), false}, // Next day, so not "passed"
}

for _, tt := range tests {
actual := HasDatePassed(tt.time)
actual := HasDatePassed(now, tt.time)
if actual != tt.expected {
t.Errorf("HasDatePassed(%v) = %v; want %v", tt.time, actual, tt.expected)
t.Errorf("HasDatePassed(%v, %v) = %v; want %v", now, tt.time, actual, tt.expected)
}
}
}

0 comments on commit 1bd0e50

Please sign in to comment.