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

Unified Diff: mojo/python/tests/bindings_serialization_deserialization_unittest.py

Issue 578263003: 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 side-by-side diff with in-line comments
Download patch
Index: mojo/python/tests/bindings_serialization_deserialization_unittest.py
diff --git a/mojo/python/tests/bindings_serialization_deserialization_unittest.py b/mojo/python/tests/bindings_serialization_deserialization_unittest.py
index da24645447bb119920370591b466d097e5e6ba2d..fa6a9bfe81fa175cd3f48c175208d5f80d943416 100644
--- a/mojo/python/tests/bindings_serialization_deserialization_unittest.py
+++ b/mojo/python/tests/bindings_serialization_deserialization_unittest.py
@@ -15,6 +15,16 @@ import sample_service_mojom
def _NewHandle():
return mojo.system.MessagePipe().handle0
+
+def _NewBar():
+ bar_instance = sample_service_mojom.Bar()
sdefresne 2014/09/18 12:56:22 nit: I think you don't test serialization/deserial
qsr 2014/09/18 14:16:37 Added new test for this.
+ bar_instance.alpha = 22
+ bar_instance.beta = 87
+ bar_instance.gamma = 122
+ bar_instance.type = sample_service_mojom.Bar.Type.BOTH
+ return bar_instance
+
+
def _NewFoo():
foo_instance = sample_service_mojom.Foo()
foo_instance.name = "Foo.name"
@@ -23,10 +33,10 @@ def _NewFoo():
foo_instance.a = False
foo_instance.b = True
foo_instance.c = True
- foo_instance.bar = sample_service_mojom.Bar()
+ foo_instance.bar = _NewBar()
foo_instance.extra_bars = [
- sample_service_mojom.Bar(),
- sample_service_mojom.Bar()
+ _NewBar(),
+ _NewBar(),
]
foo_instance.data = 'Hello world'
foo_instance.source = _NewHandle()
@@ -41,12 +51,89 @@ def _NewFoo():
],
[],
]
- foo_instance.array_of_bools = [ True, 0, 1, 2, 0 ]
+ foo_instance.array_of_bools = [ True, 0, 1, 2, 0, 0, 0, 0, 0, True ]
return foo_instance
+
+def _AreBarEquals(bar1, bar2):
+ if not bool(bar1):
sdefresne 2014/09/18 12:56:22 style: if not bar1: When using an object in a boo
qsr 2014/09/18 14:16:37 Method doesn't exist anymore.
+ return bar1 == bar2
+ if not bool(bar2):
+ return False
+
+ properties = [ "alpha",
sdefresne 2014/09/18 12:56:22 nit: you can use a tuple instead of a list here si
qsr 2014/09/18 14:16:37 This doesn't exist anymore.
+ "beta",
+ "gamma",
+ "type",
+ ]
+ for p in properties:
sdefresne 2014/09/18 12:56:22 You can extract this loop in a method. BTW, if yo
qsr 2014/09/18 14:16:37 Changed equality checking by having a generic equa
+ p1 = getattr(bar1, p)
+ p2 = getattr(bar1, p)
sdefresne 2014/09/18 12:56:22 s/bar1/bar2/
qsr 2014/09/18 14:16:37 Not there anymore.
+ if p1 != p2:
+ print '\nElement are not equals for property %r. %r != %r.' % (p, p1, p2)
+ return False
+ return True
+
+
+def _AreBarArraysEquals(bars1, bars2):
+ if not bool(bars1):
+ return bars1 == bars2
+ if not bool(bars2):
+ return False
+ if len(bars1) != len(bars2):
+ return False
+ for (bar1, bar2) in zip(bars1, bars2):
+ if not _AreBarEquals(bar1, bar2):
+ return False
+ return True
+
+
+def _AreFooEquals(foo1, foo2):
+ if not bool(foo1):
+ return foo1 == foo2
+ if not bool(foo2):
+ return False
+
+ properties = [ "name",
+ "x",
+ "y",
+ "a",
+ "b",
+ "c",
+ "data",
+ "source",
+ "input_streams",
+ "output_streams",
+ "array_of_array_of_bools",
+ "multi_array_of_strings",
+ "array_of_bools"
sdefresne 2014/09/18 12:56:22 nit: missing trailing comma
qsr 2014/09/18 14:16:37 Not there anymore.
+ ]
+ for p in properties:
+ p1 = getattr(foo1, p)
+ p2 = getattr(foo2, p)
+ if p1 != p2:
+ print 'Element are not equals for property %r. %r != %r.' % (p, p1, p2)
+ return False
+ if not _AreBarEquals(foo1.bar, foo2.bar):
+ return False
+ if not _AreBarArraysEquals(foo1.extra_bars, foo2.extra_bars):
+ return False
+ return True
+
class SerializationDeserializationTest(unittest.TestCase):
def testFooSerialization(self):
(data, _) = _NewFoo().Serialize()
self.assertTrue(len(data))
self.assertEquals(len(data) % 8, 0)
+
+ def testFooDeserialization(self):
+ (data, handles) = _NewFoo().Serialize()
+ self.assertTrue(
+ sample_service_mojom.Foo.Deserialize(memoryview(data), handles))
+
+ def testFooSerializationDeserialization(self):
+ foo1 = _NewFoo()
+ (data, handles) = foo1.Serialize()
+ foo2 = sample_service_mojom.Foo.Deserialize(memoryview(data), handles)
+ self.assertTrue(_AreFooEquals(foo1, foo2))
sdefresne 2014/09/18 12:56:22 Can you test failure to deserialization by passing
qsr 2014/09/18 14:16:37 Done.

Powered by Google App Engine
This is Rietveld 408576698