michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import os, sys michael@0: import glob michael@0: import optparse michael@0: import traceback michael@0: import WebIDL michael@0: michael@0: class TestHarness(object): michael@0: def __init__(self, test, verbose): michael@0: self.test = test michael@0: self.verbose = verbose michael@0: self.printed_intro = False michael@0: michael@0: def start(self): michael@0: if self.verbose: michael@0: self.maybe_print_intro() michael@0: michael@0: def finish(self): michael@0: if self.verbose or self.printed_intro: michael@0: print "Finished test %s" % self.test michael@0: michael@0: def maybe_print_intro(self): michael@0: if not self.printed_intro: michael@0: print "Starting test %s" % self.test michael@0: self.printed_intro = True michael@0: michael@0: def test_pass(self, msg): michael@0: if self.verbose: michael@0: print "TEST-PASS | %s" % msg michael@0: michael@0: def test_fail(self, msg): michael@0: self.maybe_print_intro() michael@0: print "TEST-UNEXPECTED-FAIL | %s" % msg michael@0: michael@0: def ok(self, condition, msg): michael@0: if condition: michael@0: self.test_pass(msg) michael@0: else: michael@0: self.test_fail(msg) michael@0: michael@0: def check(self, a, b, msg): michael@0: if a == b: michael@0: self.test_pass(msg) michael@0: else: michael@0: self.test_fail(msg) michael@0: print "\tGot %s expected %s" % (a, b) michael@0: michael@0: def run_tests(tests, verbose): michael@0: testdir = os.path.join(os.path.dirname(__file__), 'tests') michael@0: if not tests: michael@0: tests = glob.iglob(os.path.join(testdir, "*.py")) michael@0: sys.path.append(testdir) michael@0: michael@0: for test in tests: michael@0: (testpath, ext) = os.path.splitext(os.path.basename(test)) michael@0: _test = __import__(testpath, globals(), locals(), ['WebIDLTest']) michael@0: michael@0: harness = TestHarness(test, verbose) michael@0: harness.start() michael@0: try: michael@0: _test.WebIDLTest.__call__(WebIDL.Parser(), harness) michael@0: except Exception, ex: michael@0: print "TEST-UNEXPECTED-FAIL | Unhandled exception in test %s: %s" % (testpath, ex) michael@0: traceback.print_exc() michael@0: finally: michael@0: harness.finish() michael@0: michael@0: if __name__ == '__main__': michael@0: usage = """%prog [OPTIONS] [TESTS] michael@0: Where TESTS are relative to the tests directory.""" michael@0: parser = optparse.OptionParser(usage=usage) michael@0: parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True, michael@0: help="Don't print passing tests.") michael@0: options, tests = parser.parse_args() michael@0: michael@0: run_tests(tests, verbose=options.verbose)