Skip to content

Commit

Permalink
refactor: move ForceTimeLocal to pesticide
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Dec 13, 2024
1 parent b961ba1 commit a179cb0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
15 changes: 15 additions & 0 deletions pesticide/time_test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pesticide

import (
"testing"
"time"
)

// ForceTimeLocal temporarily sets [time.Local] for test purpose.
func ForceTimeLocal(t *testing.T, local *time.Location) {
t.Helper()

originalLocal := time.Local
time.Local = local
t.Cleanup(func() { time.Local = originalLocal })
}
9 changes: 4 additions & 5 deletions registry/conversion/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-sprout/sprout/pesticide"
"github.com/go-sprout/sprout/registry/conversion"
rtime "github.com/go-sprout/sprout/registry/time"
)

func TestToBool(t *testing.T) {
Expand Down Expand Up @@ -137,7 +136,7 @@ func TestToDate(t *testing.T) {
require.NoError(t, err)

// temporarily force time.Local to New York
rtime.ForceTimeLocal(t, local)
pesticide.ForceTimeLocal(t, local)

tc := []pesticide.TestCase{
{
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestToDate(t *testing.T) {
require.NoError(t, err)

// temporarily force time.Local to New York
rtime.ForceTimeLocal(t, local)
pesticide.ForceTimeLocal(t, local)

tc := []pesticide.TestCase{
{
Expand Down Expand Up @@ -206,7 +205,7 @@ func TestToDate(t *testing.T) {
t.Run("dates without timezone (local time should be assumed)", func(t *testing.T) {
t.Run("UTC", func(t *testing.T) {
// temporarily force time.Local to UTC
rtime.ForceTimeLocal(t, time.UTC)
pesticide.ForceTimeLocal(t, time.UTC)

tc := []pesticide.TestCase{
{
Expand All @@ -231,7 +230,7 @@ func TestToDate(t *testing.T) {
require.NoError(t, err)

// temporarily force time.Local to New York
rtime.ForceTimeLocal(t, local)
pesticide.ForceTimeLocal(t, local)

tc := []pesticide.TestCase{
{
Expand Down
4 changes: 2 additions & 2 deletions registry/time/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestDate(t *testing.T) {
t.Run("unixtime", func(t *testing.T) {
t.Run("UTC", func(t *testing.T) {
// temporarily force time.Local to UTC
rtime.ForceTimeLocal(t, time.UTC)
pesticide.ForceTimeLocal(t, time.UTC)

// here we are simulating a [gotime.Now]
timeTest := time.Date(2024, 5, 7, 15, 4, 5, 0, time.UTC)
Expand All @@ -82,7 +82,7 @@ func TestDate(t *testing.T) {
require.NoError(t, err)

// temporarily force time.Local to New York
rtime.ForceTimeLocal(t, local)
pesticide.ForceTimeLocal(t, local)

// here we are simulating a [gotime.Now] call
timeTest := time.Date(2024, 5, 7, 15, 4, 5, 0, local)
Expand Down
10 changes: 0 additions & 10 deletions registry/time/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package time

import (
"testing"
"time"
)

Expand All @@ -23,12 +22,3 @@ func computeTimeFromFormat(date any) time.Time {
// otherwise, fallback to the current time
return time.Now().Local()
}

// ForceTimeLocal temporarily sets [time.Local] for test purpose.
func ForceTimeLocal(t *testing.T, local *time.Location) {
t.Helper()

originalLocal := time.Local
time.Local = local
t.Cleanup(func() { time.Local = originalLocal })
}

0 comments on commit a179cb0

Please sign in to comment.