blob: afaabd9d06e29f3abf3d9f6b50455711a9daebaf [file] [log] [blame]
Christopher Dunnf9864232007-06-14 21:01:26 +00001# Simple implementation of a json test runner to run the test against json-py.
Christopher Dunnbd1e8952014-11-19 23:30:47 -06002from __future__ import print_function
Christopher Dunnf9864232007-06-14 21:01:26 +00003import sys
4import os.path
5import json
6import types
7
8if len(sys.argv) != 2:
Christopher Dunnbd1e8952014-11-19 23:30:47 -06009 print("Usage: %s input-json-file", sys.argv[0])
Christopher Dunnf9864232007-06-14 21:01:26 +000010 sys.exit(3)
11
12input_path = sys.argv[1]
13base_path = os.path.splitext(input_path)[0]
14actual_path = base_path + '.actual'
15rewrite_path = base_path + '.rewrite'
16rewrite_actual_path = base_path + '.actual-rewrite'
17
Christopher Dunn494950a2015-01-24 15:29:52 -060018def valueTreeToString(fout, value, path = '.'):
Christopher Dunnf9864232007-06-14 21:01:26 +000019 ty = type(value)
20 if ty is types.DictType:
Christopher Dunn494950a2015-01-24 15:29:52 -060021 fout.write('%s={}\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000022 suffix = path[-1] != '.' and '.' or ''
23 names = value.keys()
24 names.sort()
25 for name in names:
Christopher Dunn494950a2015-01-24 15:29:52 -060026 valueTreeToString(fout, value[name], path + suffix + name)
Christopher Dunnf9864232007-06-14 21:01:26 +000027 elif ty is types.ListType:
Christopher Dunn494950a2015-01-24 15:29:52 -060028 fout.write('%s=[]\n' % path)
29 for index, childValue in zip(xrange(0,len(value)), value):
30 valueTreeToString(fout, childValue, path + '[%d]' % index)
Christopher Dunnf9864232007-06-14 21:01:26 +000031 elif ty is types.StringType:
Christopher Dunn494950a2015-01-24 15:29:52 -060032 fout.write('%s="%s"\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000033 elif ty is types.IntType:
Christopher Dunn494950a2015-01-24 15:29:52 -060034 fout.write('%s=%d\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000035 elif ty is types.FloatType:
Christopher Dunn494950a2015-01-24 15:29:52 -060036 fout.write('%s=%.16g\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000037 elif value is True:
Christopher Dunn494950a2015-01-24 15:29:52 -060038 fout.write('%s=true\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000039 elif value is False:
Christopher Dunn494950a2015-01-24 15:29:52 -060040 fout.write('%s=false\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000041 elif value is None:
Christopher Dunn494950a2015-01-24 15:29:52 -060042 fout.write('%s=null\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000043 else:
44 assert False and "Unexpected value type"
45
Christopher Dunn494950a2015-01-24 15:29:52 -060046def parseAndSaveValueTree(input, actual_path):
47 root = json.loads(input)
48 fout = file(actual_path, 'wt')
49 valueTreeToString(fout, root)
Christopher Dunnf9864232007-06-14 21:01:26 +000050 fout.close()
51 return root
52
Christopher Dunn494950a2015-01-24 15:29:52 -060053def rewriteValueTree(value, rewrite_path):
54 rewrite = json.dumps(value)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000055 #rewrite = rewrite[1:-1] # Somehow the string is quoted ! jsonpy bug ?
Christopher Dunn494950a2015-01-24 15:29:52 -060056 file(rewrite_path, 'wt').write(rewrite + '\n')
Christopher Dunnf9864232007-06-14 21:01:26 +000057 return rewrite
58
Christopher Dunn494950a2015-01-24 15:29:52 -060059input = file(input_path, 'rt').read()
60root = parseAndSaveValueTree(input, actual_path)
61rewrite = rewriteValueTree(json.write(root), rewrite_path)
62rewrite_root = parseAndSaveValueTree(rewrite, rewrite_actual_path)
Christopher Dunnf9864232007-06-14 21:01:26 +000063
Christopher Dunn494950a2015-01-24 15:29:52 -060064sys.exit(0)