OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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. |
| 6 // VMOptions=--enable_type_checks --enable_asserts |
| 7 |
| 8 library MessageTest; |
| 9 import 'dart:async'; |
| 10 import 'dart:collection'; |
| 11 import 'dart:isolate'; |
| 12 import 'package:async_helper/async_helper.dart'; |
| 13 import 'package:expect/expect.dart'; |
| 14 import 'dart:typed_data'; |
| 15 |
| 16 void echoMain(msg) { |
| 17 SendPort replyTo = msg[0]; |
| 18 SendPort pong = msg[1]; |
| 19 ReceivePort port = new ReceivePort(); |
| 20 replyTo.send(port.sendPort); |
| 21 port.listen((msg) { |
| 22 if (msg == "halt") { |
| 23 port.close(); |
| 24 } else { |
| 25 pong.send(msg); |
| 26 } |
| 27 }); |
| 28 } |
| 29 |
| 30 class A { |
| 31 var field = 499; |
| 32 |
| 33 A(); |
| 34 A.named(this.field); |
| 35 } |
| 36 |
| 37 class B extends A { |
| 38 final field2; |
| 39 B() : field2 = 99; |
| 40 B.named(this.field2, x) : super.named(x); |
| 41 } |
| 42 |
| 43 class C extends B { |
| 44 var field = 33; |
| 45 |
| 46 get superField => super.field; |
| 47 get superField2 => super.field2; |
| 48 } |
| 49 |
| 50 class M { |
| 51 get field2 => 11; |
| 52 } |
| 53 |
| 54 class D extends C with M { |
| 55 var gee = 123; |
| 56 } |
| 57 |
| 58 class E { |
| 59 Function fun; |
| 60 E(this.fun); |
| 61 |
| 62 static fooFun() => 499; |
| 63 instanceFun() => 1234; |
| 64 } |
| 65 barFun() => 42; |
| 66 |
| 67 class F { |
| 68 final field = "field"; |
| 69 const F(); |
| 70 } |
| 71 |
| 72 class G { |
| 73 final field; |
| 74 const G(this.field); |
| 75 } |
| 76 |
| 77 void runTests(SendPort ping, Queue checks) { |
| 78 ping.send("abc"); |
| 79 checks.add((x) => Expect.equals("abc", x)); |
| 80 |
| 81 ping.send([1, 2]); |
| 82 checks.add((x) { |
| 83 Expect.isTrue(x is List); |
| 84 Expect.listEquals([1, 2], x); |
| 85 // Make sure the list is mutable. |
| 86 x[0] = 0; |
| 87 Expect.equals(0, x[0]); |
| 88 // List must be extendable. |
| 89 x.add(3); |
| 90 Expect.equals(3, x[2]); |
| 91 }); |
| 92 |
| 93 List fixed = new List(2); |
| 94 fixed[0] = 0; |
| 95 fixed[1] = 1; |
| 96 ping.send(fixed); |
| 97 checks.add((x) { |
| 98 Expect.isTrue(x is List); |
| 99 Expect.listEquals([0, 1], x); |
| 100 // List must be mutable. |
| 101 x[0] = 3; |
| 102 Expect.equals(3, x[0]); |
| 103 // List must be fixed length. |
| 104 Expect.throws(() { x.add(5); }); |
| 105 }); |
| 106 |
| 107 List cyclic = []; |
| 108 cyclic.add(cyclic); |
| 109 ping.send(cyclic); |
| 110 checks.add((x) { |
| 111 Expect.isTrue(x is List); |
| 112 Expect.equals(1, x.length); |
| 113 Expect.identical(x, x[0]); |
| 114 // List must be mutable. |
| 115 x[0] = 55; |
| 116 Expect.equals(55, x[0]); |
| 117 // List must be extendable. |
| 118 x.add(42); |
| 119 Expect.equals(42, x[1]); |
| 120 }); |
| 121 |
| 122 List cyclic2 = new List(1); |
| 123 cyclic2[0] = cyclic2; |
| 124 ping.send(cyclic2); |
| 125 checks.add((x) { |
| 126 Expect.isTrue(x is List); |
| 127 Expect.equals(1, x.length); |
| 128 Expect.identical(x, x[0]); |
| 129 // List must be mutable. |
| 130 x[0] = 55; |
| 131 Expect.equals(55, x[0]); |
| 132 // List must be fixed. |
| 133 Expect.throws(() => x.add(42)); |
| 134 }); |
| 135 |
| 136 List constList = const [1, 2]; |
| 137 ping.send(constList); |
| 138 checks.add((x) { |
| 139 Expect.isTrue(x is List); |
| 140 Expect.listEquals([1, 2], x); |
| 141 // Make sure the list is immutable. |
| 142 Expect.throws(() => x[0] = 0); /// constList: ok |
| 143 // List must not be extendable. |
| 144 Expect.throws(() => x.add(3)); |
| 145 Expect.identical(x, constList); /// constList_identical: ok |
| 146 }); |
| 147 |
| 148 Uint8List uint8 = new Uint8List(2); |
| 149 uint8[0] = 0; |
| 150 uint8[1] = 1; |
| 151 ping.send(uint8); |
| 152 checks.add((x) { |
| 153 Expect.isTrue(x is Uint8List); |
| 154 Expect.equals(2, x.length); |
| 155 Expect.equals(0, x[0]); |
| 156 Expect.equals(1, x[1]); |
| 157 }); |
| 158 |
| 159 ping.send({"foo": 499, "bar": 32}); |
| 160 checks.add((x) { |
| 161 Expect.isTrue(x is LinkedHashMap); |
| 162 Expect.listEquals(["foo", "bar"], x.keys.toList()); |
| 163 Expect.listEquals([499, 32], x.values.toList()); |
| 164 // Must be mutable. |
| 165 x["foo"] = 22; |
| 166 Expect.equals(22, x["foo"]); |
| 167 // Must be extendable. |
| 168 x["gee"] = 499; |
| 169 Expect.equals(499, x["gee"]); |
| 170 }); |
| 171 |
| 172 ping.send({0: 499, 1: 32}); |
| 173 checks.add((x) { |
| 174 Expect.isTrue(x is LinkedHashMap); |
| 175 Expect.listEquals([0, 1], x.keys.toList()); |
| 176 Expect.listEquals([499, 32], x.values.toList()); |
| 177 // Must be mutable. |
| 178 x[0] = 22; |
| 179 Expect.equals(22, x[0]); |
| 180 // Must be extendable. |
| 181 x["gee"] = 499; |
| 182 Expect.equals(499, x["gee"]); |
| 183 }); |
| 184 |
| 185 Map cyclicMap = {}; |
| 186 cyclicMap["cycle"] = cyclicMap; |
| 187 ping.send(cyclicMap); |
| 188 checks.add((x) { |
| 189 Expect.isTrue(x is LinkedHashMap); |
| 190 Expect.identical(x, x["cycle"]); |
| 191 // Must be mutable. |
| 192 x["cycle"] = 22; |
| 193 Expect.equals(22, x["cycle"]); |
| 194 // Must be extendable. |
| 195 x["gee"] = 499; |
| 196 Expect.equals(499, x["gee"]); |
| 197 }); |
| 198 |
| 199 Map constMap = const {'foo': 499}; |
| 200 ping.send(constMap); |
| 201 checks.add((x) { |
| 202 Expect.isTrue(x is Map); |
| 203 print(x.length); |
| 204 Expect.equals(1, x.length); |
| 205 Expect.equals(499, x['foo']); |
| 206 Expect.identical(constMap, x); /// constMap: ok |
| 207 Expect.throws(() => constMap['bar'] = 42); |
| 208 }); |
| 209 |
| 210 ping.send(new A()); |
| 211 checks.add((x) { |
| 212 Expect.isTrue(x is A); |
| 213 Expect.equals(499, x.field); |
| 214 }); |
| 215 |
| 216 ping.send(new A.named(42)); |
| 217 checks.add((x) { |
| 218 Expect.isTrue(x is A); |
| 219 Expect.equals(42, x.field); |
| 220 }); |
| 221 |
| 222 ping.send(new B()); |
| 223 checks.add((x) { |
| 224 Expect.isTrue(x is A); |
| 225 Expect.isTrue(x is B); |
| 226 Expect.equals(499, x.field); |
| 227 Expect.equals(99, x.field2); |
| 228 Expect.throws(() => x.field2 = 22); |
| 229 }); |
| 230 |
| 231 ping.send(new B.named(1, 2)); |
| 232 checks.add((x) { |
| 233 Expect.isTrue(x is A); |
| 234 Expect.isTrue(x is B); |
| 235 Expect.equals(2, x.field); |
| 236 Expect.equals(1, x.field2); |
| 237 Expect.throws(() => x.field2 = 22); |
| 238 }); |
| 239 |
| 240 ping.send(new C()); |
| 241 checks.add((x) { |
| 242 Expect.isTrue(x is A); |
| 243 Expect.isTrue(x is B); |
| 244 Expect.isTrue(x is C); |
| 245 Expect.equals(33, x.field); |
| 246 Expect.equals(99, x.field2); |
| 247 Expect.equals(499, x.superField); |
| 248 Expect.throws(() => x.field2 = 22); |
| 249 }); |
| 250 |
| 251 ping.send(new D()); |
| 252 checks.add((x) { |
| 253 Expect.isTrue(x is A); |
| 254 Expect.isTrue(x is B); |
| 255 Expect.isTrue(x is C); |
| 256 Expect.isTrue(x is D); |
| 257 Expect.isTrue(x is M); |
| 258 Expect.equals(33, x.field); |
| 259 Expect.equals(11, x.field2); |
| 260 Expect.equals(499, x.superField); |
| 261 Expect.equals(99, x.superField2); |
| 262 Expect.throws(() => x.field2 = 22); |
| 263 }); |
| 264 |
| 265 D cyclicD = new D(); |
| 266 cyclicD.field = cyclicD; |
| 267 ping.send(cyclicD); |
| 268 checks.add((x) { |
| 269 Expect.isTrue(x is A); |
| 270 Expect.isTrue(x is B); |
| 271 Expect.isTrue(x is C); |
| 272 Expect.isTrue(x is D); |
| 273 Expect.isTrue(x is M); |
| 274 Expect.identical(x, x.field); |
| 275 Expect.equals(11, x.field2); |
| 276 Expect.equals(499, x.superField); |
| 277 Expect.equals(99, x.superField2); |
| 278 Expect.throws(() => x.field2 = 22); |
| 279 }); |
| 280 |
| 281 ping.send(new E(E.fooFun)); /// fun: ok |
| 282 checks.add((x) { /// fun: continued |
| 283 Expect.equals(E.fooFun, x.fun); /// fun: continued |
| 284 Expect.equals(499, x.fun()); /// fun: continued |
| 285 }); /// fun: continued |
| 286 |
| 287 ping.send(new E(barFun)); /// fun: continued |
| 288 checks.add((x) { /// fun: continued |
| 289 Expect.equals(barFun, x.fun); /// fun: continued |
| 290 Expect.equals(42, x.fun()); /// fun: continued |
| 291 }); /// fun: continued |
| 292 |
| 293 Expect.throws(() => ping.send(new E(new E(null).instanceFun))); |
| 294 |
| 295 F nonConstF = new F(); |
| 296 ping.send(nonConstF); |
| 297 checks.add((x) { |
| 298 Expect.equals("field", x.field); |
| 299 Expect.isFalse(identical(nonConstF, x)); |
| 300 }); |
| 301 |
| 302 const F constF = const F(); |
| 303 ping.send(constF); |
| 304 checks.add((x) { |
| 305 Expect.equals("field", x.field); |
| 306 Expect.identical(constF, x); /// constInstance: ok |
| 307 }); |
| 308 |
| 309 G g1 = new G(nonConstF); |
| 310 G g2 = new G(constF); |
| 311 G g3 = const G(constF); |
| 312 ping.send(g1); |
| 313 ping.send(g2); |
| 314 ping.send(g3); |
| 315 |
| 316 checks.add((x) { // g1. |
| 317 Expect.isTrue(x is G); |
| 318 Expect.isFalse(identical(g1, x)); |
| 319 F f = x.field; |
| 320 Expect.equals("field", f.field); |
| 321 Expect.isFalse(identical(nonConstF, f)); |
| 322 }); |
| 323 checks.add((x) { // g1. |
| 324 Expect.isTrue(x is G); |
| 325 Expect.isFalse(identical(g1, x)); |
| 326 F f = x.field; |
| 327 Expect.equals("field", f.field); |
| 328 Expect.identical(constF, f); /// constInstance: continued |
| 329 }); |
| 330 checks.add((x) { // g3. |
| 331 Expect.isTrue(x is G); |
| 332 Expect.identical(g1, x); /// constInstance: continued |
| 333 F f = x.field; |
| 334 Expect.equals("field", f.field); |
| 335 Expect.identical(constF, f); /// constInstance: continued |
| 336 }); |
| 337 } |
| 338 |
| 339 void main() { |
| 340 asyncStart(); |
| 341 Queue checks = new Queue(); |
| 342 ReceivePort testPort = new ReceivePort(); |
| 343 Completer completer = new Completer(); |
| 344 |
| 345 testPort.listen((msg) { |
| 346 Function check = checks.removeFirst(); |
| 347 check(msg); |
| 348 if (checks.isEmpty) { |
| 349 completer.complete(); |
| 350 testPort.close(); |
| 351 } |
| 352 }); |
| 353 |
| 354 ReceivePort initialReplyPort = new ReceivePort(); |
| 355 Isolate |
| 356 .spawn(echoMain, [initialReplyPort.sendPort, testPort.sendPort]) |
| 357 .then((_) => initialReplyPort.first) |
| 358 .then((SendPort ping) { |
| 359 runTests(ping, checks); |
| 360 Expect.isTrue(checks.length > 0); |
| 361 completer.future |
| 362 .then((_) => ping.send("halt")) |
| 363 .then((_) => asyncEnd()); |
| 364 }); |
| 365 } |
OLD | NEW |