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
|
# -*- coding: utf-8 -*-
"""test utils for tests of examples
A module to test must have:
- main(): a method
- EXPOUT: string which contains expected output
- EXPERR: string which contains expected error output
"""
__all__ = ['TestUtil']
import logging
import os
import StringIO
import sys
import cssutils
PY2x = sys.version_info < (3,0)
class OutReplacement(object):
"io.StringIO does not work somehow?!"
def __init__(self):
self.t = ''
def write(self, t):
if not PY2x:
# py3 hack
if t.startswith('b') and t.endswith("'"):
t = t[2:-1]
self.t += t
def getvalue(self):
if not PY2x:
# py3 hack
self.t = self.t.replace('\\n', '\n')
self.t = self.t.replace('\\\\', '\\')
return self.t
class TestUtil(object):
def __init__(self):
"init out and err to catch both"
self._out = OutReplacement()#StringIO()
sys.stdout = self._out
self._err = StringIO.StringIO()
hdlr = logging.StreamHandler(self._err)
cssutils.log._log.addHandler(hdlr)
def end(self, expo, expe):
"return out, err"
sys.stdout = sys.__stdout__
out = self._out.getvalue()
err = self._err.getvalue()
ok = (out == expo and err == expe)
if not ok:
print
if out != expo:
print '### out:\n%r\n### != expout:\n%r\n' % (out, expo)
else:
print '### err:\n%r\n### != experr:\n%r\n' % (err, expe)
return ok
modules = 0
errors = 0
def mod(module):
global modules, errors
modules += 1
t = TestUtil()
module.main()
ok = t.end(module.EXPOUT, module.EXPERR)
if not ok:
errors += 1
print '---', ok, ':', os.path.basename(module.__file__)
print
def main():
if PY2x:
print "DOCTESTS:::::::::::::"
# doctests
import website
import doctest
doctest.testmod(website)
print
print 80*'-'
print
print
global modules, errors
import build
mod(build)
import customlog
mod(customlog)
import imports
mod(imports)
import parse
mod(parse)
import selectors_tolower
mod(selectors_tolower)
import cssencodings
mod(cssencodings)
import minify
mod(minify)
print
print 80*'-'
print 'Ran %i tests (%i errors).' % (modules, errors)
if PY2x:
print '**Check doctest results above!**'
else:
print 'doctests do not work in Python 3 (yet?)!'
if __name__ == '__main__':
main()
|