-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.py
195 lines (170 loc) · 7.74 KB
/
Report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
"""
Report.py adds relevant logging for
"""
__date__ = "20130101"
__author__ = "jlettvin"
__maintainer__ = "jlettvin"
__email__ = "[email protected]"
__copyright__ = "Copyright(c) 2013 Jonathan D. Lettvin, All Rights Reserved"
__license__ = "GPLv3"
__status__ = "Production"
__version__ = "0.0.1"
"""
Report.py
Report.py generates HTML reports based on a simple markup language.
Copyright(c) 2013 Jonathan D. Lettvin, All Rights Reserved"
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os, sys
sys.path.append('..')
from Logger import Logger
from Tag import *
render = rules['render']
class Report(Logger):
def __init__(self, markup):
prefix = {'.': 'passed', '?': 'warning', '!': 'error', '^': 'header'}
with TABLE():
for line in markup:
with TR():
for cell in [chunk.strip() for chunk in line.split('|')]:
style = 'info'
if cell[0] in prefix.keys():
style = prefix[cell[0]]
cell = cell[1:]
attributes = {'value': cell}
attributes.update(render['cell.%s' % (style)])
TD('close', **attributes)
@property
def final(self):
return TAG.final()
if __name__ == "__main__":
##########################################################################
# TEST RESOURCES
# Methods for coloring output text.
g_ = Color(foreground='green', background='black', render='bright')
rw = Color(foreground='red' , background='white', render='bright')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def whoami(level):
"""Introspect the name of the calling function."""
return inspect.stack()[level][3]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def showDiff(actual, expect):
"Display colored text for two buffers with diff style markers."
# Prefer to put actual before expect since actual will be shown in red.
if actual != expect:
print>>sys.stderr, '<'*79
print>>sys.stderr, rw(actual)
print>>sys.stderr, '='*79
print>>sys.stderr, g_(expect)
print>>sys.stderr, '>'*79
"""
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def assertPass():
testname = whoami(2)
print g_('[PASS]'), testname
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def assertFail():
testname = whoami(2)
print rw('[FAIL]'), testname
"""
# assert methods for use by test methods.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def assertPassFail(actual, expect, **kw):
level = kw.get('level', 1)
check = kw.get('check', True)
force = kw.get('force', False)
testname = whoami(level+1)
passfail = ((actual == expect) == check)
print (g_('[PASS]') if passfail else rw('[FAIL]')), testname
#if not passfail or force: showDiff(actual, expect)
showDiff(actual, expect)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def assertEqual(actual, expect, **kw):
kw2 = kw.copy()
kw2['level']=2
assertPassFail(actual, expect, **kw2)
"""
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def assertNotEqual(actual, expect, **kw):
kw2 = kw.copy()
kw2['level']=2
kw2['check']=False
assertPassFail(actual, expect, **kw2)
"""
#TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
def test_simple():
expect = """\
<table>
<tr>
<td bgcolor="white" align="center" value="a" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="b" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="c" fontattribute="normal" width="500" href="" fontcolor="black"/>
</tr>
<tr>
<td bgcolor="green" align="center" value="d" fontattribute="bold" width="500" href="" fontcolor="black"/>
<td bgcolor="yellow" align="center" value="e" fontattribute="bold" width="500" href="" fontcolor="brown"/>
<td bgcolor="black" align="center" value="f" fontattribute="bold" width="500" href="" fontcolor="red"/>
</tr>
<tr>
<td bgcolor="white" align="center" value="g" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="h" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="i" fontattribute="normal" width="500" href="" fontcolor="black"/>
</tr>
</table>
"""
assertEqual(Report(['a|b|c', '.d|?e|!f', 'g|h|i']).final, expect)
#TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
def test_constructed():
expect = """\
<table>
<tr>
<td bgcolor="white" align="center" value="x" fontattribute="bold" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="y" fontattribute="bold" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="z" fontattribute="bold" width="500" href="" fontcolor="black"/>
</tr>
<tr>
<td bgcolor="white" align="center" value="a" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="b" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="c" fontattribute="normal" width="500" href="" fontcolor="black"/>
</tr>
<tr>
<td bgcolor="green" align="center" value="d" fontattribute="bold" width="500" href="" fontcolor="black"/>
<td bgcolor="yellow" align="center" value="e" fontattribute="bold" width="500" href="" fontcolor="brown"/>
<td bgcolor="black" align="center" value="f" fontattribute="bold" width="500" href="" fontcolor="red"/>
</tr>
<tr>
<td bgcolor="white" align="center" value="g" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="h" fontattribute="normal" width="500" href="" fontcolor="black"/>
<td bgcolor="white" align="center" value="i" fontattribute="normal" width="500" href="" fontcolor="black"/>
</tr>
</table>
"""
xyz_str = x, y, z = ['x', 'y', 'z']
abc_str = a, b, c = ['a', 'b', 'c']
def_str = d, e, f = ['d', 'e', 'f']
ghi_str = g, h, i = ['g', 'h', 'i']
passed, warn, error, header = ['.', '?', '!', '^']
square = [
string.join([header+x, header+y, header+z], '|'),
string.join(abc_str, '|'),
string.join([passed+d, warn+e, error+f], '|'),
string.join(ghi_str, '|'),]
assertEqual(Report(square).final, expect)
def test_100_percent():
two = ['a', 'b']
with open(os.devnull, 'w') as devnull:
with RedirectStdStreams(stdout=devnull, stderr=devnull):
showDiff(two[0], two[1])
test_simple()
test_constructed()
test_100_percent()