Debian Codegraph @

tests/test_xml.py

raw
1 """Tests for XML output."""
2 import datetime
3 import difflib
4 import glob
5 import ly.musicxml
6 from lxml import etree
7 import os
8 import os.path
9 import io
10 import pytest
11 import re
12 import sys
13
14
15 def test_glissando():
16 compare_output('glissando')
17
18
19 def test_tie():
20 compare_output('tie')
21
22
23 def test_merge_voice():
24 compare_output('merge_voice')
25
26
27 def test_variable():
28 compare_output('variable')
29
30
31 def test_dynamics():
32 compare_output('dynamics')
33
34
35 def test_tuplet():
36 compare_output('tuplet')
37
38 def test_merge_voice_slurs():
39 compare_output('merge_voice_slurs')
40
41 def test_break():
42 compare_output('break')
43
44
45 def test_mark():
46 compare_output('mark')
47
48
49 @pytest.mark.xfail
50 def test_partial():
51 compare_output('partial')
52
53
54 def test_full_bar():
55 compare_output('full_bar_rest')
56
57
58 def test_stem_direction():
59 compare_output('stem')
60
61
62 def test_church():
63 compare_output('church_modes')
64
65
66 @pytest.mark.xfail
67 def test_markup():
68 compare_output('markup')
69
70
71 def test_breathe():
72 compare_output('breathe')
73
74
75 def test_no_barcheck():
76 compare_output('no_barcheck')
77
78
79 def ly_to_xml(filename):
80 """Read Lilypond file and return XML string."""
81 writer = ly.musicxml.writer()
82 with open(filename, 'r') as lyfile:
83 writer.parse_text(lyfile.read())
84 xml = writer.musicxml()
85 sio = io.BytesIO()
86 xml.write(sio, "utf-8")
87 return sio.getvalue().decode("utf-8")
88
89 encoding_date_element_re = re.compile(r'(?<=<encoding-date>)\d{4}-\d{2}-\d{2}(?=</encoding-date>)')
90
91 def read_expected_xml(filename):
92 """Return string with expected XML from file."""
93 with open(filename, 'r') as xmlfile:
94 output = xmlfile.read()
95 # Replace date in XML file with today's date
96 output = encoding_date_element_re.sub(str(datetime.date.today()), output)
97 return output
98
99
100 def compare_output(filename):
101 """Compare XML output with expected output."""
102 filebase = os.path.join(os.path.dirname(__file__), 'test_xml_files',
103 filename)
104
105 output = ly_to_xml(filebase + '.ly')
106 expected_output = read_expected_xml(filebase + '.xml')
107
108 assert_multi_line_equal(expected_output, output)
109 validate_xml(output)
110
111
112 def validate_xml(xml):
113 """Validate XML against XSD file."""
114 xsdname = os.path.join(os.path.dirname(__file__), 'musicxml.xsd')
115 xsdfile = open(xsdname, 'r')
116 xmlschema_doc = etree.parse(xsdfile)
117 xsdfile.close()
118 xmlschema = etree.XMLSchema(xmlschema_doc)
119 parser = etree.XMLParser(schema=xmlschema)
120 xml_bytes = xml.encode('utf-8')
121 # Raises Exception if not valid:
122 etree.fromstring(xml_bytes, parser)
123
124
125 def assert_multi_line_equal(first, second, msg=None):
126 """Assert that two multi-line strings are equal.
127
128 If they aren't, show a nice diff.
129 """
130 assert isinstance(first, str), 'First argument is not a string'
131 assert isinstance(second, str), 'Second argument is not a string'
132
133 if first != second:
134 message = ''.join(difflib.ndiff(first.splitlines(True),
135 second.splitlines(True)))
136 if msg:
137 message += " : " + msg
138 assert False, "Multi-line strings are unequal:\n" + message
139
140
141 def regenerate_xml():
142 """Regenerate the XML files"""
143 extension_re = re.compile(r'\.ly$')
144 for ly_path in glob.glob(os.path.join(os.path.dirname(__file__), 'test_xml_files/*.ly')):
145 xml_path = extension_re.sub('.xml', ly_path)
146 xml = ly_to_xml(ly_path)
147 with open(xml_path, 'w') as fw:
148 fw.write(xml)
149
150
151 # Run
152 # $ test_xml.py regenerate
153 # to generate the expected XML files anew with current python-ly
154 if __name__ == '__main__':
155 if len(sys.argv) > 1 and sys.argv[1] == 'regenerate':
156 regenerate_xml()