| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from mido.midifiles.meta import ( |
| 8 |
KeySignatureError, |
| 9 |
MetaMessage, |
| 10 |
MetaSpec_key_signature, |
| 11 |
UnknownMetaMessage, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
def test_copy_invalid_argument(): |
| 16 |
with pytest.raises(ValueError): |
| 17 |
MetaMessage('track_name').copy(a=1) |
| 18 |
|
| 19 |
|
| 20 |
def test_copy_cant_override_type(): |
| 21 |
with pytest.raises(ValueError): |
| 22 |
MetaMessage('track_name').copy(type='end_of_track') |
| 23 |
|
| 24 |
|
| 25 |
class TestKeySignature: |
| 26 |
@pytest.mark.parametrize('bad_key_sig', [[8, 0], [8, 1], [0, 2], |
| 27 |
[9, 1], [255 - 7, 0]]) |
| 28 |
def test_bad_key_sig_throws_key_signature_error(self, bad_key_sig): |
| 29 |
with pytest.raises(KeySignatureError): |
| 30 |
MetaSpec_key_signature().decode(MetaMessage('key_signature'), |
| 31 |
bad_key_sig) |
| 32 |
|
| 33 |
@pytest.mark.parametrize('input_bytes,expect_sig', [([0, 0], 'C'), |
| 34 |
([0, 1], 'Am'), |
| 35 |
([255 - 6, 0], 'Cb'), |
| 36 |
([255 - 6, 1], 'Abm'), |
| 37 |
([7, 1], 'A#m') |
| 38 |
]) |
| 39 |
def test_key_signature(self, input_bytes, expect_sig): |
| 40 |
msg = MetaMessage('key_signature') |
| 41 |
MetaSpec_key_signature().decode(msg, input_bytes) |
| 42 |
assert msg.key == expect_sig |
| 43 |
|
| 44 |
|
| 45 |
def test_meta_message_repr(): |
| 46 |
msg = MetaMessage('end_of_track', time=10) |
| 47 |
msg_eval = eval(repr(msg)) |
| 48 |
assert msg == msg_eval |
| 49 |
|
| 50 |
|
| 51 |
def test_unknown_meta_message_repr(): |
| 52 |
msg = UnknownMetaMessage(type_byte=99, data=[1, 2], time=10) |
| 53 |
msg_eval = eval(repr(msg)) |
| 54 |
assert msg == msg_eval |
| 55 |
|
| 56 |
|
| 57 |
def test_meta_from_bytes_invalid(): |
| 58 |
test_bytes = [ |
| 59 |
0xC0, |
| 60 |
0x05 |
| 61 |
] |
| 62 |
with pytest.raises(ValueError): |
| 63 |
MetaMessage.from_bytes(test_bytes) |
| 64 |
|
| 65 |
|
| 66 |
def test_meta_from_bytes_data_too_short(): |
| 67 |
test_bytes = [ |
| 68 |
0xFF, |
| 69 |
0x01, |
| 70 |
0x04, |
| 71 |
ord('T'), ord('E'), ord('S'), |
| 72 |
] |
| 73 |
with pytest.raises(ValueError): |
| 74 |
MetaMessage.from_bytes(test_bytes) |
| 75 |
|
| 76 |
|
| 77 |
def test_meta_from_bytes_data_too_long(): |
| 78 |
test_bytes = [ |
| 79 |
0xFF, |
| 80 |
0x01, |
| 81 |
0x04, |
| 82 |
ord('T'), ord('E'), ord('S'), ord('T'), ord('S') |
| 83 |
] |
| 84 |
with pytest.raises(ValueError): |
| 85 |
MetaMessage.from_bytes(test_bytes) |
| 86 |
|
| 87 |
|
| 88 |
def test_meta_from_bytes_text(): |
| 89 |
test_bytes = [ |
| 90 |
0xFF, |
| 91 |
0x01, |
| 92 |
0x04, |
| 93 |
ord('T'), ord('E'), ord('S'), ord('T') |
| 94 |
] |
| 95 |
msg = MetaMessage.from_bytes(test_bytes) |
| 96 |
assert msg.type == 'text' |
| 97 |
assert msg.text == 'TEST' |