blob: 26caf0cfb42aa63b0f098aec0abbe03b832c359b [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001# Copyright 2007 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-09 18:01:33 -07002# Distributed under MIT license, or public domain if desired and
3# recognized in your jurisdiction.
4# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Christopher Dunnbd1e8952014-11-19 23:30:47 -06006from __future__ import print_function
Christopher Dunncd140b52015-01-16 13:44:27 -06007from __future__ import unicode_literals
8from io import open
9from glob import glob
Christopher Dunnf9864232007-06-14 21:01:26 +000010import sys
11import os
Christopher Dunn4ca9d252015-01-09 22:28:20 -060012import os.path
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000013import optparse
Christopher Dunnf9864232007-06-14 21:01:26 +000014
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +000015VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes '
Christopher Dunnf9864232007-06-14 21:01:26 +000016
Christopher Dunncd140b52015-01-16 13:44:27 -060017def getStatusOutput(cmd):
18 """
19 Return int, unicode (for both Python 2 and 3).
20 Note: os.popen().close() would return None for 0.
21 """
Christopher Dunnac6bbbc2015-01-20 11:36:05 -060022 print(cmd, file=sys.stderr)
Christopher Dunncd140b52015-01-16 13:44:27 -060023 pipe = os.popen(cmd)
24 process_output = pipe.read()
25 try:
26 # We have been using os.popen(). When we read() the result
27 # we get 'str' (bytes) in py2, and 'str' (unicode) in py3.
28 # Ugh! There must be a better way to handle this.
29 process_output = process_output.decode('utf-8')
30 except AttributeError:
31 pass # python3
32 status = pipe.close()
33 return status, process_output
Christopher Dunn494950a2015-01-24 15:29:52 -060034def compareOutputs(expected, actual, message):
Christopher Dunnf9864232007-06-14 21:01:26 +000035 expected = expected.strip().replace('\r','').split('\n')
36 actual = actual.strip().replace('\r','').split('\n')
37 diff_line = 0
Christopher Dunn494950a2015-01-24 15:29:52 -060038 max_line_to_compare = min(len(expected), len(actual))
Christopher Dunnbd1e8952014-11-19 23:30:47 -060039 for index in range(0,max_line_to_compare):
Christopher Dunnf9864232007-06-14 21:01:26 +000040 if expected[index].strip() != actual[index].strip():
41 diff_line = index + 1
42 break
43 if diff_line == 0 and len(expected) != len(actual):
44 diff_line = max_line_to_compare+1
45 if diff_line == 0:
46 return None
Christopher Dunn494950a2015-01-24 15:29:52 -060047 def safeGetLine(lines, index):
Christopher Dunnf9864232007-06-14 21:01:26 +000048 index += -1
49 if index >= len(lines):
50 return ''
51 return lines[index].strip()
52 return """ Difference in %s at line %d:
53 Expected: '%s'
54 Actual: '%s'
55""" % (message, diff_line,
56 safeGetLine(expected,diff_line),
Christopher Dunn494950a2015-01-24 15:29:52 -060057 safeGetLine(actual,diff_line))
Hans Johnsona3c8e862019-01-12 12:32:15 -060058
Christopher Dunn494950a2015-01-24 15:29:52 -060059def safeReadFile(path):
Christopher Dunnf9864232007-06-14 21:01:26 +000060 try:
Christopher Dunn494950a2015-01-24 15:29:52 -060061 return open(path, 'rt', encoding = 'utf-8').read()
Christopher Dunn9aa61442014-11-19 23:10:02 -060062 except IOError as e:
Christopher Dunnf9864232007-06-14 21:01:26 +000063 return '<File "%s" is missing: %s>' % (path,e)
64
Christopher Dunn411d88f2020-04-24 01:45:19 -050065class FailError(Exception):
66 def __init__(self, msg):
67 super(Exception, self).__init__(msg)
68
Christopher Dunn494950a2015-01-24 15:29:52 -060069def runAllTests(jsontest_executable_path, input_dir = None,
Christopher Dunn70704b92015-01-23 12:04:14 -060070 use_valgrind=False, with_json_checker=False,
71 writerClass='StyledWriter'):
Christopher Dunnf9864232007-06-14 21:01:26 +000072 if not input_dir:
Christopher Dunn494950a2015-01-24 15:29:52 -060073 input_dir = os.path.join(os.getcwd(), 'data')
74 tests = glob(os.path.join(input_dir, '*.json'))
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +000075 if with_json_checker:
Hans Johnsona3c8e862019-01-12 12:32:15 -060076 all_test_jsonchecker = glob(os.path.join(input_dir, '../jsonchecker', '*.json'))
77 # These tests fail with strict json support, but pass with jsoncpp extra lieniency
78 """
79 Failure details:
80 * Test ../jsonchecker/fail25.json
81 Parsing should have failed:
82 [" tab character in string "]
83
84 * Test ../jsonchecker/fail13.json
85 Parsing should have failed:
86 {"Numbers cannot have leading zeroes": 013}
87
88 * Test ../jsonchecker/fail18.json
89 Parsing should have failed:
90 [[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
91
92 * Test ../jsonchecker/fail8.json
93 Parsing should have failed:
94 ["Extra close"]]
95
96 * Test ../jsonchecker/fail7.json
97 Parsing should have failed:
98 ["Comma after the close"],
99
100 * Test ../jsonchecker/fail10.json
101 Parsing should have failed:
102 {"Extra value after close": true} "misplaced quoted value"
103
104 * Test ../jsonchecker/fail27.json
105 Parsing should have failed:
106 ["line
107 break"]
108 """
109 known_differences_withjsonchecker = [ "fail25.json", "fail13.json", "fail18.json", "fail8.json",
110 "fail7.json", "fail10.json", "fail27.json" ]
111 test_jsonchecker = [ test for test in all_test_jsonchecker if os.path.basename(test) not in known_differences_withjsonchecker ]
112
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000113 else:
114 test_jsonchecker = []
Christopher Dunnf9864232007-06-14 21:01:26 +0000115 failed_tests = []
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000116 valgrind_path = use_valgrind and VALGRIND_CMD or ''
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000117 for input_path in tests + test_jsonchecker:
Christopher Dunn494950a2015-01-24 15:29:52 -0600118 expect_failure = os.path.basename(input_path).startswith('fail')
Baptiste Lepilleur9c98f222011-05-01 15:40:47 +0000119 is_json_checker_test = (input_path in test_jsonchecker) or expect_failure
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600120 print('TESTING:', input_path, end=' ')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000121 options = is_json_checker_test and '--json-checker' or ''
Christopher Dunn70704b92015-01-23 12:04:14 -0600122 options += ' --json-writer %s'%writerClass
Christopher Dunn494950a2015-01-24 15:29:52 -0600123 cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options,
Christopher Dunncd140b52015-01-16 13:44:27 -0600124 input_path)
125 status, process_output = getStatusOutput(cmd)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000126 if is_json_checker_test:
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000127 if expect_failure:
Christopher Dunncd140b52015-01-16 13:44:27 -0600128 if not status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600129 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -0600130 failed_tests.append((input_path, 'Parsing should have failed:\n%s' %
131 safeReadFile(input_path)))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000132 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600133 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +0000134 else:
Christopher Dunncd140b52015-01-16 13:44:27 -0600135 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600136 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -0600137 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000138 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600139 print('OK')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000140 else:
141 base_path = os.path.splitext(input_path)[0]
Christopher Dunn494950a2015-01-24 15:29:52 -0600142 actual_output = safeReadFile(base_path + '.actual')
143 actual_rewrite_output = safeReadFile(base_path + '.actual-rewrite')
144 open(base_path + '.process-output', 'wt', encoding = 'utf-8').write(process_output)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000145 if status:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600146 print('parsing failed')
Christopher Dunn494950a2015-01-24 15:29:52 -0600147 failed_tests.append((input_path, 'Parsing failed:\n' + process_output))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000148 else:
149 expected_output_path = os.path.splitext(input_path)[0] + '.expected'
Christopher Dunn494950a2015-01-24 15:29:52 -0600150 expected_output = open(expected_output_path, 'rt', encoding = 'utf-8').read()
151 detail = (compareOutputs(expected_output, actual_output, 'input')
152 or compareOutputs(expected_output, actual_rewrite_output, 'rewrite'))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000153 if detail:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600154 print('FAILED')
Christopher Dunn494950a2015-01-24 15:29:52 -0600155 failed_tests.append((input_path, detail))
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000156 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600157 print('OK')
Christopher Dunnf9864232007-06-14 21:01:26 +0000158
159 if failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600160 print()
161 print('Failure details:')
Christopher Dunnf9864232007-06-14 21:01:26 +0000162 for failed_test in failed_tests:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600163 print('* Test', failed_test[0])
164 print(failed_test[1])
165 print()
166 print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests),
Christopher Dunn494950a2015-01-24 15:29:52 -0600167 len(failed_tests)))
Christopher Dunn411d88f2020-04-24 01:45:19 -0500168 raise FailError(repr(failed_tests))
Christopher Dunnf9864232007-06-14 21:01:26 +0000169 else:
Christopher Dunnbd1e8952014-11-19 23:30:47 -0600170 print('All %d tests passed.' % len(tests))
Christopher Dunnf9864232007-06-14 21:01:26 +0000171
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000172def main():
173 from optparse import OptionParser
Christopher Dunn494950a2015-01-24 15:29:52 -0600174 parser = OptionParser(usage="%prog [options] <path to jsontestrunner.exe> [test case directory]")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000175 parser.add_option("--valgrind",
176 action="store_true", dest="valgrind", default=False,
177 help="run all the tests using valgrind to detect memory leaks")
Baptiste Lepilleur7c66ac22010-02-21 14:26:08 +0000178 parser.add_option("-c", "--with-json-checker",
179 action="store_true", dest="with_json_checker", default=False,
180 help="run all the tests from the official JSONChecker test suite of json.org")
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000181 parser.enable_interspersed_args()
182 options, args = parser.parse_args()
183
184 if len(args) < 1 or len(args) > 2:
Christopher Dunn494950a2015-01-24 15:29:52 -0600185 parser.error('Must provides at least path to jsontestrunner executable.')
186 sys.exit(1)
Christopher Dunnf9864232007-06-14 21:01:26 +0000187
Christopher Dunn494950a2015-01-24 15:29:52 -0600188 jsontest_executable_path = os.path.normpath(os.path.abspath(args[0]))
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000189 if len(args) > 1:
Christopher Dunn494950a2015-01-24 15:29:52 -0600190 input_path = os.path.normpath(os.path.abspath(args[1]))
Christopher Dunnf9864232007-06-14 21:01:26 +0000191 else:
192 input_path = None
Christopher Dunn411d88f2020-04-24 01:45:19 -0500193 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 14:39:57 -0600194 use_valgrind=options.valgrind,
195 with_json_checker=options.with_json_checker,
196 writerClass='StyledWriter')
Christopher Dunn411d88f2020-04-24 01:45:19 -0500197 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 14:39:57 -0600198 use_valgrind=options.valgrind,
199 with_json_checker=options.with_json_checker,
200 writerClass='StyledStreamWriter')
Christopher Dunn411d88f2020-04-24 01:45:19 -0500201 runAllTests(jsontest_executable_path, input_path,
Christopher Dunn9e4bcf32015-01-23 14:39:57 -0600202 use_valgrind=options.valgrind,
203 with_json_checker=options.with_json_checker,
204 writerClass='BuiltStyledStreamWriter')
Baptiste Lepilleur932cfc72009-11-19 20:16:59 +0000205
206if __name__ == '__main__':
Christopher Dunn411d88f2020-04-24 01:45:19 -0500207 try:
208 main()
209 except FailError:
210 sys.exit(1)