/usr/share/pyshared/slimmer/tests/testSlimmer.py is in python-slimmer 0.1.30-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | import os
from time import time
import unittest
import codecs
import sys
sys.path.insert(0,'../')
import slimmer
VERBOSE = False
from codechunks import *
class SlimmerTestCase(unittest.TestCase):
def _assert(self, str1, str2, name=''):
""" special kind of assertEqual that strips """
if not str1.strip() == str2.strip():
print '\n'+"-"*70
print "DIFFERENCES %s"%name
print "----Result|"+"-"*50
#print repr(str1.strip())
print str1.strip()
print "----Expect|"+"-"*50
#print repr(str2.strip())
print str2.strip()
print "-"*70
x = str1.strip()
y = str2.strip()
print x
arrow = ''
for i, e in enumerate(list(x)):
if e==y[i]:
arrow += "-"
else:
arrow += "^"
break
print arrow
self.assertEqual(str1.strip(), str2.strip())
#assert str1.strip() == str2.strip()
#return self.assertEqual(str1.strip(), str2.strip())
def timer(self, name, timeittook, size1, size2):
record = {'name':name, 'time':timeittook,
'size1':size1, 'size2':size2}
records = self.timed_records
records.append(record)
self.time_records = records
def setUp(self):
self.timed_records=[]
def tearDown(self):
for record in self.timed_records:
if VERBOSE:
print "-- %s --:"%record['name']
print "%s seconds"%round(record['time'], 6)
print "Was: %s\tNow: %s"%(record['size1'], record['size2'])
percent = round(100*record['size2']/float(record['size1']), 5)
percent = "%s%%"%percent
difference = record['size1'] - record['size2']
print "Difference: %s (%s)"%(difference, percent)
print
def atest(self, str1, str2, name, func, printresult=VERBOSE, *args, **kw):
""" standard type of test """
before = str1
expect = str2
t0=time()
args = [before]+list(args)
result = apply(func, args, kw)
if printresult:
print result
T = time()-t0
self.timer(name,time()-t0, len(before), len(result))
self._assert(result, expect, name)
#--- Start the madness! ----------------------------------------------
def testGuessSyntax(self):
for var, value in globals().items():
syntax = None
if var.find('CSS')> -1:
syntax = slimmer.guessSyntax(value)
self.assertEqual(syntax, 'css')
elif var.find('JS')> -1:
syntax = slimmer.guessSyntax(value)
self.assertEqual(syntax, 'js')
elif var.find('HTML')> -1:
syntax = slimmer.guessSyntax(value)
self.assertTrue(syntax in ('html','xhtml'))
else:
continue
def testGuessHTMLorXHTML(self):
code = """<ul>
<li>One</li>
<li>Two</li>
</ul>"""
self.assertEqual(slimmer.guessSyntax(code), 'html')
code = """
<a href="foo.html"><img src="foo.jpg" /></a>
"""
self.assertEqual(slimmer.guessSyntax(code), 'xhtml')
def testCSS1(self):
before = CSS_1
expect = expect_CSS_1
self.atest(before, expect, "CSS1", slimmer.css_slimmer)
def testCSS2(self):
before = CSS_2
expect = expect_CSS_2
self.atest(before, expect, "CSS2", slimmer.css_slimmer)
def testCSS3(self):
before = CSS_3
expect = expect_CSS_3
self.atest(before, expect, "CSS3", slimmer.css_slimmer)
def testCSS3b(self):
before = CSS_3b
expect = expect_CSS_3b
self.atest(before, expect, "CSS3b", slimmer.css_slimmer)
def testCSS3c(self):
before = CSS_3c
expect = expect_CSS_3c
self.atest(before, expect, "CSS3c", slimmer.css_slimmer)
def testCSS4(self):
before = CSS_4
expect = expect_CSS_4
self.atest(before, expect, "CSS4", slimmer.css_slimmer)
def testCSS5(self):
before = CSS_5
expect = expect_CSS_5
self.atest(before, expect, "CSS5", slimmer.css_slimmer)
def testCSS6(self):
before = CSS_6
expect = expect_CSS_6
self.atest(before, expect, "CSS6", slimmer.css_slimmer)
def testCSS7(self):
before = CSS_7
expect = expect_CSS_7
self.atest(before, expect, "CSS6", slimmer.css_slimmer)
def testHTML1(self):
before = HTML_1
expect = expect_HTML_1
self.atest(before, expect, "HTML1", slimmer.html_slimmer)
def testHTML2(self):
before = HTML_2
expect = expect_HTML_2
self.atest(before, expect, "HTML2", slimmer.html_slimmer)
def testHTML3(self):
before = HTML_3
expect = expect_HTML_3
self.atest(before, expect, "HTML3", slimmer.html_slimmer)
def testHTML4(self):
before = HTML_4
expect = expect_HTML_4
self.atest(before, expect, "HTML4", slimmer.html_slimmer)
def testHTML5(self):
before = HTML_5
expect = expect_HTML_5
self.atest(before, expect, "HTML5", slimmer.html_slimmer)
def testHTML6(self):
before = HTML_6
expect = expect_HTML_6
self.atest(before, expect, "HTML6", slimmer.html_slimmer)
def testHTML7(self):
before = HTML_7
expect = expect_HTML_7
self.atest(before, expect, "HTML7", slimmer.html_slimmer)
def testHTML8(self):
before = HTML_8
expect = expect_HTML_8
self.atest(before, expect, "HTML8", slimmer.html_slimmer)
def testHTML9(self):
""" UTF-8 characters in the HTML """
before = HTML_9
expect = expect_HTML_9
self.atest(before, expect, "HTML9", slimmer.html_slimmer)
def testJS1(self):
before = JS_1
expect = expect_JS_1
self.atest(before, expect, "JS1", slimmer.js_slimmer)
def testJS2(self):
before = JS_2
expect = expect_JS_2
self.atest(before, expect, "JS2", slimmer.js_slimmer)
def testJS3(self):
before = JS_3
expect = expect_JS_3
self.atest(before, expect, "JS3", slimmer.js_slimmer)
def testJS4(self):
before = JS_4
expect = expect_JS_4
self.atest(before, expect, "JS4", slimmer.js_slimmer)
def testJS5(self):
before = JS_5
expect = expect_JS_5
self.atest(before, expect, "JS5", slimmer.js_slimmer)
def testJS6(self):
before = JS_6
expect = expect_JS_6
self.atest(before, expect, "JS6", slimmer.js_slimmer)
def testJS8(self):
before = JS_8
expect = expect_JS_8
self.atest(before, expect, "JS8", slimmer.js_slimmer)
def testJS9(self):
before = JS_9
expect = expect_JS_9
self.atest(before, expect, "JS9", slimmer.js_slimmer)
def testJS10(self):
before = JS_10
expect = expect_JS_10
self.atest(before, expect, "JS10", slimmer.js_slimmer)
def testJS11a(self):
before = JS_11
expect = expect_JS_11
self.atest(before, expect, "JS11", slimmer.js_slimmer)
def testJS11b(self):
before = JS_11
expect = expect_JS_11_hardcore
self.atest(before, expect, "JS11", slimmer.js_slimmer,
hardcore=True)
def testJS12a(self):
before = JS_12
expect = expect_JS_12
self.atest(before, expect, "JS12", slimmer.js_slimmer)
def testJS12b(self):
before = JS_12
expect = expect_JS_12_hardcore
self.atest(before, expect, "JS12", slimmer.js_slimmer,
hardcore=True)
def testJS13(self):
before = JS_13
expect = expect_JS_13
self.atest(before, expect, "JS13", slimmer.js_slimmer)
def testJS14a(self):
before = JS_14
expect = expect_JS_14
self.atest(before, expect, "JS14a", slimmer.js_slimmer)
def testJS14b(self):
before = JS_14
expect = expect_JS_14_hardcore
self.atest(before, expect, "JS14b", slimmer.js_slimmer,
hardcore=True)
def testJS15(self):
before = JS_15
expect = expect_JS_15
self.atest(before, expect, "JS15", slimmer.js_slimmer)
def testJS16(self):
before = JS_16
expect = expect_JS_16
self.atest(before, expect, "JS16", slimmer.js_slimmer)
def testUnicodeHTML1(self):
here = os.path.dirname(__file__)
before = codecs.open(os.path.join(here, 'euc-jp.html'),
'r','euc-jp').read()
assert isinstance(before, unicode)
after = slimmer.html_slimmer(before)
assert isinstance(after, unicode)
def testUnicodeHTML2(self):
here = os.path.dirname(__file__)
before = codecs.open(os.path.join(here, 'utf-8.html'),
'r','utf-8').read()
assert isinstance(before, unicode)
after = slimmer.html_slimmer(before)
assert isinstance(after, unicode)
expect = u'<html><p>\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35\u0e04\u0e23\u0e31\u0e1a</p></html>'
assert after == expect
def suite():
return unittest.makeSuite(SlimmerTestCase)
if __name__ == '__main__':
if '-v' in sys.argv[1:] or '--verbose' in sys.argv[1:]:
VERBOSE = True
unittest.main()
|