|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 import os, sys |
|
6 import glob |
|
7 import optparse |
|
8 import traceback |
|
9 import WebIDL |
|
10 |
|
11 class TestHarness(object): |
|
12 def __init__(self, test, verbose): |
|
13 self.test = test |
|
14 self.verbose = verbose |
|
15 self.printed_intro = False |
|
16 |
|
17 def start(self): |
|
18 if self.verbose: |
|
19 self.maybe_print_intro() |
|
20 |
|
21 def finish(self): |
|
22 if self.verbose or self.printed_intro: |
|
23 print "Finished test %s" % self.test |
|
24 |
|
25 def maybe_print_intro(self): |
|
26 if not self.printed_intro: |
|
27 print "Starting test %s" % self.test |
|
28 self.printed_intro = True |
|
29 |
|
30 def test_pass(self, msg): |
|
31 if self.verbose: |
|
32 print "TEST-PASS | %s" % msg |
|
33 |
|
34 def test_fail(self, msg): |
|
35 self.maybe_print_intro() |
|
36 print "TEST-UNEXPECTED-FAIL | %s" % msg |
|
37 |
|
38 def ok(self, condition, msg): |
|
39 if condition: |
|
40 self.test_pass(msg) |
|
41 else: |
|
42 self.test_fail(msg) |
|
43 |
|
44 def check(self, a, b, msg): |
|
45 if a == b: |
|
46 self.test_pass(msg) |
|
47 else: |
|
48 self.test_fail(msg) |
|
49 print "\tGot %s expected %s" % (a, b) |
|
50 |
|
51 def run_tests(tests, verbose): |
|
52 testdir = os.path.join(os.path.dirname(__file__), 'tests') |
|
53 if not tests: |
|
54 tests = glob.iglob(os.path.join(testdir, "*.py")) |
|
55 sys.path.append(testdir) |
|
56 |
|
57 for test in tests: |
|
58 (testpath, ext) = os.path.splitext(os.path.basename(test)) |
|
59 _test = __import__(testpath, globals(), locals(), ['WebIDLTest']) |
|
60 |
|
61 harness = TestHarness(test, verbose) |
|
62 harness.start() |
|
63 try: |
|
64 _test.WebIDLTest.__call__(WebIDL.Parser(), harness) |
|
65 except Exception, ex: |
|
66 print "TEST-UNEXPECTED-FAIL | Unhandled exception in test %s: %s" % (testpath, ex) |
|
67 traceback.print_exc() |
|
68 finally: |
|
69 harness.finish() |
|
70 |
|
71 if __name__ == '__main__': |
|
72 usage = """%prog [OPTIONS] [TESTS] |
|
73 Where TESTS are relative to the tests directory.""" |
|
74 parser = optparse.OptionParser(usage=usage) |
|
75 parser.add_option('-q', '--quiet', action='store_false', dest='verbose', default=True, |
|
76 help="Don't print passing tests.") |
|
77 options, tests = parser.parse_args() |
|
78 |
|
79 run_tests(tests, verbose=options.verbose) |