Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 1 | # Simple implementation of a json test runner to run the test against json-py. |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 2 | from __future__ import print_function |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import os.path |
| 5 | import json |
| 6 | import types |
| 7 | |
| 8 | if len(sys.argv) != 2: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 9 | print("Usage: %s input-json-file", sys.argv[0]) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 10 | sys.exit(3) |
| 11 | |
| 12 | input_path = sys.argv[1] |
| 13 | base_path = os.path.splitext(input_path)[0] |
| 14 | actual_path = base_path + '.actual' |
| 15 | rewrite_path = base_path + '.rewrite' |
| 16 | rewrite_actual_path = base_path + '.actual-rewrite' |
| 17 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 18 | def valueTreeToString(fout, value, path = '.'): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 19 | ty = type(value) |
| 20 | if ty is types.DictType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 21 | fout.write('%s={}\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 22 | suffix = path[-1] != '.' and '.' or '' |
| 23 | names = value.keys() |
| 24 | names.sort() |
| 25 | for name in names: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 26 | valueTreeToString(fout, value[name], path + suffix + name) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 27 | elif ty is types.ListType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 28 | fout.write('%s=[]\n' % path) |
| 29 | for index, childValue in zip(xrange(0,len(value)), value): |
| 30 | valueTreeToString(fout, childValue, path + '[%d]' % index) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 31 | elif ty is types.StringType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 32 | fout.write('%s="%s"\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 33 | elif ty is types.IntType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 34 | fout.write('%s=%d\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 35 | elif ty is types.FloatType: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 36 | fout.write('%s=%.16g\n' % (path,value)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 37 | elif value is True: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 38 | fout.write('%s=true\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 39 | elif value is False: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 40 | fout.write('%s=false\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 41 | elif value is None: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 42 | fout.write('%s=null\n' % path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 43 | else: |
| 44 | assert False and "Unexpected value type" |
| 45 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 46 | def parseAndSaveValueTree(input, actual_path): |
| 47 | root = json.loads(input) |
| 48 | fout = file(actual_path, 'wt') |
| 49 | valueTreeToString(fout, root) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 50 | fout.close() |
| 51 | return root |
| 52 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 53 | def rewriteValueTree(value, rewrite_path): |
| 54 | rewrite = json.dumps(value) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 55 | #rewrite = rewrite[1:-1] # Somehow the string is quoted ! jsonpy bug ? |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 56 | file(rewrite_path, 'wt').write(rewrite + '\n') |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 57 | return rewrite |
| 58 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 59 | input = file(input_path, 'rt').read() |
| 60 | root = parseAndSaveValueTree(input, actual_path) |
| 61 | rewrite = rewriteValueTree(json.write(root), rewrite_path) |
| 62 | rewrite_root = parseAndSaveValueTree(rewrite, rewrite_actual_path) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 63 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 64 | sys.exit(0) |