-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_test.go
More file actions
127 lines (114 loc) · 3.37 KB
/
Copy pathcmd_test.go
File metadata and controls
127 lines (114 loc) · 3.37 KB
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
127
package wui
import (
"testing"
"time"
)
type msgA struct{}
type msgB struct{}
func TestBatchCollectsAllMsgs(t *testing.T) {
cmd := Batch(
func() Msg { return msgA{} },
func() Msg { return msgB{} },
)
batch, ok := cmd().(BatchMsg)
if !ok {
t.Fatalf("Batch produced %T, want BatchMsg", cmd())
}
if len(batch.Msgs) != 2 {
t.Fatalf("batch has %d msgs, want 2", len(batch.Msgs))
}
// Order matches the argument order, not completion order.
if _, isA := batch.Msgs[0].(msgA); !isA {
t.Errorf("first msg = %T, want msgA", batch.Msgs[0])
}
if _, isB := batch.Msgs[1].(msgB); !isB {
t.Errorf("second msg = %T, want msgB", batch.Msgs[1])
}
}
func TestBatchSkipsNilAndCollapses(t *testing.T) {
if Batch() != nil {
t.Error("Batch() = non-nil, want nil")
}
if Batch(nil, nil) != nil {
t.Error("Batch(nil, nil) = non-nil, want nil")
}
// A single surviving cmd is returned as-is, not wrapped.
cmd := Batch(nil, func() Msg { return msgA{} }, nil)
if _, isBatch := cmd().(BatchMsg); isBatch {
t.Error("Batch with one live cmd wrapped it in a BatchMsg")
}
}
func TestSequenceRunsInOrder(t *testing.T) {
var order []string
cmd := Sequence(
func() Msg { order = append(order, "first"); return msgA{} },
func() Msg { order = append(order, "second"); return msgB{} },
)
batch, ok := cmd().(BatchMsg)
if !ok {
t.Fatalf("Sequence produced %T, want BatchMsg", cmd())
}
if len(batch.Msgs) != 2 {
t.Fatalf("batch has %d msgs, want 2", len(batch.Msgs))
}
if len(order) != 2 || order[0] != "first" || order[1] != "second" {
t.Errorf("run order = %v, want [first second]", order)
}
}
func TestUnpackBatchFlattensNesting(t *testing.T) {
nested := BatchMsg{Msgs: []Msg{
msgA{},
BatchMsg{Msgs: []Msg{msgB{}, nil, BatchMsg{Msgs: []Msg{msgA{}}}}},
}}
got := unpackBatch(nested)
if len(got) != 3 {
t.Fatalf("unpackBatch returned %d msgs, want 3: %+v", len(got), got)
}
for _, m := range got {
if m == nil {
t.Error("unpackBatch kept a nil msg")
}
if _, isBatch := m.(BatchMsg); isBatch {
t.Error("unpackBatch left a nested BatchMsg")
}
}
}
func TestUnpackBatchOnPlainMsg(t *testing.T) {
got := unpackBatch(msgA{})
if len(got) != 1 {
t.Fatalf("unpackBatch(msgA{}) = %d msgs, want 1", len(got))
}
if unpackBatch(nil) != nil {
t.Error("unpackBatch(nil) = non-nil, want nil")
}
}
func TestQuitAndSend(t *testing.T) {
if _, isQuit := Quit()().(QuitMsg); !isQuit {
t.Error("Quit() did not produce a QuitMsg")
}
if _, isA := Send(msgA{})().(msgA); !isA {
t.Error("Send did not deliver its msg")
}
}
func TestTickDelaysThenDelivers(t *testing.T) {
start := time.Now()
msg := Tick(20*time.Millisecond, func() Msg { return msgA{} })()
if elapsed := time.Since(start); elapsed < 20*time.Millisecond {
t.Errorf("Tick returned after %v, want at least 20ms", elapsed)
}
if _, isA := msg.(msgA); !isA {
t.Errorf("Tick delivered %T, want msgA", msg)
}
}
// Every aligns to the wall clock, so it fires sooner than a full period
// when called mid-interval — that is what keeps a clock from drifting.
func TestEveryAlignsToWallClock(t *testing.T) {
start := time.Now()
msg := Every(50*time.Millisecond, func(time.Time) Msg { return msgA{} })()
if elapsed := time.Since(start); elapsed > 50*time.Millisecond {
t.Errorf("Every waited %v, want at most one 50ms period", elapsed)
}
if _, isA := msg.(msgA); !isA {
t.Errorf("Every delivered %T, want msgA", msg)
}
}