PageRenderTime 51ms CodeModel.GetById 14ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 2ms

/pyjs/tests/test-report.py

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