Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: mojo/python/tests/bindings_serialization_deserialization_unittest.py

Issue 584003002: Revert of mojo: Add deserialization to python structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/python/mojo/bindings/serialization.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import math
6 import unittest 5 import unittest
7 6
8 # pylint: disable=F0401 7 # pylint: disable=F0401
9 import mojo.bindings.reflection as reflection
10 import mojo.system 8 import mojo.system
11 9
12 # Generated files 10 # Generated files
13 # pylint: disable=F0401 11 # pylint: disable=F0401
14 import sample_import_mojom
15 import sample_import2_mojom
16 import sample_service_mojom 12 import sample_service_mojom
17 13
18 14
19 def _NewHandle(): 15 def _NewHandle():
20 return mojo.system.MessagePipe().handle0 16 return mojo.system.MessagePipe().handle0
21 17
22
23 def _TestEquality(x, y):
24 if x == y:
25 return True
26
27 if type(x) != type(y):
28 print '\n%r != %r. Element are not of the same type.' % (x, y)
29 return False
30
31 if isinstance(x, float) and math.isnan(x) and math.isnan(y):
32 return True
33
34 if hasattr(x, '__len__'):
35 if len(x) != len(y):
36 print '\n%r != %r. Iterables are not of the same size.' % (x, y)
37 return False
38 for (x1, y1) in zip(x, y):
39 if not _TestEquality(x1, y1):
40 return False
41 return True
42
43 if (hasattr(x, '__metaclass__') and
44 x.__metaclass__ == reflection.MojoStructType):
45 properties = [p for p in dir(x) if not p.startswith('_')]
46 for p in properties:
47 p1 = getattr(x, p)
48 p2 = getattr(y, p)
49 if not hasattr(p1, '__call__') and not _TestEquality(p1, p2):
50 print '\n%r != %r. Not equal for property %r.' % (x, y, p)
51 return False
52 return True
53
54 return False
55
56
57 def _NewBar():
58 bar_instance = sample_service_mojom.Bar()
59 bar_instance.alpha = 22
60 bar_instance.beta = 87
61 bar_instance.gamma = 122
62 bar_instance.type = sample_service_mojom.Bar.Type.BOTH
63 return bar_instance
64
65
66 def _NewFoo(): 18 def _NewFoo():
67 foo_instance = sample_service_mojom.Foo() 19 foo_instance = sample_service_mojom.Foo()
68 foo_instance.name = "Foo.name" 20 foo_instance.name = "Foo.name"
69 foo_instance.x = 23 21 foo_instance.x = 23
70 foo_instance.y = -23 22 foo_instance.y = -23
71 foo_instance.a = False 23 foo_instance.a = False
72 foo_instance.b = True 24 foo_instance.b = True
73 foo_instance.c = True 25 foo_instance.c = True
74 foo_instance.bar = _NewBar() 26 foo_instance.bar = sample_service_mojom.Bar()
75 foo_instance.extra_bars = [ 27 foo_instance.extra_bars = [
76 _NewBar(), 28 sample_service_mojom.Bar(),
77 _NewBar(), 29 sample_service_mojom.Bar()
78 ] 30 ]
79 foo_instance.data = 'Hello world' 31 foo_instance.data = 'Hello world'
80 foo_instance.source = _NewHandle() 32 foo_instance.source = _NewHandle()
81 foo_instance.input_streams = [ _NewHandle() ] 33 foo_instance.input_streams = [ _NewHandle() ]
82 foo_instance.output_streams = [ _NewHandle(), _NewHandle() ] 34 foo_instance.output_streams = [ _NewHandle(), _NewHandle() ]
83 foo_instance.array_of_array_of_bools = [ [ True, False ], [] ] 35 foo_instance.array_of_array_of_bools = [ [ True, False ], [] ]
84 foo_instance.multi_array_of_strings = [ 36 foo_instance.multi_array_of_strings = [
85 [ 37 [
86 [ "1", "2" ], 38 [ "1", "2" ],
87 [], 39 [],
88 [ "3", "4" ], 40 [ "3", "4" ],
89 ], 41 ],
90 [], 42 [],
91 ] 43 ]
92 foo_instance.array_of_bools = [ True, 0, 1, 2, 0, 0, 0, 0, 0, True ] 44 foo_instance.array_of_bools = [ True, 0, 1, 2, 0 ]
93 return foo_instance 45 return foo_instance
94 46
95
96 class SerializationDeserializationTest(unittest.TestCase): 47 class SerializationDeserializationTest(unittest.TestCase):
97 48
98 def testTestEquality(self):
99 self.assertFalse(_TestEquality(1, 2))
100
101 def testFooSerialization(self): 49 def testFooSerialization(self):
102 (data, _) = _NewFoo().Serialize() 50 (data, _) = _NewFoo().Serialize()
103 self.assertTrue(len(data)) 51 self.assertTrue(len(data))
104 self.assertEquals(len(data) % 8, 0) 52 self.assertEquals(len(data) % 8, 0)
105
106 def testFooDeserialization(self):
107 (data, handles) = _NewFoo().Serialize()
108 self.assertTrue(
109 sample_service_mojom.Foo.Deserialize(data, handles))
110
111 def testFooSerializationDeserialization(self):
112 foo1 = _NewFoo()
113 (data, handles) = foo1.Serialize()
114 foo2 = sample_service_mojom.Foo.Deserialize(data, handles)
115 self.assertTrue(_TestEquality(foo1, foo2))
116
117 def testDefaultsTestSerializationDeserialization(self):
118 v1 = sample_service_mojom.DefaultsTest()
119 v1.a18 = []
120 v1.a19 = ""
121 v1.a21 = sample_import_mojom.Point()
122 v1.a22.location = sample_import_mojom.Point()
123 v1.a22.size = sample_import2_mojom.Size()
124 (data, handles) = v1.Serialize()
125 v2 = sample_service_mojom.DefaultsTest.Deserialize(data, handles)
126 self.assertTrue(_TestEquality(v1, v2))
127
128 def testFooDeserializationError(self):
129 with self.assertRaises(Exception):
130 sample_service_mojom.Foo.Deserialize("", [])
OLDNEW
« no previous file with comments | « mojo/public/python/mojo/bindings/serialization.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698