OLD | NEW |
1 # This Source Code is subject to the terms of the Mozilla Public License | 1 # This Source Code is subject to the terms of the Mozilla Public License |
2 # version 2.0 (the "License"). You can obtain a copy of the License at | 2 # version 2.0 (the "License"). You can obtain a copy of the License at |
3 # http://mozilla.org/MPL/2.0/. | 3 # http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 from __future__ import print_function | 5 from __future__ import print_function |
6 | 6 |
7 import sys | 7 import sys |
8 import os | 8 import os |
9 import re | 9 import re |
10 import difflib | 10 import difflib |
11 | 11 |
12 import abp_rewrite | 12 import abp_rewrite |
13 | 13 |
14 | 14 |
15 def run_tests(): | 15 def run_tests(): |
16 testDir = os.path.join(os.path.dirname(__file__), 'autotest') | 16 test_dir = os.path.join(os.path.dirname(__file__), 'autotest') |
17 succeed = True | 17 succeed = True |
18 | 18 |
19 for file in os.listdir(testDir): | 19 for filename in os.listdir(test_dir): |
20 if not re.search(r'^test_.*\.js$', file): | 20 if not re.search(r'^test_.*\.js$', filename): |
21 continue | 21 continue |
22 | 22 |
23 file = os.path.join(testDir, file) | 23 filename = os.path.join(test_dir, filename) |
24 handle = open(file, 'r') | |
25 name = None | 24 name = None |
26 arguments = None | 25 args = None |
27 for line in handle: | |
28 match = re.search(r'^//\s*([A-Za-z]+):\s*(.*?)\s*$', line) | |
29 if match and match.group(1).lower() == 'name': | |
30 name = match.group(2) | |
31 elif match and match.group(1).lower() == 'arguments': | |
32 arguments = match.group(2).split(' ') | |
33 handle.close() | |
34 | 26 |
35 if arguments == None: | 27 with open(filename, 'r') as file: |
| 28 for line in file: |
| 29 match = re.search(r'^//\s*([A-Za-z]+):\s*(.*?)\s*$', line) |
| 30 if match: |
| 31 key = match.group(1).lower() |
| 32 if key == 'name': |
| 33 name = match.group(2) |
| 34 elif key == 'args': |
| 35 args = match.group(2).split() |
| 36 |
| 37 if args is None: |
36 continue | 38 continue |
37 | 39 |
38 output = abp_rewrite.rewrite_js(arguments, file) | 40 output = abp_rewrite.rewrite_js(args, filename) |
39 expected = open(file + '.expected', 'rU').read() | 41 with open(filename + '.expected', 'rU') as file: |
| 42 expected = file.read() |
| 43 |
40 if output == expected: | 44 if output == expected: |
41 print(name + ' passed') | 45 print(name + ' passed') |
42 else: | 46 else: |
43 succeed = False | 47 succeed = False |
44 print(name + ' failed! Log:') | 48 print(name + ' failed! Log:') |
45 for line in difflib.unified_diff(expected.splitlines(), | 49 for line in difflib.unified_diff(expected.splitlines(), |
46 output.splitlines(), | 50 output.splitlines(), |
47 fromfile=file + '.expected', | 51 fromfile=filename + '.expected', |
48 tofile=file + '.output'): | 52 tofile=filename + '.output'): |
49 print(line) | 53 print(line) |
50 print() | 54 print() |
51 | 55 |
52 return succeed | 56 return succeed |
53 | 57 |
54 if __name__ == '__main__': | 58 if __name__ == '__main__': |
55 sys.exit(not run_tests()) | 59 sys.exit(not run_tests()) |
OLD | NEW |