PageRenderTime 133ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 60ms app.codeStats 0ms

/pyjs/tests/test-coverage.py

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