blob: bd749b530f30e8264221faf0f92df65de7294910 [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
6"""Simple implementation of a json test runner to run the test against
7json-py."""
8
Christopher Dunnbd1e8952014-11-19 23:30:47 -06009from __future__ import print_function
Christopher Dunnf9864232007-06-14 21:01:26 +000010import sys
11import os.path
12import json
13import types
14
15if len(sys.argv) != 2:
Christopher Dunnbd1e8952014-11-19 23:30:47 -060016 print("Usage: %s input-json-file", sys.argv[0])
Christopher Dunnf9864232007-06-14 21:01:26 +000017 sys.exit(3)
18
19input_path = sys.argv[1]
20base_path = os.path.splitext(input_path)[0]
21actual_path = base_path + '.actual'
22rewrite_path = base_path + '.rewrite'
23rewrite_actual_path = base_path + '.actual-rewrite'
24
Christopher Dunn494950a2015-01-24 15:29:52 -060025def valueTreeToString(fout, value, path = '.'):
Christopher Dunnf9864232007-06-14 21:01:26 +000026 ty = type(value)
27 if ty is types.DictType:
Christopher Dunn494950a2015-01-24 15:29:52 -060028 fout.write('%s={}\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000029 suffix = path[-1] != '.' and '.' or ''
30 names = value.keys()
31 names.sort()
32 for name in names:
Christopher Dunn494950a2015-01-24 15:29:52 -060033 valueTreeToString(fout, value[name], path + suffix + name)
Christopher Dunnf9864232007-06-14 21:01:26 +000034 elif ty is types.ListType:
Christopher Dunn494950a2015-01-24 15:29:52 -060035 fout.write('%s=[]\n' % path)
36 for index, childValue in zip(xrange(0,len(value)), value):
37 valueTreeToString(fout, childValue, path + '[%d]' % index)
Christopher Dunnf9864232007-06-14 21:01:26 +000038 elif ty is types.StringType:
Christopher Dunn494950a2015-01-24 15:29:52 -060039 fout.write('%s="%s"\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000040 elif ty is types.IntType:
Christopher Dunn494950a2015-01-24 15:29:52 -060041 fout.write('%s=%d\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000042 elif ty is types.FloatType:
Christopher Dunn494950a2015-01-24 15:29:52 -060043 fout.write('%s=%.16g\n' % (path,value))
Christopher Dunnf9864232007-06-14 21:01:26 +000044 elif value is True:
Christopher Dunn494950a2015-01-24 15:29:52 -060045 fout.write('%s=true\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000046 elif value is False:
Christopher Dunn494950a2015-01-24 15:29:52 -060047 fout.write('%s=false\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000048 elif value is None:
Christopher Dunn494950a2015-01-24 15:29:52 -060049 fout.write('%s=null\n' % path)
Christopher Dunnf9864232007-06-14 21:01:26 +000050 else:
51 assert False and "Unexpected value type"
52
Christopher Dunn494950a2015-01-24 15:29:52 -060053def parseAndSaveValueTree(input, actual_path):
54 root = json.loads(input)
55 fout = file(actual_path, 'wt')
56 valueTreeToString(fout, root)
Christopher Dunnf9864232007-06-14 21:01:26 +000057 fout.close()
58 return root
59
Christopher Dunn494950a2015-01-24 15:29:52 -060060def rewriteValueTree(value, rewrite_path):
61 rewrite = json.dumps(value)
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +000062 #rewrite = rewrite[1:-1] # Somehow the string is quoted ! jsonpy bug ?
Christopher Dunn494950a2015-01-24 15:29:52 -060063 file(rewrite_path, 'wt').write(rewrite + '\n')
Christopher Dunnf9864232007-06-14 21:01:26 +000064 return rewrite
65
Christopher Dunn494950a2015-01-24 15:29:52 -060066input = file(input_path, 'rt').read()
67root = parseAndSaveValueTree(input, actual_path)
68rewrite = rewriteValueTree(json.write(root), rewrite_path)
69rewrite_root = parseAndSaveValueTree(rewrite, rewrite_actual_path)
Christopher Dunnf9864232007-06-14 21:01:26 +000070
Christopher Dunn494950a2015-01-24 15:29:52 -060071sys.exit(0)