OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library serialization_test; | 5 library serialization_test; |
6 | 6 |
7 import 'dart:json' as json; | |
7 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
8 import 'package:serialization/serialization.dart'; | 9 import 'package:serialization/serialization.dart'; |
9 import 'package:serialization/src/serialization_helpers.dart'; | 10 import 'package:serialization/src/serialization_helpers.dart'; |
10 import 'package:serialization/src/mirrors_helpers.dart'; | 11 import 'package:serialization/src/mirrors_helpers.dart'; |
11 | 12 |
12 part 'test_models.dart'; | 13 part 'test_models.dart'; |
13 | 14 |
14 main() { | 15 main() { |
15 var p1 = new Person(); | 16 var p1 = new Person(); |
16 var a1 = new Address(); | 17 var a1 = new Address(); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 }); | 230 }); |
230 | 231 |
231 test('round-trip, flat format', () { | 232 test('round-trip, flat format', () { |
232 runRoundTripTestFlat(nodeSerializerReflective); | 233 runRoundTripTestFlat(nodeSerializerReflective); |
233 }); | 234 }); |
234 | 235 |
235 test('round-trip using Maps', () { | 236 test('round-trip using Maps', () { |
236 runRoundTripTest(nodeSerializerUsingMaps); | 237 runRoundTripTest(nodeSerializerUsingMaps); |
237 }); | 238 }); |
238 | 239 |
240 test('round-trip, flat format, using maps', () { | |
241 runRoundTripTestFlat(nodeSerializerUsingMaps); | |
242 }); | |
243 | |
239 test('round-trip with Node CustomRule', () { | 244 test('round-trip with Node CustomRule', () { |
240 runRoundTripTestFlat(nodeSerializerCustom); | 245 runRoundTripTestFlat(nodeSerializerCustom); |
241 }); | 246 }); |
242 | 247 |
243 test('round-trip with Node CustomRule, to maps', () { | 248 test('round-trip with Node CustomRule, to maps', () { |
244 runRoundTripTest(nodeSerializerCustom); | 249 runRoundTripTest(nodeSerializerCustom); |
245 }); | 250 }); |
246 | 251 |
247 test('eating your own tail', () { | 252 test('eating your own tail', () { |
248 // Create a meta-serializer, that serializes serializations, then | 253 // Create a meta-serializer, that serializes serializations, then |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 expect(identical(m2.parent, m3.parent), isTrue); | 307 expect(identical(m2.parent, m3.parent), isTrue); |
303 }); | 308 }); |
304 | 309 |
305 test("Constant values as fields", () { | 310 test("Constant values as fields", () { |
306 var s = new Serialization() | 311 var s = new Serialization() |
307 ..selfDescribing = false | 312 ..selfDescribing = false |
308 ..addRuleFor(a1, | 313 ..addRuleFor(a1, |
309 constructor: 'withData', | 314 constructor: 'withData', |
310 constructorFields: ["street", "Kirkland", "WA", "98103"], | 315 constructorFields: ["street", "Kirkland", "WA", "98103"], |
311 fields: []); | 316 fields: []); |
312 var out = s.write(a1); | 317 String out = s.write(a1); |
313 var newAddress = s.read(out); | 318 var newAddress = s.read(out); |
314 expect(newAddress.street, a1.street); | 319 expect(newAddress.street, a1.street); |
315 expect(newAddress.city, "Kirkland"); | 320 expect(newAddress.city, "Kirkland"); |
316 expect(newAddress.state, "WA"); | 321 expect(newAddress.state, "WA"); |
317 expect(newAddress.zip, "98103"); | 322 expect(newAddress.zip, "98103"); |
318 }); | 323 }); |
319 | 324 |
325 // test("Straight JSON format", () { | |
Jennifer Messerly
2013/01/11 02:21:53
what's the status of this test?
Alan Knight
2013/01/11 19:18:11
Oops. Restored.
| |
326 // var s = new Serialization(); | |
327 // var writer = s.newWriter(new SimpleJSONFormat()); | |
328 // var out = writer.write(a1); | |
329 // var reconstituted = JSON.parse(out); | |
330 // expect(reconstituted.length, 4); | |
331 // expect(reconstituted[0], "Seattle"); | |
332 // }); | |
333 | |
334 test("Straight JSON format, nested objects", () { | |
335 var p1 = new Person()..name = 'Alice'..address = a1; | |
336 var s = new Serialization() | |
337 ..addRuleFor(a1).configureForMaps() | |
338 ..addRuleFor(p1).configureForMaps(); | |
339 var writer = s.newWriter(new SimpleJSONFormat(true)); | |
340 var out = writer.write(p1); | |
341 var reconstituted = json.parse(out); | |
Jennifer Messerly
2013/01/11 02:21:53
it would be interesting to see the full JSON map h
Alan Knight
2013/01/11 19:18:11
Nice. Done.
| |
342 expect(reconstituted.length, 5); | |
343 expect(reconstituted["name"], "Alice"); | |
344 var address = reconstituted["address"]; | |
345 expect(address.length, 5); | |
346 expect(address["street"], "N 34th"); | |
347 }); | |
348 | |
349 test("Straight JSON format, round-trip", () { | |
350 // Note that we can't use the usual round-trip test because it has cycles. | |
351 var p1 = new Person()..name = 'Alice'..address = a1; | |
352 // Use maps for one rule, lists for the other. | |
353 var s = new Serialization() | |
354 ..addRuleFor(a1) | |
355 ..addRuleFor(p1).configureForMaps(); | |
356 var writer = s.newWriter(new SimpleJSONFormat(true)); | |
357 var out = writer.write(p1); | |
358 var reader = s.newReader(new SimpleJSONFormat(true)); | |
359 var p2 = reader.read(out); | |
360 expect(p2.name, "Alice"); | |
361 var a2 = p2.address; | |
362 expect(a2.street, "N 34th"); | |
363 expect(a2.city, "Seattle"); | |
364 }); | |
365 | |
366 test("Straight JSON format, round-trip with named objects", () { | |
367 // Note that we can't use the usual round-trip test because it has cycles. | |
368 var p1 = new Person()..name = 'Alice'..address = a1; | |
369 // Use maps for one rule, lists for the other. | |
370 var s = new Serialization() | |
371 ..addRule(new NamedObjectRule()) | |
372 ..addRuleFor(a1) | |
373 ..addRuleFor(p1).configureForMaps() | |
374 ..namedObjects["foo"] = a1; | |
375 var writer = s.newWriter(new SimpleJSONFormat(true)); | |
376 var out = writer.write(p1); | |
377 var reader = s.newReader(new SimpleJSONFormat(true)); | |
378 var p2 = reader.read(out, {"foo" : 12}); | |
379 expect(p2.name, "Alice"); | |
380 var a2 = p2.address; | |
381 expect(a2, 12); | |
382 }); | |
320 } | 383 } |
321 | 384 |
322 /****************************************************************************** | 385 /****************************************************************************** |
323 * The end of the tests and the beginning of various helper functions to make | 386 * The end of the tests and the beginning of various helper functions to make |
324 * it easier to write the repetitive sections. | 387 * it easier to write the repetitive sections. |
325 ******************************************************************************/ | 388 ******************************************************************************/ |
326 | 389 |
327 /** Create a Serialization for serializing Serializations. */ | 390 /** Create a Serialization for serializing Serializations. */ |
328 Serialization metaSerialization() { | 391 Serialization metaSerialization() { |
329 // Make some bogus rule instances so we have something to feed rule creation | 392 // Make some bogus rule instances so we have something to feed rule creation |
(...skipping 18 matching lines...) Expand all Loading... | |
348 ..addRule(new NamedObjectRule()) | 411 ..addRule(new NamedObjectRule()) |
349 ..addRule(new MirrorRule()); | 412 ..addRule(new MirrorRule()); |
350 return meta; | 413 return meta; |
351 } | 414 } |
352 | 415 |
353 /** | 416 /** |
354 * Read back a simple object, assumed to be the only one of its class in the | 417 * Read back a simple object, assumed to be the only one of its class in the |
355 * reader. | 418 * reader. |
356 */ | 419 */ |
357 readBackSimple(Serialization s, object, Reader reader) { | 420 readBackSimple(Serialization s, object, Reader reader) { |
358 var rule = s.rulesFor(object, null)[0]; | 421 var rule = s.rulesFor(object, null).first; |
359 reader.inflateForRule(rule); | 422 reader.inflateForRule(rule); |
360 var list2 = reader.allObjectsForRule(rule)[0]; | 423 var list2 = reader.allObjectsForRule(rule).first; |
361 return list2; | 424 return list2; |
362 } | 425 } |
363 | 426 |
364 /** | 427 /** |
365 * Set up a basic reader with some fake data. Hard-codes the assumption | 428 * Set up a basic reader with some fake data. Hard-codes the assumption |
366 * of how many rules there are. | 429 * of how many rules there are. |
367 */ | 430 */ |
368 Reader setUpReader(aSerialization, sampleData) { | 431 Reader setUpReader(aSerialization, sampleData) { |
369 var reader = new Reader(aSerialization); | 432 var reader = new Reader(aSerialization); |
370 // We're not sure which rule needs the sample data, so put it everywhere | 433 // We're not sure which rule needs the sample data, so put it everywhere |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 /** | 526 /** |
464 * Run a round-trip test on a simple of nodes, but using the flat format | 527 * Run a round-trip test on a simple of nodes, but using the flat format |
465 * rather than the maps. | 528 * rather than the maps. |
466 */ | 529 */ |
467 runRoundTripTestFlat(serializerSetUp) { | 530 runRoundTripTestFlat(serializerSetUp) { |
468 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 531 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
469 n1.children = [n2, n3]; | 532 n1.children = [n2, n3]; |
470 n2.parent = n1; | 533 n2.parent = n1; |
471 n3.parent = n1; | 534 n3.parent = n1; |
472 var s = serializerSetUp(n1); | 535 var s = serializerSetUp(n1); |
473 var output = s.writeFlat(n2); | 536 var output = s.write(n2, new SimpleFlatFormat()); |
474 expect(output is List, isTrue); | 537 expect(output is List, isTrue); |
475 var s2 = serializerSetUp(n1); | 538 var s2 = serializerSetUp(n1); |
476 var reader = new Reader(s2); | 539 var reader = new Reader(s2, new SimpleFlatFormat()); |
477 var m2 = reader.readFlat(output); | 540 var m2 = reader.read(output); |
478 var m1 = m2.parent; | 541 var m1 = m2.parent; |
479 expect(m1 is Node, isTrue); | 542 expect(m1 is Node, isTrue); |
480 var children = m1.children; | 543 var children = m1.children; |
481 expect(m1.name,"1"); | 544 expect(m1.name,"1"); |
482 var m3 = m1.children.last; | 545 var m3 = m1.children.last; |
483 expect(m2.name, "2"); | 546 expect(m2.name, "2"); |
484 expect(m3.name, "3"); | 547 expect(m3.name, "3"); |
485 expect(m2.parent, m1); | 548 expect(m2.parent, m1); |
486 expect(m3.parent, m1); | 549 expect(m3.parent, m1); |
487 expect(m1.parent, isNull); | 550 expect(m1.parent, isNull); |
488 } | 551 } |
489 | 552 |
490 /** Extract the state from [object] using the rules in [s] and return it. */ | 553 /** Extract the state from [object] using the rules in [s] and return it. */ |
491 states(object, Serialization s) { | 554 states(object, Serialization s) { |
492 var rules = s.rulesFor(object, null); | 555 var rules = s.rulesFor(object, null); |
493 return rules.mappedBy((x) => x.extractState(object, doNothing)).toList(); | 556 return rules.mappedBy((x) => x.extractState(object, doNothing)).toList(); |
494 } | 557 } |
495 | 558 |
496 /** A hard-coded rule for serializing Node instances. */ | 559 /** A hard-coded rule for serializing Node instances. */ |
497 class NodeRule extends CustomRule { | 560 class NodeRule extends CustomRule { |
498 bool appliesTo(instance, _) => instance is Node; | 561 bool appliesTo(instance, _) => instance.runtimeType == Node; |
499 getState(instance) => [instance.parent, instance.name, instance.children]; | 562 getState(instance) => [instance.parent, instance.name, instance.children]; |
500 create(state) => new Node(state[1]); | 563 create(state) => new Node(state[1]); |
501 setState(Node node, state) { | 564 setState(Node node, state) { |
502 node.parent = state[0]; | 565 node.parent = state[0]; |
503 node.children = state[2]; | 566 node.children = state[2]; |
504 } | 567 } |
505 } | 568 } |
OLD | NEW |