OLD | NEW |
| (Empty) |
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 | |
5 import mojom_data | |
6 import mojom_test | |
7 import sys | |
8 | |
9 EXPECT_EQ = mojom_test.EXPECT_EQ | |
10 EXPECT_TRUE = mojom_test.EXPECT_TRUE | |
11 RunTest = mojom_test.RunTest | |
12 | |
13 | |
14 def DeepEquals(d1, d2): | |
15 if d1 == d2: | |
16 return True | |
17 if d2.__class__ != d2.__class__: | |
18 return False | |
19 if isinstance(d1, dict): | |
20 if set(d1.keys()) != set(d2.keys()): | |
21 return False | |
22 for key in d1.keys(): | |
23 if not DeepEquals(d1[key], d2[key]): | |
24 return False | |
25 return True | |
26 if isinstance(d1, (list, tuple)): | |
27 if len(d1) != len(d2): | |
28 return False | |
29 for i in range(len(d1)): | |
30 if not DeepEquals(d1[i], d2[i]): | |
31 return False | |
32 return True | |
33 return False | |
34 | |
35 | |
36 test_dict = { | |
37 'name': 'test', | |
38 'namespace': 'testspace', | |
39 'structs': [{ | |
40 'name': 'teststruct', | |
41 'fields': [ | |
42 {'name': 'testfield1', 'kind': 'i32'}, | |
43 {'name': 'testfield2', 'kind': 'a:i32', 'ordinal': 42}]}], | |
44 'interfaces': [{ | |
45 'name': 'Server', | |
46 'peer': None, | |
47 'methods': [{ | |
48 'name': 'Foo', | |
49 'parameters': [ | |
50 {'name': 'foo', 'kind': 'i32'}, | |
51 {'name': 'bar', 'kind': 'a:x:teststruct'}], | |
52 'ordinal': 42}]}] | |
53 } | |
54 | |
55 | |
56 def TestRead(): | |
57 module = mojom_data.ModuleFromData(test_dict) | |
58 return mojom_test.TestTestModule(module) | |
59 | |
60 | |
61 def TestWrite(): | |
62 module = mojom_test.BuildTestModule() | |
63 d = mojom_data.ModuleToData(module) | |
64 return EXPECT_TRUE(DeepEquals(test_dict, d)) | |
65 | |
66 | |
67 def TestWriteRead(): | |
68 module1 = mojom_test.BuildTestModule() | |
69 | |
70 dict1 = mojom_data.ModuleToData(module1) | |
71 module2 = mojom_data.ModuleFromData(dict1) | |
72 return EXPECT_TRUE(mojom_test.ModulesAreEqual(module1, module2)) | |
73 | |
74 | |
75 def Main(args): | |
76 errors = 0 | |
77 errors += RunTest(TestWriteRead) | |
78 errors += RunTest(TestRead) | |
79 errors += RunTest(TestWrite) | |
80 | |
81 return errors | |
82 | |
83 | |
84 if __name__ == '__main__': | |
85 sys.exit(Main(sys.argv[1:])) | |
OLD | NEW |