-
Notifications
You must be signed in to change notification settings - Fork 0
/
blurb_test.go
210 lines (205 loc) · 5.38 KB
/
blurb_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (C) 2015-2020 the Gprovision Authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
package cfa
import (
"fmt"
"gprovision/pkg/log/testlog"
"sync"
"testing"
"time"
)
//func (hs *horizScroller) updateState() (change bool)
func TestHorizScroll(t *testing.T) {
want := []string{
//pos,state,displayed,pos visualization
"00 01 |Random tes| ^",
"00 02 |Random tes| ^",
"00 03 |Random tes| ^",
"01 04 |andom test| ^",
"02 05 |ndom test | ^",
"03 06 |dom test t| ^",
"04 07 |om test te| ^",
"05 08 |m test tex| ^",
"06 09 | test text| ^",
"06 10 | test text| ^",
"06 00 | test text| ^",
"00 01 |Random tes| ^",
"00 02 |Random tes| ^",
"00 03 |Random tes| ^",
"01 04 |andom test| ^",
//...
}
hs := &horizScroller{txt: []byte("Random test text"), availChars: 10}
got := []string{}
for i := 0; i < 20; i++ {
hs.updateState()
txt := truncate(hs.txt[hs.pos:], hs.availChars)
got = append(got, fmt.Sprintf("%02d %02d |%s%*s| %*s", hs.pos, hs.scrollState, txt, int(hs.availChars)-len(txt), "", hs.pos+1, "^"))
}
for i := range want {
if got[i] != want[i] {
t.Errorf("line %d: got % 40s want %s", i, got[i], want[i])
}
}
if t.Failed() {
for _, l := range got {
t.Log(l)
}
}
}
func TestVertScroller(t *testing.T) {
testdata := []struct {
name string
model Model
lines []LcdTxt
start, dims Coord
}{
{
name: "631 short fits",
model: Cfa631,
lines: Strs2LTxt("631 short fits"),
dims: Coord{Row: 1, Col: 19},
},
{
name: "631 long fits",
model: Cfa631,
lines: Strs2LTxt("631 long fits long a", "second line fits too"),
dims: Coord{Row: 1, Col: 19},
},
{
name: "631 long overflow",
model: Cfa631,
lines: Strs2LTxt("631 long overflow", "second line fits", "...and overflow"),
dims: Coord{Row: 1, Col: 19},
},
{
name: "635 over",
model: Cfa635,
lines: Strs2LTxt("635 long overflow", "second line fits", "third", "fourth", "...and overflow"),
dims: Coord{Row: 3, Col: 19},
},
{
name: "635",
model: Cfa635,
lines: Strs2LTxt(),
dims: Coord{Row: 3, Col: 19},
},
}
for _, td := range testdata {
t.Run(td.name, func(t *testing.T) {
wg := new(sync.WaitGroup)
tlog := testlog.NewTestLog(t, true, false)
l, err := connectTo(Mock(td.model, wg, true))
if err != nil {
t.Error(err)
}
l.dev.MinPktInterval = time.Microsecond
vs := NewVertScroller(l, td.lines, td.start, td.dims)
for i := 0; i < 3; i++ {
change, err := vs.draw(false)
if err != nil {
t.Error(err)
}
if !change {
t.Errorf("no change")
}
}
l.Close()
wg.Wait()
tlog.MustMatchGoldenCleaned(testlog.FilterLogPfx("Write"), decode)
})
}
}
func TestBlurb(t *testing.T) {
testdata := []struct {
name string
model Model
line LcdTxt
start, dims Coord
shouldChange []bool
shouldErr []bool
}{
{
name: "631 short fits",
model: Cfa631,
line: LcdTxt("631 short fits"),
dims: Coord{Row: 1, Col: 19},
shouldChange: []bool{true, false, false},
},
{
name: "631 short small window",
model: Cfa631,
line: LcdTxt("631 small wndw"),
start: Coord{Row: 0, Col: 14},
dims: Coord{Row: 1, Col: 19},
shouldChange: []bool{false, false, false},
shouldErr: []bool{true, true, true},
},
{
name: "631 long fits",
model: Cfa631,
line: LcdTxt("631 long fits long a second line fits too"),
dims: Coord{Row: 1, Col: 19},
shouldChange: []bool{true, true, true},
},
{
name: "631 long overflow",
model: Cfa631,
line: LcdTxt("631 long overflow second line fits ...and overflow"),
dims: Coord{Row: 1, Col: 19},
shouldChange: []bool{true, true, true},
},
{
name: "635",
model: Cfa635,
dims: Coord{Row: 3, Col: 19},
shouldChange: []bool{true, false, false},
},
{
name: "635 over",
model: Cfa635,
line: LcdTxt("635 long overflow second line fits third fourth asfdsdaf dsf ...and overflow"),
dims: Coord{Row: 3, Col: 19},
shouldChange: []bool{true, true, true},
},
}
for _, td := range testdata {
t.Run(td.name, func(t *testing.T) {
wg := new(sync.WaitGroup)
tlog := testlog.NewTestLog(t, true, false)
l, err := connectTo(Mock(td.model, wg, true))
if err != nil {
t.Error(err)
}
l.dev.MinPktInterval = time.Microsecond
b := NewBlurb(l, td.line, td.start, td.dims)
for i, shouldchange := range td.shouldChange {
tlog.Logf("Write seq %d begins -------------", i) //separator in golden file
var shoulderr bool
if i < len(td.shouldErr) {
//if array is short, shoulderr is false
shoulderr = td.shouldErr[i]
}
didChange, err := b.draw(false)
if shoulderr && err == nil {
t.Error("should error")
} else if !shoulderr && err != nil {
t.Errorf("%s for\n%#v", err, b)
}
if shouldchange != didChange {
t.Errorf("change err - got %t want %t", didChange, shouldchange)
}
}
if t.Failed() {
t.Logf("log:\n%s", tlog.Buf.String())
}
l.Close()
wg.Wait()
tlog.MustMatchGoldenCleaned(testlog.FilterLogPfx("Write"), decode)
})
}
}