-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_test.go
74 lines (47 loc) · 1.23 KB
/
clock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package clocksteps_test
import (
"testing"
"time"
"github.com/godogx/clocksteps"
"github.com/stretchr/testify/assert"
)
func TestClock(t *testing.T) {
t.Parallel()
c := clocksteps.New()
now := time.Now()
assert.True(t, now.Before(c.Now()))
// Errors while adding time to a live clock.
assert.Equal(t, clocksteps.ErrClockIsNotSet, c.Add(time.Hour))
assert.Equal(t, clocksteps.ErrClockIsNotSet, c.AddDate(0, 0, 1))
// Freeze the clock.
c.Freeze()
ts := c.Now()
<-time.After(50 * time.Millisecond)
assert.Equal(t, ts, c.Now())
// Set to another time.
ts = time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
c.Set(ts)
<-time.After(50 * time.Millisecond)
assert.Equal(t, ts, c.Now())
// Change the time.
ts = ts.Add(2 * time.Hour)
err := c.Add(2 * time.Hour)
assert.NoError(t, err)
<-time.After(50 * time.Millisecond)
assert.Equal(t, ts, c.Now())
// Change the date.
ts = ts.AddDate(2, 1, 3)
err = c.AddDate(2, 1, 3)
assert.NoError(t, err)
<-time.After(50 * time.Millisecond)
assert.Equal(t, ts, c.Now())
// Unfreeze the clock.
c.Unfreeze()
now = time.Now()
assert.True(t, now.Before(c.Now()))
}
func TestClock_Clock(t *testing.T) {
t.Parallel()
c := clocksteps.New()
assert.Equal(t, c, c.Clock())
}