PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/pyjs/tests/test-coverage.py

http://pyjamas.googlecode.com/
Python | 168 lines | 159 code | 8 blank | 1 comment | 3 complexity | d95b38c1f2d7849201404b6bb57de105 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. #!/usr/bin/env python
  2. import sys
  3. class Coverage:
  4. def __init__(self, testset_name):
  5. self.testset_name = testset_name
  6. self.lines = {}
  7. def tracer(self, frame, event, arg):
  8. lineno = frame.f_lineno
  9. filename = frame.f_globals["__file__"]
  10. if filename[-4:] in [".pyc", ".pyo"]:
  11. filename = filename[:-1]
  12. self.lines[filename][lineno] = self.lines.setdefault(filename, {}).get(lineno, 0) + 1
  13. return self.tracer
  14. def start(self):
  15. sys.settrace(self.tracer)
  16. def stop(self):
  17. sys.settrace(None)
  18. def output(self, *files):
  19. print """
  20. <html>
  21. <head>
  22. <title>Coverage for %s</title>
  23. <style>
  24. body {
  25. color: #000;
  26. background-color: #FFF;
  27. }
  28. h1, h2 {
  29. font-family: sans-serif;
  30. font-weight: normal;
  31. }
  32. td {
  33. white-space: pre;
  34. padding: 1px 5px;
  35. font-family: monospace;
  36. font-size: 10pt;
  37. }
  38. td.hit {
  39. }
  40. td.hit-line {
  41. }
  42. td.miss {
  43. background-color: #C33;
  44. }
  45. td.miss-line {
  46. background-color: #FCC;
  47. }
  48. td.ignore {
  49. color: #999;
  50. }
  51. td.ignore-line {
  52. color: #999;
  53. }
  54. td.lineno {
  55. color: #999;
  56. background-color: #EEE;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. """ % self.testset_name
  62. print """
  63. <h1>Coverage for %s</h1>
  64. """ % self.testset_name
  65. for filename in files:
  66. print """
  67. <h2>%s</h2>
  68. <table>
  69. """ % filename
  70. code = open(filename).readlines()
  71. for lineno, line in enumerate(code):
  72. count = self.lines[filename].get(lineno + 1, 0)
  73. if count == 0:
  74. if line.strip() in ["", "else:"] or line.strip().startswith("#"):
  75. klass = "ignore"
  76. else:
  77. klass = "miss"
  78. else:
  79. klass = "hit"
  80. klass2 = klass + "-line"
  81. print """<tr><td class="lineno">%s</td><td class="%s">%s</td><td class="%s">%s</td></tr>""" % (lineno + 1, klass, count, klass2, line.strip("\n"))
  82. print """
  83. </table>
  84. """
  85. print """
  86. </body>
  87. </html>
  88. """
  89. # Tester
  90. import sys
  91. sys.path.append("..")
  92. import pyjs
  93. def pyjs_tester(filename, module):
  94. output = pyjs.translate(filename + ".py", module)
  95. # Test Plan
  96. pyjs_test = [
  97. ("test001", "ui"),
  98. ("test002", "ui"),
  99. ("test003", "ui"),
  100. ("test004", "ui"),
  101. ("test005", "ui"),
  102. ("test006", "ui"),
  103. ("test007", "ui"),
  104. ("test008", "ui"),
  105. ("test009", "ui"),
  106. ("test010", None),
  107. ("test011", None),
  108. ("test012", None),
  109. ("test013", "ui"),
  110. ("test014", None),
  111. ("test015", None),
  112. ("test016", None),
  113. ("test017", None),
  114. ("test018", None),
  115. ("test019", None),
  116. ("test020", None),
  117. ("test021", None),
  118. ("test022", None),
  119. ("test023", None),
  120. ("test024", None),
  121. ("test025", None),
  122. ("test026", None),
  123. ("test027", None),
  124. ("test028", None),
  125. ("test029", None),
  126. ("test030", None),
  127. ("test031", None),
  128. ("test032", None),
  129. ("test033", None),
  130. ("test034", None),
  131. ("test035", None),
  132. ("test036", None),
  133. ("test037", None),
  134. ("test038", None),
  135. ("test039", None),
  136. ("test040", None),
  137. ("test041", None),
  138. ("test042", None),
  139. ("test043", None),
  140. ("test044", None),
  141. ("test045", None),
  142. ("test046", None),
  143. ]
  144. c = Coverage("pyjs unit tests")
  145. c.start()
  146. for filename, module in pyjs_test:
  147. pyjs_tester(filename, module)
  148. c.stop()
  149. c.output("../pyjs.py")