-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_alsamidi.py
328 lines (225 loc) · 8.15 KB
/
test_alsamidi.py
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
from __future__ import print_function
from unittest import TestCase
class NoteEvent(TestCase):
def test(self):
from alsamidi import noteevent
midi_event = (1, 60, 127, 1000, 10)
data = (1, 60, 127, 0, 10)
expected = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, noteevent(*midi_event))
class NoteOnEvent(TestCase):
def test(self):
from alsamidi import noteonevent
midi_event = (1, 60, 127)
data = (1, 60, 127, 0, 0)
expected = (6, 1, 0, 253, (0, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, noteonevent(*midi_event))
class NoteOffEvent(TestCase):
def test(self):
from alsamidi import noteoffevent
midi_event = (1, 60, 10)
data = (1, 60, 10, 0, 0)
expected = (7, 1, 0, 253, (0, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, noteoffevent(*midi_event))
class PgmChangeEvent(TestCase):
def test_sent_directly(self):
from alsamidi import pgmchangeevent
data = (1, 0, 0, 0, 0, 9)
expected = (11, 1, 0, 253, (0, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, pgmchangeevent(1, 9))
def test_scheduled(self):
from alsamidi import pgmchangeevent
data = (1, 0, 0, 0, 0, 9)
expected = (11, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, pgmchangeevent(1, 9, 1000))
class PitchbendEvent(TestCase):
def test_sent_directly(self):
from alsamidi import pitchbendevent
data = (1, 0, 9)
expected = (13, 1, 0, 253, (0, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, pitchbendevent(1, 9))
def test_scheduled(self):
from alsamidi import pitchbendevent
data = (1, 0, 9)
expected = (13, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, pitchbendevent(1, 9, 1000))
class ChanPress(TestCase):
def test_sent_directly(self):
from alsamidi import chanpress
data = (1, 0, 9)
expected = (12, 1, 0, 253, (0, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, chanpress(1, 9))
def test_scheduled(self):
from alsamidi import chanpress
data = (1, 0, 9)
expected = (12, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
self.assertEqual(expected, chanpress(1, 9, 1000))
class Tuple2Time(TestCase):
def test(self):
from alsamidi import tuple2time
self.assertEqual(1.5, tuple2time((1, 500000000)))
class Time2Tuple(TestCase):
def test(self):
from alsamidi import time2tuple
self.assertEqual((1, 500000000), time2tuple(1.5))
class ModifyEvent(TestCase):
def test(self):
from alsamidi import modifyevent
data = (1, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
modified_data = (15, 72, 127, 0, 10)
modified_event = (5, 1, 0, 250, (2, 0), (2, 0), (3, 0), modified_data)
self.assertEqual(modified_event, modifyevent(event, timedelta=1,
ch=15, dest=(3, 0), source=(2, 0), queue=250, keydelta=12))
class ModifyEvents(TestCase):
def test(self):
from alsamidi import modifyevents
data = (1, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
modified_data = (15, 72, 127, 0, 10)
modified_event = (5, 1, 0, 250, (2, 0), (2, 0), (3, 0), modified_data)
self.assertEqual([modified_event], modifyevents([event], timedelta=1,
ch=15, dest=(3, 0), source=(2, 0), queue=250, keydelta=12))
class Merge(TestCase):
def test_no_tracks(self):
from alsamidi import merge
self.assertEqual([], merge([]))
def test_track(self):
from alsamidi import merge
event = (0, 0, 0, 0, 0)
self.assertEqual([event], merge([[event]]))
class UniqueNotes(TestCase):
def test(self):
from alsamidi import uniquenotes
data = (1, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
expected = {1: [60]}
self.assertEqual(expected, uniquenotes([event]))
class TestSeq(TestCase):
def setUp(self):
try:
from mock import Mock
except:
from unittest.mock import Mock
import alsamidi
alsamidi.print = Mock()
def tearDown(self):
import alsamidi
del alsamidi.print
def test_instance(self):
from alsamidi import Seq
self.assertTrue(isinstance(Seq(), Seq))
def test_info(self):
from alsamidi import Seq
import alsamidi
data = (9, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
track = [event]
seq = Seq()
seq.names = ['name']
seq.tags = 'tags'
seq.tracks = [track]
seq.info()
alsamidi.print.assert_any_call('tags')
args = '0:', 'name ', 0, 'Sec.', 1, 'events,', {9: [60]}, 'Hi Bongo'
alsamidi.print.assert_called_with(*args)
class SeqRead(TestCase):
def setUp(self):
import os
try:
from mock import Mock
except:
from unittest.mock import Mock
import alsamidi
self.file = Mock()
text = 'track melody\n' \
'6,1,0,1,2 5430786,20 0,130 0,1 108 82 0 0\n' \
'name = value'
lines = text.split('\n')
self.file.readlines.return_value = lines
lines[0] = lines[0] + '\n'
alsamidi.open = Mock(return_value=self.file)
alsamidi.print = Mock()
self.exits = os.path.exists
os.path.exists = Mock(return_value=True)
def tearDown(self):
import os
import alsamidi
del alsamidi.open
del alsamidi.print
def test_read_not_found(self):
import os
from alsamidi import Seq
os.path.exists.return_value = False
seq = Seq()
seq.read('path')
self.assertEqual([], seq.tracks)
def test_read_named_track(self):
from alsamidi import Seq
seq = Seq()
seq.read('path')
self.assertEqual(['track melody'], seq.names)
def test_read_default_track(self):
from alsamidi import Seq
text = '6,1,0,1,2 5430786,20 0,130 0,1 108 82 0 0'
self.file.readlines.return_value = text.split('\n')
seq = Seq()
seq.read('path')
self.assertEqual(['Default'], seq.names)
def test_read_track(self):
from alsamidi import Seq
seq = Seq()
seq.read('path')
self.assertEqual([[
(6, 1, 0, 1,
(2, 5430786),
(20, 0),
(130, 0), (1, 108, 82, 0, 0))]], seq.tracks)
class SeqWrite(TestCase):
def setUp(self):
try:
from mock import Mock
except:
from unittest.mock import Mock
import alsamidi
self.file = Mock()
alsamidi.open = Mock(return_value=self.file)
alsamidi.print = Mock()
def tearDown(self):
import alsamidi
del alsamidi.open
del alsamidi.print
def test_write(self):
from alsamidi import Seq
data = (9, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
track = [event]
seq = Seq()
seq.names = ['name']
seq.tags = {'name': 'value'}
seq.orderedtags = ['name']
seq.tracks = [track]
seq.write('path')
line = '5,1,0,0,1 0,0 0,0 0,9 60 127 0 10\n'
self.file.write.assert_called_with(line)
def test_write_no_name(self):
from alsamidi import Seq
data = (9, 60, 127, 0, 10)
event = (5, 1, 0, 0, (1, 0), (0, 0), (0, 0), data)
track = [event]
seq = Seq()
seq.names = []
seq.tags = {'name': 'value'}
seq.orderedtags = ['name']
seq.tracks = [track]
seq.write('path')
line = '5,1,0,0,1 0,0 0,0 0,9 60 127 0 10\n'
self.file.write.assert_called_with(line)
def test_error(self):
from alsamidi import Seq
import alsamidi
alsamidi.open.side_effect = IOError
seq = Seq()
seq.write('path')
if __name__ == '__main__':
unittest.main()