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
|
package pausedtimer
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
d := 1 * time.Second
timer := New(d)
if timer.duration != d {
t.Errorf("expected duration %#v, got %v", d, timer.duration)
}
if timer.Ticker == nil {
t.Error("expected Ticker to be non-nil")
}
}
func TestPauseTimerZeroDuration(t *testing.T) {
timer := New(0)
if timer.GetDuration() != 0 {
t.Errorf("expected duration %v, got %v", time.Duration(0), timer.GetDuration())
}
if timer.Ticker == nil {
t.Error("expected Ticker to be non-nil")
}
}
func TestPauseTimerResetToZero(t *testing.T) {
timer := New(1 * time.Second)
timer.Reset(0)
if timer.GetDuration() != 0 {
t.Errorf("expected duration %v, got %v", time.Duration(0), timer.GetDuration())
}
}
func TestPauseTimerPauseAndResume(t *testing.T) {
d := 1 * time.Second
timer := New(d)
timer.Stop() // Simulate pause
time.Sleep(500 * time.Millisecond)
timer.Resume()
select {
case <-timer.C:
// Timer should not have fired yet
t.Fatal("Timer fired too early")
case <-time.After(600 * time.Millisecond):
// Timer should fire after resuming
}
}
func TestPauseTimerReset(t *testing.T) {
d := 1 * time.Second
timer := New(d)
got := 2 * time.Second
timer.Reset(got)
if timer.duration != got {
t.Errorf("expected duration %v, got %v", got, timer.duration)
}
}
func TestPauseTimerResume(t *testing.T) {
d := 1 * time.Second
timer := NewStopped(d)
timer.Resume()
if timer.duration != d {
t.Errorf("expected duration %v, got %v", d, timer.duration)
}
}
func TestPauseTimerGetDuration(t *testing.T) {
d := 1 * time.Second
timer := New(d)
if timer.GetDuration() != d {
t.Errorf("expected duration %v, got %v", d, timer.GetDuration())
}
}
|