blob: 2785c8eaae03f128d58873993aea8677df55b50b [file] [log] [blame]
Wenzel Jakob6d6fd092015-10-01 17:34:26 +02001import sys
2import os
3import re
4import subprocess
Dean Moldovan1fe59012016-06-01 23:16:13 +02005import difflib
Wenzel Jakob6d6fd092015-10-01 17:34:26 +02006
7remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
8remove_long_marker = re.compile(r'([0-9])L')
Wenzel Jakob607654f2015-10-13 23:58:10 +02009remove_hex = re.compile(r'0x[0-9a-fA-F]+')
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020010shorten_floats = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')
11
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020012def sanitize(lines):
13 lines = lines.split('\n')
14 for i in range(len(lines)):
15 line = lines[i]
16 if line.startswith(" |"):
17 line = ""
Jason Rhinelander3f589372016-08-07 13:05:26 -040018 if line.startswith("### "):
19 # Constructor/destructor output. Useful for example, but unreliable across compilers;
20 # testing of proper construction/destruction occurs with ConstructorStats mechanism instead
21 line = ""
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020022 line = remove_unicode_marker.sub(r'\1', line)
23 line = remove_long_marker.sub(r'\1', line)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020024 line = remove_hex.sub(r'0', line)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020025 line = shorten_floats.sub(r'\1', line)
26 line = line.replace('__builtin__', 'builtins')
27 line = line.replace('example.', '')
Wenzel Jakob27e8e102016-01-17 22:36:37 +010028 line = line.replace('unicode', 'str')
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040029 line = line.replace('ExampleWithEnum.EMode', 'EMode')
Wenzel Jakob678d7872016-01-17 22:36:41 +010030 line = line.replace('example.EMode', 'EMode')
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020031 line = line.replace('method of builtins.PyCapsule instance', '')
32 line = line.strip()
33 lines[i] = line
34
Dean Moldovan1fe59012016-06-01 23:16:13 +020035 return '\n'.join(sorted([l for l in lines if l != ""]))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020036
37path = os.path.dirname(__file__)
38if path != '':
39 os.chdir(path)
40
Wenzel Jakob80c24512016-02-20 20:53:10 +010041if len(sys.argv) < 2:
Jason Rhinelander3f589372016-08-07 13:05:26 -040042 print("Syntax: %s <test name>" % sys.argv[0])
Wenzel Jakob80c24512016-02-20 20:53:10 +010043 exit(0)
44
Wenzel Jakob38bd7112015-07-05 20:05:44 +020045name = sys.argv[1]
Jason Rhinelander7de9f6c2016-07-08 17:44:12 -040046try:
Jason Rhinelander3f589372016-08-07 13:05:26 -040047 output_bytes = subprocess.check_output([sys.executable, "-u", name + ".py"],
Jason Rhinelander7de9f6c2016-07-08 17:44:12 -040048 stderr=subprocess.STDOUT)
Ivan Smirnov8b5fc8b2016-06-19 14:40:04 +010049except subprocess.CalledProcessError as exc:
50 print('Test `{}` failed:\n{}\n'.format(name, '-' * 50))
51 print(exc.output.decode())
52 print('-' * 50)
53 sys.exit(1)
Wenzel Jakobfab881c2015-10-18 17:04:24 +020054
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020055output = sanitize(output_bytes.decode('utf-8'))
56reference = sanitize(open(name + '.ref', 'r').read())
Wenzel Jakob38bd7112015-07-05 20:05:44 +020057
Jason Rhinelander7de9f6c2016-07-08 17:44:12 -040058if output == reference:
Wenzel Jakob38bd7112015-07-05 20:05:44 +020059 print('Test "%s" succeeded.' % name)
60 exit(0)
61else:
62 print('Test "%s" FAILED!' % name)
Dean Moldovan1fe59012016-06-01 23:16:13 +020063 print('--- output')
64 print('+++ reference')
Wenzel Jakobf57133a2016-07-06 05:43:52 +020065 print(''.join(difflib.ndiff(output.splitlines(True),
66 reference.splitlines(True))))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020067 exit(-1)