OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 # Test that JSON/IR import/export works, |
| 31 # by checking that roundtripping from JSON to IR and back to JSON |
| 32 # doesn't change the data. |
| 33 |
| 34 import json |
| 35 import optparse |
| 36 import pprint |
| 37 import sys |
| 38 |
| 39 from ir import IdlDocument |
| 40 import json_import_export |
| 41 |
| 42 |
| 43 def parse_options(): |
| 44 parser = optparse.OptionParser() |
| 45 parser.add_option('--debug', action='store_true', default=False) |
| 46 parser.disable_interspersed_args() |
| 47 options, args = parser.parse_args() |
| 48 if len(args) != 1: |
| 49 parser.error('Must specify exactly 1 JSON input file as argument, but %d
given.' % len(args)) |
| 50 options.json_filename = args[0] |
| 51 return options |
| 52 |
| 53 |
| 54 def main(): |
| 55 options = parse_options() |
| 56 # 0 = Ok, 1 = Failure, so invert equality |
| 57 return not test_roundtrip_json(options.json_filename, debug=options.debug) |
| 58 |
| 59 |
| 60 def test_roundtrip_json(json_filename, debug=False): |
| 61 with open(json_filename) as json_file: |
| 62 json_text = json_file.read() |
| 63 raw_dict = json.loads(json_text) |
| 64 # Convert to IR |
| 65 clean_dict = json_import_export.strip_types(raw_dict) |
| 66 document = IdlDocument() |
| 67 document.from_json(clean_dict) |
| 68 # Convert back to JSON |
| 69 json_back = json_import_export.ir_to_json(document) |
| 70 if debug: |
| 71 # Easier to compare and dictionaries, not raw strings |
| 72 dict_back = json.loads(json_back) |
| 73 print 'Original --------------------------------------------------------
-----' |
| 74 pprint.pprint(raw_dict) |
| 75 print 'Roundtrip -------------------------------------------------------
-----' |
| 76 pprint.pprint(dict_back) |
| 77 return json_text == json_back |
| 78 |
| 79 |
| 80 if __name__ == '__main__': |
| 81 sys.exit(main()) |
OLD | NEW |