blob: a11c3fc6848f136b0d11522497dd9ff10424f4b8 [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)
49except subprocess.CalledProcessError as e:
50 if e.returncode == 99:
51 print('Test "%s" could not be run.' % name)
52 exit(0)
53 else:
54 raise
Wenzel Jakobfab881c2015-10-18 17:04:24 +020055
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020056output = sanitize(output_bytes.decode('utf-8'))
57reference = sanitize(open(name + '.ref', 'r').read())
Wenzel Jakob38bd7112015-07-05 20:05:44 +020058
Jason Rhinelander7de9f6c2016-07-08 17:44:12 -040059if output == reference:
Wenzel Jakob38bd7112015-07-05 20:05:44 +020060 print('Test "%s" succeeded.' % name)
61 exit(0)
62else:
63 print('Test "%s" FAILED!' % name)
Dean Moldovan1fe59012016-06-01 23:16:13 +020064 print('--- output')
65 print('+++ reference')
Wenzel Jakobf57133a2016-07-06 05:43:52 +020066 print(''.join(difflib.ndiff(output.splitlines(True),
67 reference.splitlines(True))))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020068 exit(-1)