1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import sys 6 7import data 8import test_support 9 10EXPECT_EQ = test_support.EXPECT_EQ 11EXPECT_TRUE = test_support.EXPECT_TRUE 12RunTest = test_support.RunTest 13 14 15def DeepEquals(d1, d2): 16 if d1 == d2: 17 return True 18 if d2.__class__ != d2.__class__: 19 return False 20 if isinstance(d1, dict): 21 if set(d1.keys()) != set(d2.keys()): 22 return False 23 for key in d1.keys(): 24 if not DeepEquals(d1[key], d2[key]): 25 return False 26 return True 27 if isinstance(d1, (list, tuple)): 28 if len(d1) != len(d2): 29 return False 30 for i in range(len(d1)): 31 if not DeepEquals(d1[i], d2[i]): 32 return False 33 return True 34 return False 35 36 37test_dict = { 38 'name': 'test', 39 'namespace': 'testspace', 40 'structs': [{ 41 'name': 'teststruct', 42 'fields': [ 43 {'name': 'testfield1', 'kind': 'i32'}, 44 {'name': 'testfield2', 'kind': 'a:i32', 'ordinal': 42}]}], 45 'interfaces': [{ 46 'name': 'Server', 47 'client': None, 48 'methods': [{ 49 'name': 'Foo', 50 'parameters': [ 51 {'name': 'foo', 'kind': 'i32'}, 52 {'name': 'bar', 'kind': 'a:x:teststruct'}], 53 'ordinal': 42}]}] 54} 55 56 57def TestRead(): 58 module = data.ModuleFromData(test_dict) 59 return test_support.TestTestModule(module) 60 61 62def TestWrite(): 63 module = test_support.BuildTestModule() 64 d = data.ModuleToData(module) 65 return EXPECT_TRUE(DeepEquals(test_dict, d)) 66 67 68def TestWriteRead(): 69 module1 = test_support.BuildTestModule() 70 71 dict1 = data.ModuleToData(module1) 72 module2 = data.ModuleFromData(dict1) 73 return EXPECT_TRUE(test_support.ModulesAreEqual(module1, module2)) 74 75 76def Main(args): 77 errors = 0 78 errors += RunTest(TestWriteRead) 79 errors += RunTest(TestRead) 80 errors += RunTest(TestWrite) 81 82 return errors 83 84 85if __name__ == '__main__': 86 sys.exit(Main(sys.argv[1:])) 87