-
Notifications
You must be signed in to change notification settings - Fork 4
/
midi_lexer_test.go
690 lines (523 loc) · 20.1 KB
/
midi_lexer_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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// Copyright 2012 Joe Wass. All rights reserved.
// Use of this source code is governed by the MIT license
// which can be found in the LICENSE file.
// MIDI package
// A package for reading Standard Midi Files, written in Go.
// Joe Wass 2012
/*
* Tests for lexer.
* Check that the state transitions work fine and that the lexer can cope with real streams of MIDI data.
*/
package midi
import (
"io"
"testing"
)
var lexer *MidiLexer
var mockLexerCallback *CountingLexerCallback
var mockReadSeeker io.ReadSeeker
var finished bool
var err error
// Get clean values.
func setupData(data *[]byte) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(data)
}
/*
* Correct state transitions.
*/
// Start of file, consume header.
// ExpectHeader -> ExpectChunk
func TestLexerShouldExpectHeader(t *testing.T) {
// Just enough for the header chunk
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0xC8})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: New file, ExpectHeader state.
// Should be ready for header
assertIntsEqual(lexer.state, ExpectHeader, t)
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectChunk, t)
// Began() was called.
assertIntsEqual(mockLexerCallback.began, 1, t)
// Began() was called with the right values.
assertIntsEqual(int(mockLexerCallback.header), 1, t)
assertIntsEqual(int(mockLexerCallback.headerData.Format), 1, t)
assertIntsEqual(int(mockLexerCallback.headerData.NumTracks), 2, t)
if mockLexerCallback.headerData.TimeFormat != MetricalTimeFormat {
t.Fatal("Was not MetricalTimeFormat")
}
assertIntsEqual(int(mockLexerCallback.headerData.TicksPerQuarterNote), 200, t)
}
// Expect a chunk, get an unrecognised type. Should skip to next.
// ExpectChunk -> ExpectChunk
func TestMidiLexerShouldSkipUnknownTrack(t *testing.T) {
// Just enough for the header chunk
mockLexerCallback = new(CountingLexerCallback)
// Head of data stream is MThd, where the lexer will expect MTrk
mockReadSeeker = NewMockReadSeeker(&[]byte{ /* start of unknown block, claims to be 2-long */ 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x02, 0xCA, 0xFE /* Start of next block. */, 0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0xC8})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a chunk.
lexer.state = ExpectChunk
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectChunk, t)
// Reader should have jumped to position 10, the next block.
var position, err = lexer.input.Seek(0, 1)
assertNoError(err, t)
assertIntsEqual(int(position), 10, t)
}
// Expect a chunk, get an end of file. Should end gracefully.
// ExpectChunk -> Done
func TestMidiLexerShouldReachEndOfFile(t *testing.T) {
// Just enough for the header chunk
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a chunk.
lexer.state = ExpectChunk
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// finished
assertTrue(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, Done, t)
}
// Expect a chunk, get MTrk. Should enter ExpectTrackEvent state.
// ExpectChunk -> ExpectTrackEvent
func TestMidiLexerShouldExpectTrackEvent(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
// Head of data stream is MThd, where the lexer will expect MTrk
mockReadSeeker = NewMockReadSeeker(&[]byte{0x4D, 0x54, 0x72, 0x6B, 0x00, 0x00, 0x00, 0xEE, 0x00, 0x01, 0x00, 0x02, 0x00, 0xC8})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a chunk.
lexer.state = ExpectChunk
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.track, 1, t)
assertIntsEqual(int(mockLexerCallback.chunkHeader.Length), 0xEE, t)
}
// Expect a chunk, get MTrk.
// Should store reported track length and go back to ExpectChunk at end of chunk.
// ExpectChunk -> ExpectTrackEvent
func TestMidiLexerShouldHandleChunkLengths(t *testing.T) {
// TODO
}
// Expect a chunk, get MTrk with a too-short length.
// Should raise a BadSizeChunk error
// ExpectChunk -> ExpectTrackEvent
func TestMidiLexerShouldHandleChunkLengthError(t *testing.T) {
// TODO
}
// Expect a track event, parse a NoteOff message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestNoteOff(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x40, 0x85, 0x04, 0x03})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.noteOff, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x40, t)
assertUint8sEqual(mockLexerCallback.channel, 0x05, t)
assertUint8sEqual(mockLexerCallback.pitch, 0x04, t)
assertUint8sEqual(mockLexerCallback.velocity, 0x03, t)
}
// Expect a track event, parse a NoteOn message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestNoteOn(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x40, 0x95, 0x04, 0x03})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.noteOn, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x40, t)
assertUint8sEqual(mockLexerCallback.channel, 0x05, t)
assertUint8sEqual(mockLexerCallback.pitch, 0x04, t)
assertUint8sEqual(mockLexerCallback.velocity, 0x03, t)
}
// Expect a track event, parse a NoteOn message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestNotePolyphonicKeyPressure(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x40, 0xA7, 0x12, 0x34})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.polyphonicAfterTouch, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x40, t)
assertUint8sEqual(mockLexerCallback.channel, 0x07, t)
assertUint8sEqual(mockLexerCallback.pitch, 0x12, t)
assertUint8sEqual(mockLexerCallback.pressure, 0x34, t)
}
// Expect a track event, parse a message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestProgramChange(t *testing.T) {
// TODO
}
// Expect a track event, parse a message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestChannelPressure(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x40, 0xD8, 0x56})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.channelAfterTouch, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x40, t)
assertUint8sEqual(mockLexerCallback.channel, 0x08, t)
assertUint8sEqual(mockLexerCallback.pressure, 0x56, t)
}
// Expect a track event, parse a PitchWheel message.
// ExpectTrackEvent -> ExpectTrackEvent
func TestPitchWheel(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
// Three sequential pitch wheel events. NB the value is 14-bit,
// split over two bytes, little end first!
mockReadSeeker = NewMockReadSeeker(&[]byte{
0x10, 0xE9, 0x00, 0x40, // 0x2000 should be centre
0x20, 0xE8, 0x34, 0x24, // 0x1234 encoded
0x50, 0xE7, 0x00, 0x40})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
/*
* FIRST
*/
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.pitchWheel, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x10, t)
assertUint8sEqual(mockLexerCallback.channel, 0x09, t)
assertInt16sEqual(mockLexerCallback.pitchWheelValue, 0x00, t)
assertUint16Equal(mockLexerCallback.pitchWheelValueAbsolute, 0x2000, t)
/*
* SECOND
*/
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.pitchWheel, 2, t)
assertUint32Equal(mockLexerCallback.time, 0x20, t)
assertUint8sEqual(mockLexerCallback.channel, 0x08, t)
assertInt16sEqual(mockLexerCallback.pitchWheelValue, -0xDCC, t)
assertUint16Equal(mockLexerCallback.pitchWheelValueAbsolute, 0x1234, t)
/*
* THIRD
*/
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.pitchWheel, 3, t)
assertUint32Equal(mockLexerCallback.time, 0x50, t)
assertUint8sEqual(mockLexerCallback.channel, 0x07, t)
assertInt16sEqual(mockLexerCallback.pitchWheelValue, 0x00, t)
assertUint16Equal(mockLexerCallback.pitchWheelValueAbsolute, 0x2000, t)
}
// Expect a track event, get SequenceNumber with no number.
// ExpectTrackEvent -> ExpectTrackEvent
func TestNilSequenceNumber(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
// Three sequential pitch wheel events. NB the value is 14-bit,
// split over two bytes, little end first!
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x00, 0x00})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.sequenceNumber, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertFalse(mockLexerCallback.sequenceNumberGiven, t)
}
// Expect a track event, get SequenceNumber with a number supplied.
// ExpectTrackEvent -> ExpectTrackEvent
func TestSequenceNumber(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
// Three sequential pitch wheel events. NB the value is 14-bit,
// split over two bytes, little end first!
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x00, 0x02, 0xA7, 0xC5})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Track should have been called.
assertIntsEqual(mockLexerCallback.sequenceNumber, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertUint16Equal(mockLexerCallback.sequenceNumberValue, 42949, t)
assertTrue(mockLexerCallback.sequenceNumberGiven, t)
}
// Expect a track event, get Text event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestText(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x01, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.text, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get CopyrightText event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestCopyrightText(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x02, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.copyrightText, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get SequenceName event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestSequenceName(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x03, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.sequenceName, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get TrackInstrumentName event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestTrackInstrumentName(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x04, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.trackInstrumentName, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get LyricText event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestLyricText(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x05, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.lyricText, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get MarkerText event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestMarkerText(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x06, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.markerText, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get CuePointText event.
// ExpectTrackEvent -> ExpectTrackEvent
func TestCuePointText(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x07, 0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
assertIntsEqual(lexer.state, ExpectTrackEvent, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.cuePointText, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
assertStringsEqual(mockLexerCallback.textValue, "[email protected]", t)
}
// Expect a track event, get EndOfTrack event.
// ExpectTrackEvent -> ExpectChunk
func TestEndOfTrack(t *testing.T) {
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0x09, 0xFF, 0x2F, 0x00})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
// Pre: ExpectChunk
// Should be ready for a track event.
lexer.state = ExpectTrackEvent
finished, err = lexer.next()
assertNoError(err, t)
// Post:
// not finished yet
assertFalse(finished, t)
// ExpectChunk state.
// As this ends the track, we go back to ExpectChunk state ready for the next track.
assertIntsEqual(lexer.state, ExpectChunk, t)
// callback.Text should have been callbacked.
assertIntsEqual(mockLexerCallback.endOfTrack, 1, t)
assertUint32Equal(mockLexerCallback.time, 0x09, t)
}
/*
* Exceptional state transitions.
*/
// Bad header chunk type at start of file should result in error
func TestLexerShouldErrorBadHeader(t *testing.T) {
// Just enough for the header chunk
mockLexerCallback = new(CountingLexerCallback)
mockReadSeeker = NewMockReadSeeker(&[]byte{0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0xC8})
lexer = NewMidiLexer(mockReadSeeker, mockLexerCallback)
finished, err = lexer.next()
assertError(err, ExpectedMthd, t)
}