-
Notifications
You must be signed in to change notification settings - Fork 4
/
mocks.go
339 lines (306 loc) · 13 KB
/
mocks.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
// 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
// joe@afandian.com
/*
* Mocks implementations for testing.
* In order to check that the right calls are made to the LexerCallback by the Lexer, a mock callback is pssed to it during tests.
*/
package midi
import (
"io"
)
// A mock implementation of LexerCallback that does nothing.
type MockLexerCallback struct{}
func (*MockLexerCallback) Header(header HeaderData) {}
func (*MockLexerCallback) Track(header ChunkHeader) {}
func (*MockLexerCallback) Began() {}
func (*MockLexerCallback) Finished() {}
func (*MockLexerCallback) ErrorReading() {}
func (*MockLexerCallback) ErrorOpeningFile() {}
func (*MockLexerCallback) NoteOff(channel uint8, pitch uint8, velocity uint8, time uint32) {}
func (*MockLexerCallback) NoteOn(channel uint8, pitch uint8, velocity uint8, time uint32) {}
func (*MockLexerCallback) PolyphonicAfterTouch(channel uint8, pitch uint8, pressure uint8, time uint32) {
}
func (*MockLexerCallback) ControlChange(channel uint8, controller uint8, value uint8, time uint32) {}
func (*MockLexerCallback) ProgramChange(channel uint8, program uint8, time uint32) {}
func (*MockLexerCallback) ChannelAfterTouch(channel uint8, value uint8, time uint32) {}
func (*MockLexerCallback) PitchWheel(channel uint8, value int16, absValue uint16, time uint32) {}
func (*MockLexerCallback) TimeCodeQuarter(messageType uint8, values uint8, time uint32) {}
func (*MockLexerCallback) Tempo(bpm uint32, microsecondsPerCrotchet uint32, time uint32) {}
func (*MockLexerCallback) SongPositionPointer(beats uint16, time uint32) {}
func (*MockLexerCallback) SongSelect(song uint8, time uint32) {}
func (*MockLexerCallback) Undefined1(time uint32) {}
func (*MockLexerCallback) Undefined2(time uint32) {}
func (*MockLexerCallback) TuneRequest(time uint32) {}
func (*MockLexerCallback) TimingClock(time uint32) {}
func (*MockLexerCallback) Undefined3(time uint32) {}
func (*MockLexerCallback) Start(time uint32) {}
func (*MockLexerCallback) Continue(time uint32) {}
func (*MockLexerCallback) Stop(time uint32) {}
func (*MockLexerCallback) Undefined4(time uint32) {}
func (*MockLexerCallback) ActiveSensing(time uint32) {}
func (*MockLexerCallback) Reset(time uint32) {}
func (*MockLexerCallback) Done(time uint32) {}
func (*MockLexerCallback) SequenceNumber(channel uint8, number uint16, numberGiven bool, time uint32) {
}
func (*MockLexerCallback) Text(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) CopyrightText(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) SequenceName(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) TrackInstrumentName(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) LyricText(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) MarkerText(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) CuePointText(channel uint8, text string, time uint32) {}
func (*MockLexerCallback) EndOfTrack(channel uint8, time uint32) {}
func (*MockLexerCallback) TimeSignature(numerator uint8, denomenator uint8, clocksPerClick uint8, demiSemiQuaverPerQuarter uint8, time uint32) {
}
func (*MockLexerCallback) KeySignature(key ScaleDegree, mode KeySignatureMode, sharpsOrFlats int8) {
}
// A mock implementation of LexerCallback that counts each method call and stores the most recent values,
// so that calls can be verified.
type CountingLexerCallback struct {
// Callback counts
header int
track int
began int
finished int
errorReading int
errorOpeningFile int
noteOff int
noteOn int
polyphonicAfterTouch int
controlChange int
programChange int
channelAfterTouch int
pitchWheel int
timeCodeQuarter int
songPositionPointer int
songSelect int
undefined1 int
undefined2 int
tuneRequest int
timingClock int
undefined3 int
start int
continue_ int
stop int
undefined4 int
activeSensing int
reset int
done int
endOfTrack int
text int
copyrightText int
sequenceName int
trackInstrumentName int
lyricText int
markerText int
cuePointText int
sequenceNumber int
tempo int
// Most recent values
headerData HeaderData
chunkHeader ChunkHeader
pitch uint8
channel uint8
time uint32
velocity uint8
pressure uint8
textValue string
pitchWheelValue int16
pitchWheelValueAbsolute uint16
sequenceNumberGiven bool
sequenceNumberValue uint16
// Time sig args
numerator uint8
denomenator uint8
clocksPerClick uint8
demiSemiQuaverPerQuarter uint8
// Tempo
microsecondsPerCrotchet uint32
bpm uint32
}
func (cbk *CountingLexerCallback) Header(header HeaderData) { cbk.header++; cbk.headerData = header }
func (cbk *CountingLexerCallback) Track(header ChunkHeader) { cbk.track++; cbk.chunkHeader = header }
func (cbk *CountingLexerCallback) Began() { cbk.began++ }
func (cbk *CountingLexerCallback) Finished() { cbk.finished++ }
func (cbk *CountingLexerCallback) ErrorReading() { cbk.errorReading++ }
func (cbk *CountingLexerCallback) ErrorOpeningFile() { cbk.errorOpeningFile++ }
func (cbk *CountingLexerCallback) NoteOff(channel uint8, pitch uint8, velocity uint8, time uint32) {
cbk.noteOff++
cbk.pitch = pitch
cbk.channel = channel
cbk.velocity = velocity
cbk.time = time
}
func (cbk *CountingLexerCallback) NoteOn(channel uint8, pitch uint8, velocity uint8, time uint32) {
cbk.noteOn++
cbk.pitch = pitch
cbk.channel = channel
cbk.velocity = velocity
cbk.time = time
}
func (cbk *CountingLexerCallback) PolyphonicAfterTouch(channel uint8, pitch uint8, pressure uint8, time uint32) {
cbk.polyphonicAfterTouch++
cbk.channel = channel
cbk.pitch = pitch
cbk.pressure = pressure
cbk.time = time
}
func (cbk *CountingLexerCallback) Tempo(bpm uint32, microsecondsPerCrotchet uint32, time uint32) {
// TODO USE
cbk.tempo++
cbk.microsecondsPerCrotchet = microsecondsPerCrotchet
cbk.bpm = bpm
cbk.time = time
}
func (cbk *CountingLexerCallback) ControlChange(channel uint8, controller uint8, value uint8, time uint32) {
cbk.controlChange++
}
func (cbk *CountingLexerCallback) ProgramChange(channel uint8, program uint8, time uint32) {
cbk.programChange++
}
func (cbk *CountingLexerCallback) ChannelAfterTouch(channel uint8, pressure uint8, time uint32) {
cbk.channelAfterTouch++
cbk.channel = channel
cbk.pressure = pressure
cbk.time = time
}
func (cbk *CountingLexerCallback) PitchWheel(channel uint8, value int16, absValue uint16, time uint32) {
cbk.pitchWheel++
cbk.pitchWheelValue = value
cbk.pitchWheelValueAbsolute = absValue
cbk.time = time
cbk.channel = channel
}
func (cbk *CountingLexerCallback) TimeCodeQuarter(messageType uint8, values uint8, time uint32) {
cbk.timeCodeQuarter++
}
func (cbk *CountingLexerCallback) SongPositionPointer(beats uint16, time uint32) {
cbk.songPositionPointer++
}
func (cbk *CountingLexerCallback) SongSelect(song uint8, time uint32) { cbk.songSelect++ }
func (cbk *CountingLexerCallback) Undefined1(time uint32) { cbk.undefined1++ }
func (cbk *CountingLexerCallback) Undefined2(time uint32) { cbk.undefined2++ }
func (cbk *CountingLexerCallback) TuneRequest(time uint32) { cbk.tuneRequest++ }
func (cbk *CountingLexerCallback) TimingClock(time uint32) { cbk.timingClock++ }
func (cbk *CountingLexerCallback) Undefined3(time uint32) { cbk.undefined3++ }
func (cbk *CountingLexerCallback) Start(time uint32) { cbk.start++ }
func (cbk *CountingLexerCallback) Continue(time uint32) { cbk.continue_++ }
func (cbk *CountingLexerCallback) Stop(time uint32) { cbk.stop++ }
func (cbk *CountingLexerCallback) Undefined4(time uint32) { cbk.undefined4++ }
func (cbk *CountingLexerCallback) ActiveSensing(time uint32) { cbk.activeSensing++ }
func (cbk *CountingLexerCallback) Reset(time uint32) { cbk.reset++ }
func (cbk *CountingLexerCallback) Done(time uint32) { cbk.done++ }
func (cbk *CountingLexerCallback) SequenceNumber(channel uint8, number uint16, numberGiven bool, time uint32) {
cbk.sequenceNumber++
cbk.time = time
cbk.sequenceNumberValue = number
cbk.sequenceNumberGiven = numberGiven
}
func (cbk *CountingLexerCallback) Text(channel uint8, text string, time uint32) {
cbk.text++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) CopyrightText(channel uint8, text string, time uint32) {
cbk.copyrightText++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) SequenceName(channel uint8, text string, time uint32) {
cbk.sequenceName++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) TrackInstrumentName(channel uint8, text string, time uint32) {
cbk.trackInstrumentName++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) LyricText(channel uint8, text string, time uint32) {
cbk.lyricText++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) MarkerText(channel uint8, text string, time uint32) {
cbk.markerText++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) CuePointText(channel uint8, text string, time uint32) {
cbk.cuePointText++
cbk.textValue = text
cbk.time = time
}
func (cbk *CountingLexerCallback) EndOfTrack(channel uint8, time uint32) {
cbk.endOfTrack++
cbk.time = time
}
func (cbk *CountingLexerCallback) TimeSignature(numerator uint8, denomenator uint8, clocksPerClick uint8, demiSemiQuaverPerQuarter uint8, time uint32) {
cbk.numerator = numerator
cbk.denomenator = denomenator
cbk.clocksPerClick = clocksPerClick
cbk.demiSemiQuaverPerQuarter = demiSemiQuaverPerQuarter
cbk.time = time
}
func (*CountingLexerCallback) KeySignature(key ScaleDegree, mode KeySignatureMode, sharpsOrFlats int8) {
// TODO fill out when tests written.
}
// MockReadSeeker is a mock Reader and Seeker. Constructed with data, behaves as a file reader.
// This is used to pass MIDI data to the Lexer and also to the MIDI value parsing functions.
type MockReadSeeker struct {
data *[]byte
position int64
}
// NewMockReadSeeker creates a new MockReadSeeker object backed by the given byte array data.
func NewMockReadSeeker(data *[]byte) *MockReadSeeker {
return &MockReadSeeker{data: data}
}
// Read fills the given buffer, returning the number of bytes and an error.
func (reader *MockReadSeeker) Read(p []byte) (n int, err error) {
var amount = int64(len(p))
var maxAmount = int64(len(*reader.data)) - reader.position
// Don't read past the end
if amount > maxAmount {
amount = maxAmount
}
copy(p, (*reader.data)[reader.position:reader.position+amount])
reader.position += amount
return int(amount), nil
}
// Seek sets the offset for the next Read or Write to offset, interpreted according to the value of `whence`:
// 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end.
// Seek returns the new offset and an Error, if any.
func (reader *MockReadSeeker) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
case 0:
{
if offset > int64(len(*reader.data)) {
return -1, io.EOF
}
reader.position = offset
return reader.position, nil
}
case 1:
{
if offset+reader.position > int64(len(*reader.data)) {
return -1, io.EOF
}
reader.position += offset
return reader.position, nil
}
case 2:
{
if offset > int64(len(*reader.data)) {
return -1, io.EOF
}
reader.position = int64(len(*reader.data)) - offset
return reader.position, nil
}
}
return
}