Skip to content

Commit

Permalink
timecop -> make Freeze and Unfreeze a const flag instead a function call
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adamluzsi committed Jul 22, 2024
1 parent c77c046 commit 609918c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions clock/Clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion clock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions clock/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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
}
Expand Down
24 changes: 14 additions & 10 deletions clock/timecop/timecop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions clock/timecop/timecop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand All @@ -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()))
})
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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
}
Expand Down

0 comments on commit 609918c

Please sign in to comment.