Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | # Copyright 2007 Baptiste Lepilleur and The JsonCpp Authors |
Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 2 | # 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 Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 6 | from __future__ import print_function |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 7 | from __future__ import unicode_literals |
| 8 | from io import open |
| 9 | from glob import glob |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 10 | import sys |
| 11 | import os |
Christopher Dunn | 4ca9d25 | 2015-01-09 22:28:20 -0600 | [diff] [blame] | 12 | import os.path |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 13 | import optparse |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 14 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 15 | VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes ' |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 16 | |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 17 | def 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 Dunn | ac6bbbc | 2015-01-20 11:36:05 -0600 | [diff] [blame] | 22 | print(cmd, file=sys.stderr) |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 23 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 34 | def compareOutputs(expected, actual, message): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 35 | expected = expected.strip().replace('\r','').split('\n') |
| 36 | actual = actual.strip().replace('\r','').split('\n') |
| 37 | diff_line = 0 |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 38 | max_line_to_compare = min(len(expected), len(actual)) |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 39 | for index in range(0,max_line_to_compare): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 40 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 47 | def safeGetLine(lines, index): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 48 | 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 57 | safeGetLine(actual,diff_line)) |
Hans Johnson | a3c8e86 | 2019-01-12 12:32:15 -0600 | [diff] [blame] | 58 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 59 | def safeReadFile(path): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 60 | try: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 61 | return open(path, 'rt', encoding = 'utf-8').read() |
Christopher Dunn | 9aa6144 | 2014-11-19 23:10:02 -0600 | [diff] [blame] | 62 | except IOError as e: |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 63 | return '<File "%s" is missing: %s>' % (path,e) |
| 64 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 65 | def runAllTests(jsontest_executable_path, input_dir = None, |
Christopher Dunn | 70704b9 | 2015-01-23 12:04:14 -0600 | [diff] [blame] | 66 | use_valgrind=False, with_json_checker=False, |
| 67 | writerClass='StyledWriter'): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 68 | if not input_dir: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 69 | input_dir = os.path.join(os.getcwd(), 'data') |
| 70 | tests = glob(os.path.join(input_dir, '*.json')) |
Baptiste Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 71 | if with_json_checker: |
Hans Johnson | a3c8e86 | 2019-01-12 12:32:15 -0600 | [diff] [blame] | 72 | all_test_jsonchecker = glob(os.path.join(input_dir, '../jsonchecker', '*.json')) |
| 73 | # These tests fail with strict json support, but pass with jsoncpp extra lieniency |
| 74 | """ |
| 75 | Failure details: |
| 76 | * Test ../jsonchecker/fail25.json |
| 77 | Parsing should have failed: |
| 78 | [" tab character in string "] |
| 79 | |
| 80 | * Test ../jsonchecker/fail13.json |
| 81 | Parsing should have failed: |
| 82 | {"Numbers cannot have leading zeroes": 013} |
| 83 | |
| 84 | * Test ../jsonchecker/fail18.json |
| 85 | Parsing should have failed: |
| 86 | [[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] |
| 87 | |
| 88 | * Test ../jsonchecker/fail8.json |
| 89 | Parsing should have failed: |
| 90 | ["Extra close"]] |
| 91 | |
| 92 | * Test ../jsonchecker/fail7.json |
| 93 | Parsing should have failed: |
| 94 | ["Comma after the close"], |
| 95 | |
| 96 | * Test ../jsonchecker/fail10.json |
| 97 | Parsing should have failed: |
| 98 | {"Extra value after close": true} "misplaced quoted value" |
| 99 | |
| 100 | * Test ../jsonchecker/fail27.json |
| 101 | Parsing should have failed: |
| 102 | ["line |
| 103 | break"] |
| 104 | """ |
| 105 | known_differences_withjsonchecker = [ "fail25.json", "fail13.json", "fail18.json", "fail8.json", |
| 106 | "fail7.json", "fail10.json", "fail27.json" ] |
| 107 | test_jsonchecker = [ test for test in all_test_jsonchecker if os.path.basename(test) not in known_differences_withjsonchecker ] |
| 108 | |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 109 | else: |
| 110 | test_jsonchecker = [] |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 111 | failed_tests = [] |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 112 | valgrind_path = use_valgrind and VALGRIND_CMD or '' |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 113 | for input_path in tests + test_jsonchecker: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 114 | expect_failure = os.path.basename(input_path).startswith('fail') |
Baptiste Lepilleur | 9c98f22 | 2011-05-01 15:40:47 +0000 | [diff] [blame] | 115 | is_json_checker_test = (input_path in test_jsonchecker) or expect_failure |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 116 | print('TESTING:', input_path, end=' ') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 117 | options = is_json_checker_test and '--json-checker' or '' |
Christopher Dunn | 70704b9 | 2015-01-23 12:04:14 -0600 | [diff] [blame] | 118 | options += ' --json-writer %s'%writerClass |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 119 | cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options, |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 120 | input_path) |
| 121 | status, process_output = getStatusOutput(cmd) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 122 | if is_json_checker_test: |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 123 | if expect_failure: |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 124 | if not status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 125 | print('FAILED') |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 126 | failed_tests.append((input_path, 'Parsing should have failed:\n%s' % |
| 127 | safeReadFile(input_path))) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 128 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 129 | print('OK') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 130 | else: |
Christopher Dunn | cd140b5 | 2015-01-16 13:44:27 -0600 | [diff] [blame] | 131 | if status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 132 | print('FAILED') |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 133 | failed_tests.append((input_path, 'Parsing failed:\n' + process_output)) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 134 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 135 | print('OK') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 136 | else: |
| 137 | base_path = os.path.splitext(input_path)[0] |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 138 | actual_output = safeReadFile(base_path + '.actual') |
| 139 | actual_rewrite_output = safeReadFile(base_path + '.actual-rewrite') |
| 140 | open(base_path + '.process-output', 'wt', encoding = 'utf-8').write(process_output) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 141 | if status: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 142 | print('parsing failed') |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 143 | failed_tests.append((input_path, 'Parsing failed:\n' + process_output)) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 144 | else: |
| 145 | expected_output_path = os.path.splitext(input_path)[0] + '.expected' |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 146 | expected_output = open(expected_output_path, 'rt', encoding = 'utf-8').read() |
| 147 | detail = (compareOutputs(expected_output, actual_output, 'input') |
| 148 | or compareOutputs(expected_output, actual_rewrite_output, 'rewrite')) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 149 | if detail: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 150 | print('FAILED') |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 151 | failed_tests.append((input_path, detail)) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 152 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 153 | print('OK') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 154 | |
| 155 | if failed_tests: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 156 | print() |
| 157 | print('Failure details:') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 158 | for failed_test in failed_tests: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 159 | print('* Test', failed_test[0]) |
| 160 | print(failed_test[1]) |
| 161 | print() |
| 162 | print('Test results: %d passed, %d failed.' % (len(tests)-len(failed_tests), |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 163 | len(failed_tests))) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 164 | return 1 |
| 165 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 166 | print('All %d tests passed.' % len(tests)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 167 | return 0 |
| 168 | |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 169 | def main(): |
| 170 | from optparse import OptionParser |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 171 | parser = OptionParser(usage="%prog [options] <path to jsontestrunner.exe> [test case directory]") |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 172 | parser.add_option("--valgrind", |
| 173 | action="store_true", dest="valgrind", default=False, |
| 174 | help="run all the tests using valgrind to detect memory leaks") |
Baptiste Lepilleur | 7c66ac2 | 2010-02-21 14:26:08 +0000 | [diff] [blame] | 175 | parser.add_option("-c", "--with-json-checker", |
| 176 | action="store_true", dest="with_json_checker", default=False, |
| 177 | help="run all the tests from the official JSONChecker test suite of json.org") |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 178 | parser.enable_interspersed_args() |
| 179 | options, args = parser.parse_args() |
| 180 | |
| 181 | if len(args) < 1 or len(args) > 2: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 182 | parser.error('Must provides at least path to jsontestrunner executable.') |
| 183 | sys.exit(1) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 184 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 185 | jsontest_executable_path = os.path.normpath(os.path.abspath(args[0])) |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 186 | if len(args) > 1: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 187 | input_path = os.path.normpath(os.path.abspath(args[1])) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 188 | else: |
| 189 | input_path = None |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 190 | status = runAllTests(jsontest_executable_path, input_path, |
Christopher Dunn | 9e4bcf3 | 2015-01-23 14:39:57 -0600 | [diff] [blame] | 191 | use_valgrind=options.valgrind, |
| 192 | with_json_checker=options.with_json_checker, |
| 193 | writerClass='StyledWriter') |
Christopher Dunn | 70704b9 | 2015-01-23 12:04:14 -0600 | [diff] [blame] | 194 | if status: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 195 | sys.exit(status) |
| 196 | status = runAllTests(jsontest_executable_path, input_path, |
Christopher Dunn | 9e4bcf3 | 2015-01-23 14:39:57 -0600 | [diff] [blame] | 197 | use_valgrind=options.valgrind, |
| 198 | with_json_checker=options.with_json_checker, |
| 199 | writerClass='StyledStreamWriter') |
| 200 | if status: |
| 201 | sys.exit(status) |
| 202 | status = runAllTests(jsontest_executable_path, input_path, |
| 203 | use_valgrind=options.valgrind, |
| 204 | with_json_checker=options.with_json_checker, |
| 205 | writerClass='BuiltStyledStreamWriter') |
| 206 | if status: |
| 207 | sys.exit(status) |
Baptiste Lepilleur | 932cfc7 | 2009-11-19 20:16:59 +0000 | [diff] [blame] | 208 | |
| 209 | if __name__ == '__main__': |
| 210 | main() |