From 609918cccf5ffa209d8c2b54865c7fd3f01e6ddb Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Mon, 22 Jul 2024 21:38:27 +0200 Subject: [PATCH] timecop -> make Freeze and Unfreeze a const flag instead a function call As a function, it could be misleading that it does an action, but as a const, it express clearly that on its own, it does nothing. --- clock/Clock_test.go | 4 ++-- clock/README.md | 2 +- clock/examples_test.go | 4 ++-- clock/timecop/timecop.go | 24 ++++++++++++++---------- clock/timecop/timecop_test.go | 10 +++++----- examples_test.go | 4 ++-- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/clock/Clock_test.go b/clock/Clock_test.go index 9d57bb8..7182b75 100644 --- a/clock/Clock_test.go +++ b/clock/Clock_test.go @@ -59,7 +59,7 @@ func TestNow(t *testing.T) { expTime := let.Time(s) s.Before(func(t *testcase.T) { - timecop.Travel(t, expTime.Get(t).UTC(), timecop.Freeze()) + timecop.Travel(t, expTime.Get(t).UTC(), timecop.Freeze) }) s.Then("the time it just returned in the same Local as time.Now()", func(t *testcase.T) { @@ -230,7 +230,7 @@ func TestAfter(t *testing.T) { s.Test("no matter what, when the wait time is zero, clock.After returns instantly", func(t *testcase.T) { timecop.SetSpeed(t, 0.001) - timecop.Travel(t, time.Second, timecop.Freeze()) + timecop.Travel(t, time.Second, timecop.Freeze) assert.Within(t, time.Millisecond, func(ctx context.Context) { <-clock.After(0) }, "expected to finish instantly") diff --git a/clock/README.md b/clock/README.md index ef1ab84..4cf8a23 100644 --- a/clock/README.md +++ b/clock/README.md @@ -63,7 +63,7 @@ func Test(t *testing.T) { CreatedAt: clock.TimeNow(), } - timecop.Travel(t, expected.CreatedAt, timecop.Freeze()) + timecop.Travel(t, expected.CreatedAt, timecop.Freeze) assert.Equal(t, expected, MyFunc()) } diff --git a/clock/examples_test.go b/clock/examples_test.go index 71159bf..caa1635 100644 --- a/clock/examples_test.go +++ b/clock/examples_test.go @@ -26,7 +26,7 @@ func ExampleTimeNow_freeze() { CreatedAt: clock.Now(), } - timecop.Travel(tb, expected.CreatedAt, timecop.Freeze()) + timecop.Travel(tb, expected.CreatedAt, timecop.Freeze) assert.Equal(tb, expected, MyFunc()) } @@ -43,7 +43,7 @@ func ExampleTimeNow_withTravelByDate() { var tb testing.TB date := time.Date(2022, 01, 01, 12, 0, 0, 0, time.Local) - timecop.Travel(tb, date, timecop.Freeze()) // freeze the time until it is read + timecop.Travel(tb, date, timecop.Freeze) // freeze the time until it is read time.Sleep(time.Second) _ = clock.Now() // equals with date } diff --git a/clock/timecop/timecop.go b/clock/timecop/timecop.go index 70e943f..de7599d 100644 --- a/clock/timecop/timecop.go +++ b/clock/timecop/timecop.go @@ -48,16 +48,20 @@ func travelByTime(tb testing.TB, target time.Time, opt internal.Option) { tb.Cleanup(internal.SetTime(target, opt)) } -// Freeze instruct travel to freeze the time. -func Freeze() TravelOption { - return fnTravelOption(func(o *internal.Option) { - o.Freeze = true - }) +// Freeze is a Travel TravelOption, and it instruct travel to freeze the time wherever it lands after the travelling.. +const Freeze = freeze(0) + +type freeze int + +func (freeze) configure(o *internal.Option) { + o.Freeze = true } -// Unfreeze instruct travel to unfreeze the time. -func Unfreeze() TravelOption { - return fnTravelOption(func(o *internal.Option) { - o.Unfreeze = true - }) +// Unfreeze is a Travel TravelOption, and it instruct travel that after the time travelling, the flow of time should continue. +const Unfreeze = unfreeze(0) + +type unfreeze int + +func (unfreeze) configure(o *internal.Option) { + o.Unfreeze = true } diff --git a/clock/timecop/timecop_test.go b/clock/timecop/timecop_test.go index d54d859..eaf1ab3 100644 --- a/clock/timecop/timecop_test.go +++ b/clock/timecop/timecop_test.go @@ -52,7 +52,7 @@ func TestSetSpeed(t *testing.T) { }) t.Run("on frozen time SetSpeed don't start the time", func(t *testing.T) { now := time.Now() - timecop.Travel(t, now, timecop.Freeze()) + timecop.Travel(t, now, timecop.Freeze) timecop.SetSpeed(t, rnd.Float64()) time.Sleep(time.Microsecond) got := clock.Now() @@ -127,7 +127,7 @@ func TestTravel_timeTime(t *testing.T) { nano = rnd.IntB(1, int(time.Microsecond-1)) ) date := time.Date(year, month, day, hour, minute, second, nano, time.Local) - timecop.Travel(t, date, timecop.Freeze()) + timecop.Travel(t, date, timecop.Freeze) time.Sleep(time.Millisecond) got := clock.Now() assert.True(t, date.Equal(got)) @@ -146,13 +146,13 @@ func TestTravel_timeTime(t *testing.T) { nano = rnd.IntB(1, int(time.Microsecond-1)) ) date := time.Date(year, month, day, hour, minute, second, nano, time.Local) - timecop.Travel(t, date, timecop.Freeze()) + timecop.Travel(t, date, timecop.Freeze) time.Sleep(time.Millisecond) got := clock.Now() assert.True(t, date.Equal(got)) assert.Waiter{WaitDuration: time.Second}.Wait() assert.True(t, date.Equal(clock.Now())) - timecop.Travel(t, clock.Now(), timecop.Unfreeze()) + timecop.Travel(t, clock.Now(), timecop.Unfreeze) assert.MakeRetry(time.Second).Assert(t, func(it assert.It) { it.Must.False(date.Equal(clock.Now())) }) @@ -162,7 +162,7 @@ func TestTravel_timeTime(t *testing.T) { func TestTravel_cleanup(t *testing.T) { date := time.Now().AddDate(-10, 0, 0) t.Run("", func(t *testing.T) { - timecop.Travel(t, date, timecop.Freeze()) + timecop.Travel(t, date, timecop.Freeze) assert.Equal(t, date.Year(), clock.Now().Year()) }) const msg = "was not expected that timecop travel leak out from the sub test" diff --git a/examples_test.go b/examples_test.go index 4df1551..ce7d864 100644 --- a/examples_test.go +++ b/examples_test.go @@ -1450,7 +1450,7 @@ func Example_clockTimeNow_withTimecop() { CreatedAt: clock.Now(), } - timecop.Travel(tb, expected.CreatedAt, timecop.Freeze()) + timecop.Travel(tb, expected.CreatedAt, timecop.Freeze) assert.Equal(tb, expected, MyFunc()) } @@ -1467,7 +1467,7 @@ func Example_clockTimeNow_withTravelByDate() { var tb testing.TB date := time.Date(2022, 01, 01, 12, 0, 0, 0, time.Local) - timecop.Travel(tb, date, timecop.Freeze()) // freeze the time until it is read + timecop.Travel(tb, date, timecop.Freeze) // freeze the time until it is read time.Sleep(time.Second) _ = clock.Now() // equals with date }