OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Dart test program for testing serialization of messages without spawning | |
6 // isolates. | |
7 | |
8 // --------------------------------------------------------------------------- | |
9 // Serialization test. | |
10 // --------------------------------------------------------------------------- | |
11 library SerializationTest; | |
12 import "package:expect/expect.dart"; | |
13 import 'dart:isolate'; | |
14 | |
15 main() { | |
16 // TODO(sigmund): fix once we can disable privacy for testing (bug #1882) | |
17 testAllTypes(TestingOnly.copy); | |
18 testAllTypes(TestingOnly.serialize); | |
19 } | |
20 | |
21 void testAllTypes(Function f) { | |
22 copyAndVerify(0, f); | |
23 copyAndVerify(499, f); | |
24 copyAndVerify(true, f); | |
25 copyAndVerify(false, f); | |
26 copyAndVerify("", f); | |
27 copyAndVerify("foo", f); | |
28 copyAndVerify([], f); | |
29 copyAndVerify([1, 2], f); | |
30 copyAndVerify([[]], f); | |
31 copyAndVerify([1, []], f); | |
32 copyAndVerify({}, f); | |
33 copyAndVerify({ 'a': 3 }, f); | |
34 copyAndVerify({ 'a': 3, 'b': 5, 'c': 8 }, f); | |
35 copyAndVerify({ 'a': [1, 2] }, f); | |
36 copyAndVerify({ 'b': { 'c' : 99 } }, f); | |
37 copyAndVerify([ { 'a': 499 }, { 'b': 42 } ], f); | |
38 | |
39 var port = new ReceivePort(); | |
40 Expect.throws(() => f(port)); | |
41 port.close(); | |
42 | |
43 var a = [ 1, 3, 5 ]; | |
44 var b = { 'b': 49 }; | |
45 var c = [ a, b, a, b, a ]; | |
46 var copied = f(c); | |
47 verify(c, copied); | |
48 Expect.isFalse(identical(c, copied)); | |
49 Expect.identical(copied[0], copied[2]); | |
50 Expect.identical(copied[0], copied[4]); | |
51 Expect.identical(copied[1], copied[3]); | |
52 } | |
53 | |
54 void copyAndVerify(o, Function f) { | |
55 var copy = f(o); | |
56 verify(o, copy); | |
57 } | |
58 | |
59 void verify(o, copy) { | |
60 if ((o is bool) || (o is num) || (o is String)) { | |
61 Expect.equals(o, copy); | |
62 } else if (o is List) { | |
63 Expect.isTrue(copy is List); | |
64 Expect.equals(o.length, copy.length); | |
65 for (int i = 0; i < o.length; i++) { | |
66 verify(o[i], copy[i]); | |
67 } | |
68 } else if (o is Map) { | |
69 Expect.isTrue(copy is Map); | |
70 Expect.equals(o.length, copy.length); | |
71 o.forEach((key, value) { | |
72 Expect.isTrue(copy.containsKey(key)); | |
73 verify(value, copy[key]); | |
74 }); | |
75 } else { | |
76 Expect.fail("Unexpected object encountered"); | |
77 } | |
78 } | |
OLD | NEW |