forked from frescobaldi/python-ly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_xml.py
More file actions
95 lines (67 loc) · 2.37 KB
/
test_xml.py
File metadata and controls
95 lines (67 loc) · 2.37 KB
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
"""Tests for XML output."""
import datetime
import difflib
import ly.musicxml
from lxml import etree
import os
import io
def test_glissando():
compare_output('glissando')
def test_tie():
compare_output('tie')
def test_merge_voice():
compare_output('merge_voice')
def test_variable():
compare_output('variable')
def test_dynamics():
compare_output('dynamics')
def test_tuplet():
compare_output('tuplet')
def test_partial():
compare_output('partial')
def ly_to_xml(filename):
"""Read Lilypond file and return XML string."""
writer = ly.musicxml.writer()
with open(filename, 'r') as lyfile:
writer.parse_text(lyfile.read())
xml = writer.musicxml()
sio = io.StringIO()
xml.write(sio, "utf-8")
return sio.getvalue()
def read_expected_xml(filename):
"""Return string with expected XML from file."""
with open(filename, 'r') as xmlfile:
output = xmlfile.read()
# Replace date in XML file with today's date
output = output.replace("2016-03-28", str(datetime.date.today()))
return output
def compare_output(filename):
"""Compare XML output with expected output."""
filebase = os.path.join(os.path.dirname(__file__), 'test_xml_files',
filename)
output = ly_to_xml(filebase + '.ly')
expected_output = read_expected_xml(filebase + '.xml')
assert_multi_line_equal(expected_output, output)
validate_xml(output)
def validate_xml(xml):
"""Validate XML against XSD file."""
xsdname = os.path.join(os.path.dirname(__file__), 'musicxml.xsd')
xsdfile = open(xsdname, 'r')
xmlschema_doc = etree.parse(xsdfile)
xsdfile.close()
xmlschema = etree.XMLSchema(xmlschema_doc)
parser = etree.XMLParser(schema=xmlschema)
# Raises Exception if not valid:
etree.fromstring(xml, parser)
def assert_multi_line_equal(first, second, msg=None):
"""Assert that two multi-line strings are equal.
If they aren't, show a nice diff.
"""
assert isinstance(first, str), 'First argument is not a string'
assert isinstance(second, str), 'Second argument is not a string'
if first != second:
message = ''.join(difflib.ndiff(first.splitlines(True),
second.splitlines(True)))
if msg:
message += " : " + msg
assert False, "Multi-line strings are unequal:\n" + message