-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_osc_test.go
More file actions
363 lines (322 loc) · 7.56 KB
/
parser_osc_test.go
File metadata and controls
363 lines (322 loc) · 7.56 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package xterm
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
// testOscHandler records calls for testing.
type testOscHandler struct {
starts int
puts []string
ends []bool
endRet bool
}
func (h *testOscHandler) Start() { h.starts++ }
func (h *testOscHandler) Put(data []uint32, start, end int) {
h.puts = append(h.puts, utf32ToString(data, start, end))
}
func (h *testOscHandler) End(success bool) bool { h.ends = append(h.ends, success); return h.endRet }
func toUint32(s string) []uint32 {
r := make([]uint32, len(s))
for i, c := range s {
r[i] = uint32(c)
}
return r
}
func TestOscParserIDParsing(t *testing.T) {
t.Parallel()
type Expectation struct {
Starts int
Puts []string
Ends []bool
}
type TestCase struct {
Name string
Input string
Expected Expectation
}
tests := []TestCase{
{
Name: "parse id and payload",
Input: "52;SGVsbG8=",
Expected: Expectation{
Starts: 1,
Puts: []string{"SGVsbG8="},
Ends: []bool{true},
},
},
{
Name: "id only no payload",
Input: "0",
Expected: Expectation{
Starts: 1,
Puts: nil,
Ends: []bool{true},
},
},
{
Name: "empty semicolon payload",
Input: "7;",
Expected: Expectation{
Starts: 1,
Puts: nil,
Ends: []bool{true},
},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{endRet: true}
// Register for all test IDs
for _, id := range []int{0, 7, 52} {
p.RegisterHandler(id, h)
}
data := toUint32(tc.Input)
p.Start()
p.Put(data, 0, len(data))
p.End(true)
got := Expectation{Starts: h.starts, Puts: h.puts, Ends: h.ends}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestOscParserHandlerDispatch(t *testing.T) {
t.Parallel()
type Expectation struct {
H1Starts int
H2Starts int
H1Ends int
H2Ends int
}
p := NewOscParser()
h1 := &testOscHandler{}
h2 := &testOscHandler{}
p.RegisterHandler(10, h1)
p.RegisterHandler(10, h2)
data := toUint32("10;payload")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
got := Expectation{
H1Starts: h1.starts,
H2Starts: h2.starts,
H1Ends: len(h1.ends),
H2Ends: len(h2.ends),
}
expected := Expectation{
H1Starts: 1,
H2Starts: 1,
H1Ends: 1,
H2Ends: 1,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscParserFallback(t *testing.T) {
t.Parallel()
type FallbackCall struct {
Ident int
Action string
}
type Expectation struct {
Calls []FallbackCall
}
p := NewOscParser()
var calls []FallbackCall
p.SetHandlerFallback(func(ident int, action string, payload ...interface{}) {
calls = append(calls, FallbackCall{Ident: ident, Action: action})
})
data := toUint32("999;hello")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
got := Expectation{Calls: calls}
expected := Expectation{Calls: []FallbackCall{
{Ident: 999, Action: "START"},
{Ident: 999, Action: "PUT"},
{Ident: 999, Action: "END"},
}}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscParserDispose(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{}
d := p.RegisterHandler(10, h)
d.Dispose()
data := toUint32("10;test")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
if h.starts != 0 {
t.Errorf("handler should not have been called after dispose, got %d starts", h.starts)
}
}
func TestOscParserAbort(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{}
p.RegisterHandler(0, h)
// Non-digit in ID position causes abort
data := toUint32("x;payload")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
if h.starts != 0 {
t.Errorf("handler should not start on abort, got %d starts", h.starts)
}
}
func TestOscParserReset(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{}
p.RegisterHandler(1, h)
data := toUint32("1;partial")
p.Start()
p.Put(data, 0, len(data))
// Reset without End
p.Reset()
// h.End should have been called with false
type Expectation struct {
Ends []bool
}
got := Expectation{Ends: h.ends}
expected := Expectation{Ends: []bool{false}}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscStringHandler(t *testing.T) {
t.Parallel()
type Expectation struct {
Received string
Result bool
}
type TestCase struct {
Name string
Payload string
Success bool
Expected Expectation
}
tests := []TestCase{
{
Name: "successful end",
Payload: "hello world",
Success: true,
Expected: Expectation{Received: "hello world", Result: true},
},
{
Name: "unsuccessful end",
Payload: "hello",
Success: false,
Expected: Expectation{Received: "", Result: false},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
var received string
h := NewOscStringHandler(func(data string) bool {
received = data
return true
})
h.Start()
data := toUint32(tc.Payload)
h.Put(data, 0, len(data))
result := h.End(tc.Success)
got := Expectation{Received: received, Result: result}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestOscStringHandlerPayloadLimit(t *testing.T) {
t.Parallel()
var received string
h := NewOscStringHandler(func(data string) bool {
received = data
return true
})
h.Start()
// Exceed payload limit
big := strings.Repeat("A", ParserPayloadLimit+1)
data := toUint32(big)
h.Put(data, 0, len(data))
result := h.End(true)
if result != false {
t.Error("expected false when payload limit exceeded")
}
if received != "" {
t.Error("handler should not have been called when limit exceeded")
}
}
func TestOscParserMultiplePuts(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{endRet: true}
p.RegisterHandler(4, h)
// Send ID and payload in separate Put calls
id := toUint32("4;")
p.Start()
p.Put(id, 0, len(id))
payload1 := toUint32("hel")
p.Put(payload1, 0, len(payload1))
payload2 := toUint32("lo")
p.Put(payload2, 0, len(payload2))
p.End(true)
type Expectation struct {
Starts int
Puts []string
}
got := Expectation{Starts: h.starts, Puts: h.puts}
expected := Expectation{Starts: 1, Puts: []string{"hel", "lo"}}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscParserClearHandler(t *testing.T) {
t.Parallel()
p := NewOscParser()
h := &testOscHandler{}
p.RegisterHandler(5, h)
p.ClearHandler(5)
data := toUint32("5;test")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
if h.starts != 0 {
t.Errorf("handler should not be called after ClearHandler, got %d starts", h.starts)
}
}
func TestOscParserHandlerEndConsumption(t *testing.T) {
t.Parallel()
// When a later handler returns true from End, earlier handlers get End(false)
p := NewOscParser()
h1 := &testOscHandler{endRet: false}
h2 := &testOscHandler{endRet: true} // this one consumes
p.RegisterHandler(1, h1)
p.RegisterHandler(1, h2)
data := toUint32("1;data")
p.Start()
p.Put(data, 0, len(data))
p.End(true)
// h2 (last registered) is called first with success=true
// h1 gets End(false) because h2 consumed
type Expectation struct {
H1Ends []bool
H2Ends []bool
}
got := Expectation{H1Ends: h1.ends, H2Ends: h2.ends}
expected := Expectation{H1Ends: []bool{false}, H2Ends: []bool{true}}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}