145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Test wrapper from Quod Libet
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# http://www.sacredchao.net/quodlibet/
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport unittest, sys
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsuites = []
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgadd = registerCase = suites.append
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfrom unittest import TestCase
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgclass Mock(object):
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # A generic mocking object.
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def __init__(self, **kwargs): self.__dict__.update(kwargs)
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport test_intnum
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport test_symrec
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport test_bytecode
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgimport test_expr
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgclass Result(unittest.TestResult):
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    separator1 = '=' * 70
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    separator2 = '-' * 70
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def addSuccess(self, test):
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        unittest.TestResult.addSuccess(self, test)
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        sys.stdout.write('.')
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def addError(self, test, err):
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        unittest.TestResult.addError(self, test, err)
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        sys.stdout.write('E')
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def addFailure(self, test, err):
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        unittest.TestResult.addFailure(self, test, err)
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        sys.stdout.write('F')
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def printErrors(self):
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        succ = self.testsRun - (len(self.errors) + len(self.failures))
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        v = "%3d" % succ
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        count = 50 - self.testsRun
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        sys.stdout.write((" " * count) + v + "\n")
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        self.printErrorList('ERROR', self.errors)
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        self.printErrorList('FAIL', self.failures)
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def printErrorList(self, flavour, errors):
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        for test, err in errors:
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sys.stdout.write(self.separator1 + "\n")
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sys.stdout.write("%s: %s\n" % (flavour, str(test)))
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sys.stdout.write(self.separator2 + "\n")
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            sys.stdout.write("%s\n" % err)
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgclass Runner:
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    def run(self, test):
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        suite = unittest.makeSuite(test)
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        pref = '%s (%d): ' % (test.__name__, len(suite._tests))
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        print pref + " " * (25 - len(pref)),
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        result = Result()
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        suite(result)
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        result.printErrors()
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        return bool(result.failures + result.errors)
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef unit(run = []):
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    runner = Runner()
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    failures = False
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for test in suites:
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if not run or test.__name__ in run:
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            failures |= runner.run(test)
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return failures
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgif __name__ == "__main__":
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    raise SystemExit(unit(sys.argv[1:]))
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
70