-
Notifications
You must be signed in to change notification settings - Fork 1
/
step_stopwatch_test.go
106 lines (86 loc) · 2.12 KB
/
step_stopwatch_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package stopwatch
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/google/go-cmp/cmp"
)
type timeMock struct {
time time.Time
}
func (t *timeMock) now() time.Time {
return t.time
}
func TestStopwatchGetResults(t *testing.T) {
mock := &timeMock{}
testStopWatch := &stopwatch{
now: mock.now,
}
mock.time = time.Date(2022, 2, 2, 22, 22, 22, 0, time.UTC)
testStopWatch.Start()
mock.time = time.Date(2022, 2, 2, 22, 22, 23, 0, time.UTC)
testStopWatch.Step("a")
mock.time = time.Date(2022, 2, 2, 22, 22, 25, 0, time.UTC)
testStopWatch.Step("b")
mock.time = time.Date(2022, 2, 2, 22, 22, 28, 0, time.UTC)
testStopWatch.Step("c")
actual := testStopWatch.GetResults()
expect := &Results{
Steps: []Step{
{Label: "a", Duration: 1 * time.Second},
{Label: "b", Duration: 2 * time.Second},
{Label: "c", Duration: 3 * time.Second},
},
}
diff := cmp.Diff(expect, actual)
if diff != "" {
t.Fatal(diff)
}
}
func TestStopwatchGetResultMap(t *testing.T) {
mock := &timeMock{}
testStopWatch := &stopwatch{
now: mock.now,
}
mock.time = time.Date(2022, 2, 2, 22, 22, 22, 0, time.UTC)
testStopWatch.Start()
mock.time = time.Date(2022, 2, 2, 22, 22, 23, 0, time.UTC)
testStopWatch.Step("a")
mock.time = time.Date(2022, 2, 2, 22, 22, 25, 0, time.UTC)
testStopWatch.Step("b")
mock.time = time.Date(2022, 2, 2, 22, 22, 28, 0, time.UTC)
testStopWatch.Step("c")
actual := testStopWatch.GetResultMap()
expect := []map[string]int64{
{
"a": int64(1 * time.Second),
},
{
"b": int64(2 * time.Second),
},
{
"c": int64(3 * time.Second),
},
}
diff := cmp.Diff(expect, actual)
if diff != "" {
t.Fatal(diff)
}
}
func TestStopwatchCopy(t *testing.T) {
sw := NewStopWatch()
sw.Start()
sw.Step("one")
sw.Step("two")
swc := sw.Copy()
swc.Step("three")
swc.Step("four")
sw.Step("three")
sw.Step("four")
t1, t2 := sw.GetResults(), swc.GetResults()
require.Equal(t, len(t1.Steps), len(t2.Steps))
require.Equal(t, t1.Steps[0], t2.Steps[0])
require.Equal(t, t1.Steps[1], t2.Steps[1])
require.NotEqual(t, t1.Steps[2], t2.Steps[2])
require.NotEqual(t, t1.Steps[3], t2.Steps[3])
}