dom/bindings/parser/runtests.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bindings/parser/runtests.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +import os, sys
     1.9 +import glob
    1.10 +import optparse
    1.11 +import traceback
    1.12 +import WebIDL
    1.13 +
    1.14 +class TestHarness(object):
    1.15 +    def __init__(self, test, verbose):
    1.16 +        self.test = test
    1.17 +        self.verbose = verbose
    1.18 +        self.printed_intro = False
    1.19 +
    1.20 +    def start(self):
    1.21 +        if self.verbose:
    1.22 +            self.maybe_print_intro()
    1.23 +
    1.24 +    def finish(self):
    1.25 +        if self.verbose or self.printed_intro:
    1.26 +            print "Finished test %s" % self.test
    1.27 +
    1.28 +    def maybe_print_intro(self):
    1.29 +        if not self.printed_intro:
    1.30 +            print "Starting test %s" % self.test
    1.31 +            self.printed_intro = True
    1.32 +
    1.33 +    def test_pass(self, msg):
    1.34 +        if self.verbose:
    1.35 +            print "TEST-PASS | %s" % msg
    1.36 +
    1.37 +    def test_fail(self, msg):
    1.38 +        self.maybe_print_intro()
    1.39 +        print "TEST-UNEXPECTED-FAIL | %s" % msg
    1.40 +
    1.41 +    def ok(self, condition, msg):
    1.42 +        if condition:
    1.43 +            self.test_pass(msg)
    1.44 +        else:
    1.45 +            self.test_fail(msg)
    1.46 +
    1.47 +    def check(self, a, b, msg):
    1.48 +        if a == b:
    1.49 +            self.test_pass(msg)
    1.50 +        else:
    1.51 +            self.test_fail(msg)
    1.52 +            print "\tGot %s expected %s" % (a, b)
    1.53 +
    1.54 +def run_tests(tests, verbose):
    1.55 +    testdir = os.path.join(os.path.dirname(__file__), 'tests')
    1.56 +    if not tests:
    1.57 +        tests = glob.iglob(os.path.join(testdir, "*.py"))
    1.58 +    sys.path.append(testdir)
    1.59 +
    1.60 +    for test in tests:
    1.61 +        (testpath, ext) = os.path.splitext(os.path.basename(test))
    1.62 +        _test = __import__(testpath, globals(), locals(), ['WebIDLTest'])
    1.63 +
    1.64 +        harness = TestHarness(test, verbose)
    1.65 +        harness.start()
    1.66 +        try:
    1.67 +            _test.WebIDLTest.__call__(WebIDL.Parser(), harness)
    1.68 +        except Exception, ex:
    1.69 +            print "TEST-UNEXPECTED-FAIL | Unhandled exception in test %s: %s" % (testpath, ex)
    1.70 +            traceback.print_exc()
    1.71 +        finally:
    1.72 +            harness.finish()
    1.73 +
    1.74 +if __name__ == '__main__':
    1.75 +    usage = """%prog [OPTIONS] [TESTS]
    1.76 +               Where TESTS are relative to the tests directory."""
    1.77 +    parser = optparse.OptionParser(usage=usage)
    1.78 +    parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
    1.79 +                      help="Don't print passing tests.")
    1.80 +    options, tests = parser.parse_args()
    1.81 +
    1.82 +    run_tests(tests, verbose=options.verbose)

mercurial