forked from anaseto/gruid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_test.go
169 lines (156 loc) · 2.97 KB
/
ui_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gruid
import (
"bytes"
"context"
"testing"
)
type testModel struct {
gd Grid
count int
quit bool
draw bool
test int
}
type testMsg int
const niter = 90
func (tm *testModel) Update(msg Msg) Effect {
tm.draw = true
switch msg := msg.(type) {
case MsgInit:
cmd := Cmd(func() Msg {
return testMsg(1)
})
return Batch(cmd, cmd, Sub(func(ctx context.Context, msgs chan<- Msg) { msgs <- testMsg(1) }))
case testMsg:
tm.test++
if tm.test == 3 && tm.quit {
return End()
}
case MsgKeyDown:
if tm.quit {
return nil
}
switch msg.Key {
case KeyEnter:
tm.count++
case KeyEscape:
tm.draw = false
tm.quit = true
if tm.test == 3 {
return End()
}
}
}
return nil
}
func (tm *testModel) Draw() Grid {
if !tm.draw || tm.quit {
return tm.gd.Slice(Range{})
}
if tm.count%3 == 0 {
tm.gd.Fill(Cell{Rune: '0'})
} else {
tm.gd.Fill(Cell{Rune: '1'})
}
return tm.gd
}
type testDriver struct {
init bool
closed bool
t *testing.T
count int
}
func (td *testDriver) Init() error {
td.init = true
return nil
}
func (td *testDriver) PollMsgs(ctx context.Context, msgs chan<- Msg) error {
count := 0
for {
var msg Msg
msg = MsgKeyDown{Key: KeyEnter}
if count == niter {
msg = MsgScreen{}
}
if count == niter+1 {
msg = MsgKeyDown{Key: KeyEscape}
}
select {
case msgs <- msg:
case <-ctx.Done():
return nil
}
count++
}
}
func (td *testDriver) Flush(fr Frame) {
td.count++
if len(fr.Cells) != 8*4 {
td.t.Errorf("bad frame.Cells length: %d (expected %d)", len(fr.Cells), 8*4)
}
}
func (td *testDriver) Close() {
td.closed = true
}
func TestApp(t *testing.T) {
gd := NewGrid(8, 4)
m := &testModel{gd: gd}
td := &testDriver{t: t}
framebuf := &bytes.Buffer{} // for compressed recording
app := NewApp(AppConfig{
Driver: td,
Model: m,
FrameWriter: framebuf,
})
if err := app.Start(context.Background()); err != nil {
t.Errorf("Start returns error: %v", err)
}
if m.count != niter {
t.Errorf("bad count: %d", m.count)
}
if td.count != 1+1+2*niter/3 {
t.Errorf("bad driver count: %d", td.count)
}
m.gd.Iter(func(p Point, c Cell) {
if c.Rune != '0' {
t.Errorf("bad rune: %c", c.Rune)
}
})
if !td.closed || !td.init {
t.Errorf("not closed or not init")
}
dec, err := NewFrameDecoder(framebuf)
if err != nil {
t.Errorf("frame decoding %v", err)
}
count := 0
frame := Frame{}
err = dec.Decode(&frame)
for err == nil {
count++
err = dec.Decode(&frame)
}
if count != td.count {
t.Errorf("bad frame count: %d vs %d", count, td.count)
}
}
func TestApp2(t *testing.T) {
gd := NewGrid(8, 4)
m := &testModel{gd: gd}
m.count += 2
app := NewApp(AppConfig{
Driver: &testDriver{t: t},
Model: m,
})
if err := app.Start(context.Background()); err != nil {
t.Errorf("Start returns error: %v", err)
}
if m.count != niter+2 {
t.Errorf("bad count: %d", m.count)
}
m.gd.Iter(func(p Point, c Cell) {
if c.Rune != '1' {
t.Errorf("bad rune: %c", c.Rune)
}
})
}