/nodebox/tests/vdiff/tests.py

https://github.com/fone4u/nodebox-pyobjc
Python | 59 lines | 52 code | 7 blank | 0 comment | 9 complexity | 33e60ad935f7025991116acb33e9f3b8 MD5 | raw file
  1. import os, glob
  2. from nodebox.util import vdiff
  3. TEST_PATH = 'images'
  4. create_compare_image = True
  5. def format_stats(name, stats, compare_image=None):
  6. html = """<h2>%s</h1>\n""" % name
  7. html += """<img src="%s" width="150" height="150">\n""" % stats.fname1
  8. html += """<img src="%s" width="150" height="150">\n""" % stats.fname2
  9. if compare_image is not None:
  10. html += """<img class="compare" src="%s" width="150" height="150">\n""" % compare_image
  11. html += """<table class="statistics">\n"""
  12. html += """<tr class="odd"><td>Differences:</td><td>%i</td></tr>\n""" % len(stats.differences)
  13. html += """<tr class="even"><td>Total delta:</td><td>%i</td></tr>\n""" % stats.total_delta
  14. html += """<tr class="odd"><td>Max delta:</td><td>%i</td></tr>\n""" % stats.max_delta
  15. html += """<tr class="even"><td>Mean:</td><td>%.4f</td></tr>\n""" % stats.mean
  16. html += """<tr class="odd"><td>Stdev:</td><td>%.4f</td></tr>\n""" % stats.stdev
  17. html += """</table>\n"""
  18. return html
  19. html = """<html>
  20. <head>
  21. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  22. <title>vdiff tests</title>
  23. <link rel="stylesheet" type="text/css" href="images/vdiff.css" media="all">
  24. </head>
  25. <body>
  26. <h1>vdiff tests</h1>
  27. """
  28. for testPath in glob.glob('%s/*' % TEST_PATH):
  29. if not os.path.isdir(testPath): continue
  30. name = os.path.basename(testPath)
  31. print name
  32. testFiles = glob.glob('%s/*.png' % testPath)
  33. try:
  34. testFiles.remove('%s/_compare.png' % testPath)
  35. except ValueError: pass
  36. if len(testFiles) == 2:
  37. fname1, fname2 = testFiles
  38. stats = vdiff.Statistics(fname1, fname2)
  39. if create_compare_image:
  40. compare_image = vdiff.make_comparison_image(stats.size, stats.differences)
  41. compare_image_fname = '%s/_compare.png' % testPath
  42. compare_image.save(compare_image_fname)
  43. html += format_stats(name, stats, compare_image_fname)
  44. else:
  45. html += format_stats(name, stats)
  46. else:
  47. print "path %s has more than two PNG images: %s" % (testPath, testFiles)
  48. html += """</body>\n</html>\n"""
  49. fp = open('test-results.html', 'w')
  50. fp.write(html)
  51. fp.close()
  52. print "Generated test-results.html"