-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer_test.go
126 lines (117 loc) · 3.4 KB
/
timer_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package utils
import (
"fmt"
"testing"
"time"
)
func TestNewTimer(t *testing.T) {
timer := NewSplitTime("main")
if timer.Name() != "main" {
t.Errorf("Invalid timer name %s instead of %s", timer.Name(), "main")
}
}
func TestTimerStatus(t *testing.T) {
timer := NewSplitTime("timer")
if timer.Status() != Uninitalized {
t.Errorf("Invalid timer status %s instead of %s", timer.Status(), Uninitalized)
}
timer.Start()
if timer.Status() != Running {
t.Errorf("Invalid timer status %s instead of %s", timer.Status(), Running)
}
timer.Stop()
if timer.Status() != Stopped {
t.Errorf("Invalid timer status %s instead of %s", timer.Status(), Stopped)
}
}
func TestTimerDates(t *testing.T) {
timer := NewSplitTime("timer")
if timer.StartDate() != nil {
t.Errorf("Start date on uninitalized timer : %s", timer.StartDate().String())
}
if timer.StopDate() != nil {
t.Errorf("Stop date on uninitalized timer : %s", timer.StopDate().String())
}
timer.Start()
if timer.StartDate() == nil {
t.Error("Missing start date on running timer")
}
if timer.StopDate() != nil {
t.Errorf("Stop date on running timer : %s", timer.StopDate().String())
}
timer.Stop()
if timer.StartDate() == nil {
t.Error("Missing start date on stopped timer")
}
if timer.StopDate() == nil {
t.Error("Missing stop date on stopped timer")
}
}
func TestTimerImmutability(t *testing.T) {
timer := NewSplitTime("timer")
timer.Start()
startDate1 := timer.StartDate()
timer.Start()
startDate2 := timer.StartDate()
if startDate1 != startDate2 {
t.Errorf("Non immutable start date : %s != %s", startDate1.String(), startDate2.String())
}
timer.Stop()
stopDate1 := timer.StopDate()
timer.Stop()
stopDate2 := timer.StopDate()
if stopDate1 != stopDate2 {
t.Errorf("Non immutable stop date : %s != %s", stopDate1.String(), stopDate2.String())
}
timer.Start()
if timer.Status() != Stopped {
t.Errorf("Non immutable timer status %s instead of %s", timer.Status(), Stopped)
}
startDate3 := timer.StartDate()
if startDate1 != startDate3 {
t.Errorf("Non immutable start date : %s != %s", startDate1.String(), startDate3.String())
}
}
func TestTimerElapsed(t *testing.T) {
timer := NewSplitTime("timer")
if timer.Elapsed() != time.Duration(0) {
t.Errorf("Invalid uninitialized timer elapsed time %s", timer.Elapsed().String())
}
timer.Start()
if timer.Elapsed() <= time.Duration(0) {
t.Errorf("Invalid running timer elapsed time %s", timer.Elapsed().String())
}
timer.Stop()
if timer.Elapsed() <= time.Duration(0) {
t.Errorf("Invalid stopped timer elapsed time %s", timer.Elapsed().String())
}
}
func TestTimerSplit(t *testing.T) {
timer := NewSplitTime("timer")
timer.Start()
sleep := 100 * time.Millisecond
time.Sleep(sleep)
elapsed := TruncateDuration(timer.Split(), 10*time.Millisecond)
if elapsed != sleep {
t.Fatalf("Invalid split duration %v. Expected %v", elapsed, sleep)
}
timer.Stop()
}
func TestTimerStopUninitalizedTimer(t *testing.T) {
timer := NewSplitTime("timer")
timer.Stop()
if timer.Status() != Stopped {
t.Errorf("Invalid timer status %s instead of %s", timer.Status(), Stopped)
}
if timer.Elapsed() != time.Duration(0) {
t.Errorf("Invalid uninitialized stopped timer elapsed time %s", timer.Elapsed().String())
}
}
func TestTimerString(t *testing.T) {
timer := NewSplitTime("timer")
fmt.Println(timer.String())
timer.Start()
fmt.Println(timer.String())
timer.Stop()
fmt.Println(timer.String())
}