Debian Codegraph @

tests/messages/test_messages.py

raw symbols SCIP
1 # SPDX-FileCopyrightText: 2017 Ole Martin Bjorndalen <[email protected]>
2 #
3 # SPDX-License-Identifier: MIT
4
5 from pytest import raises
6
7 from mido.messages.messages import Message, SysexData
8 from mido.messages.specs import MAX_PITCHWHEEL, MAX_SONGPOS, MIN_PITCHWHEEL, MIN_SONGPOS
9
10
11 def test_msg_time_equality():
12 # Since 1.1.18 time is included in comparison.
13 assert Message('clock', time=0) == Message('clock', time=0)
14 assert Message('clock', time=0) != Message('clock', time=1)
15
16
17 def test_set_type():
18 """Can't change the type of a message."""
19 with raises(AttributeError):
20 Message('note_on').type = 'note_off'
21
22
23 def test_encode_pitchwheel():
24 assert 'E0 00 00' == Message('pitchwheel', pitch=MIN_PITCHWHEEL).hex()
25 assert 'E0 00 40' == Message('pitchwheel', pitch=0).hex()
26 assert 'E0 7F 7F' == Message('pitchwheel', pitch=MAX_PITCHWHEEL).hex()
27
28
29 def test_decode_pitchwheel():
30 assert Message.from_hex('E0 00 00').pitch == MIN_PITCHWHEEL
31 assert Message.from_hex('E0 00 40').pitch == 0
32 assert Message.from_hex('E0 7F 7F').pitch == MAX_PITCHWHEEL
33
34
35 def test_encode_songpos():
36 assert 'F2 00 00' == Message('songpos', pos=MIN_SONGPOS).hex()
37 assert 'F2 7F 7F' == Message('songpos', pos=MAX_SONGPOS).hex()
38
39
40 def test_decode_songpos():
41 assert Message.from_hex('F2 00 00').pos == MIN_SONGPOS
42 assert Message.from_hex('F2 7F 7F').pos == MAX_SONGPOS
43
44
45 def test_sysex_data_is_sysexdata_object():
46 assert isinstance(Message.from_hex('F0 00 F7').data, SysexData)
47
48
49 def test_sysex_data_accepts_different_types():
50 assert Message('sysex', data=(0, 1, 2)).data == (0, 1, 2)
51 assert Message('sysex', data=[0, 1, 2]).data == (0, 1, 2)
52 assert Message('sysex', data=range(3)).data == (0, 1, 2)
53 assert Message('sysex', data=bytearray([0, 1, 2])).data == (0, 1, 2)
54 assert Message('sysex', data=b'\x00\x01\x02').data == (0, 1, 2)
55
56
57 def test_copy():
58 assert Message('start').copy(time=1) == Message('start', time=1)
59
60
61 def test_init_invalid_argument():
62 with raises(ValueError):
63 Message('note_on', zzzzzzzzzzzz=2)
64
65 with raises(ValueError):
66 # note_on doesn't take program.
67 Message('note_on', program=2)
68
69
70 def test_copy_invalid_argument():
71 with raises(ValueError):
72 Message('note_on').copy(zzzzzzzzzzzz=2)
73
74 with raises(ValueError):
75 # note_on doesn't take program.
76 Message('note_on').copy(program=2)
77
78
79 def test_copy_cant_change_type():
80 with raises(ValueError):
81 Message('start').copy(type='stop')
82
83
84 def test_copy_can_have_same_type():
85 Message('start').copy(type='start')
86
87
88 def test_copy_handles_data_generator():
89 msg1 = Message('sysex')
90 msg2 = msg1.copy(data=(i for i in range(3)))
91 assert msg2.data == (0, 1, 2)
92 assert isinstance(msg2.data, SysexData)
93
94
95 def test_compare_with_nonmessage():
96 with raises(TypeError):
97 assert Message('clock') == 'not a message'
98
99
100 def test_from_dict_default_values():
101 msg = Message('note_on', channel=0, note=0, time=0)
102 data = {'type': 'note_on'}
103 assert Message.from_dict(data) == msg
104
105
106 def test_dict_sysex_data():
107 msg = Message('sysex', data=(1, 2, 3))
108 data = msg.dict()
109 assert data == {'type': 'sysex', 'data': [1, 2, 3], 'time': 0}
110 assert isinstance(data['data'], list)
111
112
113 def test_from_hex_sysex_data_type():
114 msg = Message.from_hex('F0 01 02 03 F7')
115 assert isinstance(msg.data, SysexData)
116
117
118 def test_repr():
119 msg = Message('note_on', channel=1, note=2, time=3)
120 msg_eval = eval(repr(msg)) # noqa: S307
121 assert msg == msg_eval