Skip to content

Commit 50f8c59

Browse files
committed
Template-ize visual_tests.py.
1 parent cf0d260 commit 50f8c59

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

visual_tests.py

+59-26
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,50 @@
1212

1313
from collections import defaultdict
1414

15+
16+
html_template = """<html><head><style media="screen" type="text/css">
17+
img{{
18+
width:100%;
19+
max-width:800px;
20+
}}
21+
</style>
22+
</head><body>
23+
{failed}
24+
{body}
25+
</body></html>
26+
"""
27+
28+
subdir_template = """<h2>{subdir}</h2><table>
29+
<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>
30+
{rows}
31+
</table>
32+
"""
33+
34+
failed_template = """<h2>Only Failed</h2><table>
35+
<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>
36+
{rows}
37+
</table>
38+
"""
39+
40+
row_template = ('<tr>'
41+
'<td>{0} {1}</td>'
42+
'<td>{2}</td>'
43+
'<td><a href="{3}"><img src="{3}"></a></td>'
44+
'<td>{4}</td>'
45+
'</tr>')
46+
47+
linked_image_template = '<a href="{0}"><img src="{0}"></a>'
48+
49+
1550
def run():
1651
# Build a website for visual comparison
1752
image_dir = "result_images"
1853
# build the website
19-
_html = ""
20-
_html += """<html><head><style media="screen" type="text/css">
21-
img{
22-
width:100%;
23-
max-width:800px;
24-
}
25-
</style>
26-
</head><body>\n"""
2754
_subdirs = [name for name in os.listdir(image_dir) if os.path.isdir(os.path.join(image_dir, name))]
2855
# loop over all pictures
29-
_row = '<tr><td>{0} {1}</td><td>{2}</td><td><a href="{3}"><img src="{3}"></a></td><td>{4}</td>\n'
30-
_failed = ""
31-
_failed += "<h2>Only Failed</h2>"
32-
_failed += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n"
3356
_has_failure = False
34-
_body = ""
57+
failed_rows = []
58+
body_sections = []
3559
for subdir in _subdirs:
3660
if subdir == "test_compare_images":
3761
# these are the image which test the image comparison functions...
@@ -51,37 +75,46 @@ def run():
5175
else:
5276
pictures[fn]["c"] = "/".join((subdir, file))
5377

54-
_body += "<h2>{0}</h2>".format(subdir)
55-
_body += "<table>\n<thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n"
78+
subdir_rows = []
5679
for name, test in six.iteritems(pictures):
5780
if test.get("f", None):
5881
# a real failure in the image generation, resulting in different images
5982
_has_failure = True
6083
s = "(failed)"
6184
failed = '<a href="{0}">diff</a>'.format(test.get("f", ""))
62-
current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", ""))
63-
_failed += _row.format(name, "", current, test.get("e", ""), failed)
85+
current = linked_image_template.format(test.get("c", ""))
86+
failed_rows.append(row_template.format(name, "", current,
87+
test.get("e", ""),
88+
failed))
6489
elif test.get("c", None) is None:
6590
# A failure in the test, resulting in no current image
6691
_has_failure = True
6792
s = "(failed)"
6893
failed = '--'
6994
current = '(Failure in test, no image produced)'
70-
_failed += _row.format(name, "", current, test.get("e", ""), failed)
95+
failed_rows.append(row_template.format(name, "", current,
96+
test.get("e", ""),
97+
failed))
7198
else:
7299
s = "(passed)"
73100
failed = '--'
74-
current = '<a href="{0}"><img src="{0}"></a>'.format(test.get("c", ""))
75-
_body += _row.format(name, "", current, test.get("e", ""), failed)
76-
_body += "</table>\n"
77-
_failed += "</table>\n"
101+
current = linked_image_template.format(test.get("c", ""))
102+
subdir_rows.append(row_template.format(name, "", current,
103+
test.get("e", ""), failed))
104+
105+
body_sections.append(
106+
subdir_template.format(subdir=subdir, rows='\n'.join(subdir_rows)))
107+
78108
if _has_failure:
79-
_html += _failed
80-
_html += _body
81-
_html += "\n</body></html>"
109+
failed = failed_template.format(rows='\n'.join(failed_rows))
110+
else:
111+
failed = ''
112+
body = ''.join(body_sections)
113+
html = html_template.format(failed=failed, body=body)
82114
index = os.path.join(image_dir, "index.html")
83115
with open(index, "w") as f:
84-
f.write(_html)
116+
f.write(html)
117+
85118
try:
86119
import webbrowser
87120
webbrowser.open(index)

0 commit comments

Comments
 (0)