-
Notifications
You must be signed in to change notification settings - Fork 3
/
deansi_test.py
executable file
·290 lines (246 loc) · 7.46 KB
/
deansi_test.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python
"""
Copyright 2012 David Garcia Garzon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from deansi import *
html_template = """\
<style>
.ansi_terminal { background-color: #222; color: #cfc; }
%s
</style>
<div class='ansi_terminal'>%s</div>
"""
import unittest
class DeansiTest(unittest.TestCase) :
def assertDeansiEqual(self, expected, inputText) :
return self.assertEqual(expected, deansi(inputText))
def test_html(self) :
self.assertDeansiEqual(
'weee<>&',
'weee<>&',
)
def test_ansiAttributes_withSingleAttribute(self) :
self.assertEqual(
([45],'text'),
ansiAttributes("[45mtext")
)
def test_ansiAttributes_withManyAttributes(self) :
self.assertEqual(
([45,54,2],'text'),
ansiAttributes("[45;54;2mtext")
)
def test_ansiAttributes_withNoAttributes(self) :
self.assertEqual(
([], 'text'),
ansiAttributes("text")
)
def test_ansiAttributes_withNoNumbers(self) :
self.assertEqual(
([], '[a;bmtext'),
ansiAttributes("[a;bmtext")
)
def test_ansiAttributes_emptyReturnsZero(self) :
self.assertEqual(
([0], 'text'),
ansiAttributes("[mtext")
)
def test_ansiState_bright(self) :
self.assertEqual(
(set(['bright']), None, None),
ansiState(1, set(), None, None),
)
def test_ansiState_faint(self) :
self.assertEqual(
(set(['faint']), None, None),
ansiState(2, set(), None, None),
)
def test_ansiState_italic(self) :
self.assertEqual(
(set(['italic']), None, None),
ansiState(3, set(), None, None),
)
def test_ansiState_underscore(self) :
self.assertEqual(
(set(['underscore']), None, None),
ansiState(4, set(), None, None),
)
def test_ansiState_blink(self) :
self.assertEqual(
(set(['blink']), None, None),
ansiState(5, set(), None, None),
)
def test_ansiState_reverse(self) :
self.assertEqual(
(set(['reverse']), None, None),
ansiState(7, set(), None, None),
)
def test_ansiState_hide(self) :
self.assertEqual(
(set(['hide']), None, None),
ansiState(8, set(), None, None),
)
def test_ansiState_addTwoAttributes(self) :
self.assertEqual(
(set(['bright', 'blink']), None, None),
ansiState(1, set(['blink']), None, None),
)
def test_ansiState_clear_clearsBits(self) :
self.assertEqual(
(set(), None, None),
ansiState(0, set(['blink', 'whatever']), None, None),
)
def test_ansiState_setForeground(self) :
self.assertEqual(
(set(), 'green', None),
ansiState(32, set(), 'green', None),
)
def test_ansiState_setForegroundTwice(self) :
self.assertEqual(
(set(), 'red', None),
ansiState(31, set(), 'green', None),
)
def test_ansiState_setBackground(self) :
self.assertEqual(
(set(), None, 'yellow'),
ansiState(43, set(), None, None),
)
def test_ansiState_clearClearsFore(self) :
self.assertEqual(
(set(), None, None),
ansiState(0, set(), 'green', None),
)
def test_ansiState_clearClearsBack(self) :
self.assertEqual(
(set(), None, None),
ansiState(0, set(), None, 'green'),
)
def test_ansiState_noForeground(self) :
self.assertEqual(
(set(['blink','inverse']), None, 'red'),
ansiState(39, set(['blink','inverse']), 'green', 'red')
)
def test_ansiState_noBackground(self) :
self.assertEqual(
(set(['blink','inverse']), 'green', None),
ansiState(49, set(['blink','inverse']), 'green', 'red')
)
def test_ansiState_resetAttribute(self) :
self.assertEqual(
(set(['inverse']), 'green', 'red'),
ansiState(25, set(['blink','inverse']), 'green', 'red')
)
def test_ansiState_resetAttributeNotInThere(self) :
self.assertEqual(
(set(['inverse']), 'green', 'red'),
ansiState(25, set(['inverse']), 'green', 'red')
)
def test_stateToClasses_withAttribs(self) :
self.assertEqual(
"ansi_blink ansi_bright",
stateToClasses(set(['bright','blink']), None, None)
)
def test_stateToClasses_withFore(self) :
self.assertEqual(
"ansi_red",
stateToClasses(set(), 'red', None)
)
def test_stateToClasses_withBack(self) :
self.assertEqual(
"ansi_bgred",
stateToClasses(set(), None, 'red')
)
def test_stateToClasses_withAll(self) :
self.assertEqual(
"ansi_blink ansi_inverse ansi_green ansi_bgred",
stateToClasses(set(['blink','inverse']), 'green', 'red')
)
def test_deansi_withCodes(self) :
self.assertEqual(
'this should be <span class=\'ansi_red\'>red</span> and this not',
deansi('this should be \033[31mred\033[0m and this not'),
)
def test_deansi_emptyAttributeClears(self) :
self.assertEqual(
'this should be <span class=\'ansi_red\'>red</span> and this not',
deansi('this should be \033[31mred\033[m and this not'),
)
def test_deansi_withComplexCodes(self) :
self.assertEqual(
'this should be <span class=\'ansi_red\'>red</span>'
'<span class=\'ansi_bright ansi_red ansi_bggreen\'> and green background</span> and this not',
deansi('this should be \033[31mred\033[42;1m and green background\033[0m and this not'),
)
def test_deansi_takesMultiline(self) :
self.assertEqual(
'this should be <span class=\'ansi_red\'>\nred</span>'
'<span class=\'ansi_bright ansi_red ansi_bggreen\'> and green \nbackground\n</span> and this not',
deansi('this should be \033[31m\nred\033[42;1m and green \nbackground\n\033[0m and this not'),
)
def test_backToBack(self) :
terminalInput = """\
Normal colors:
\033[30mblack\033[0m\
\033[31mred\033[0m\
\033[32mgreen\033[0m\
\033[33myellow\033[0m\
\033[34mblue\033[0m\
\033[35mmagenta\033[0m\
\033[36mcyan\033[0m\
\033[37mwhite\033[0m\
\033[39mdefault\033[0m
Bright colors:
\033[1;30mblack\033[0m\
\033[1;31mred\033[0m\
\033[1;32mgreen\033[0m\
\033[1;33myellow\033[0m\
\033[1;34mblue\033[0m\
\033[1;35mmagenta\033[0m\
\033[1;36mcyan\033[0m\
\033[1;37mwhite\033[0m\
\033[1;39mdefault\033[0m
Background colors:
\033[40mblack\033[0m\
\033[41mred\033[0m\
\033[42mgreen\033[0m\
\033[43myellow\033[0m\
\033[44mblue\033[0m\
\033[45mmagenta\033[0m\
\033[46mcyan\033[0m\
\033[47mwhite\033[0m\
\033[49mdefault\033[0m
Attributes:
\033[1mbright\033[0m
\033[2mfaint\033[0m
\033[3mitalic\033[0m
\033[4munderscore\033[0m
\033[5mblink\033[0m
\033[6mdouble blink\033[0m <- not implemented
\033[7mreverse\033[0m <- TODO: Find a better way to implement it
\033[8mhide\033[0m <- It's hidden, you can still select and copy it
\033[9mstrike\033[0m
Activating \033[31mred and then \033[43mdark yellow
background and then activating \033[32mgreen foreground
now changing attribute to \033[1mbright and then
\033[21mreseting it without changing colors.
\033[44mblue background and \033[5mblink attribute,
\033[49mdefault background, unsetting \033[25mblink,
unsetting \033[39m foreground and \033[0mall attribs.
"""
# print (terminalInput)
expected = open("deansi-b2b.html").read()
result = html_template % (styleSheet(), deansi(terminalInput))
if (result!=expected) :
open("deansi-failed.html","w").write(result)
self.assertMultiLineEqual(expected, result)
if __name__ == "__main__" :
unittest.main()