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

Side by Side Diff: pkg/serialization/test/serialization_test.dart

Issue 17578002: pkg/serialization: add format param to Serialization.read method (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: A few more tweaks Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
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 'dart:json' as json;
8 import 'package:unittest/unittest.dart'; 8 import 'package:unittest/unittest.dart';
9 import 'package:serialization/serialization.dart'; 9 import 'package:serialization/serialization.dart';
10 import 'package:serialization/src/serialization_helpers.dart'; 10 import 'package:serialization/src/serialization_helpers.dart';
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 var s = new Serialization.blank() 290 var s = new Serialization.blank()
291 // Add the rules in a deliberately unusual order. 291 // Add the rules in a deliberately unusual order.
292 ..addRuleFor(new Node(''), constructorFields: ['name']) 292 ..addRuleFor(new Node(''), constructorFields: ['name'])
293 ..addRule(new ListRule()) 293 ..addRule(new ListRule())
294 ..addRule(new PrimitiveRule()) 294 ..addRule(new PrimitiveRule())
295 ..selfDescribing = false; 295 ..selfDescribing = false;
296 var meta = metaSerialization(); 296 var meta = metaSerialization();
297 var metaWithMaps = metaSerializationUsingMaps(); 297 var metaWithMaps = metaSerializationUsingMaps();
298 for (var eachFormat in formats) { 298 for (var eachFormat in formats) {
299 for (var eachMeta in [meta, metaWithMaps]) { 299 for (var eachMeta in [meta, metaWithMaps]) {
300 var serialized = eachMeta.write(s, eachFormat); 300 var serialized = eachMeta.write(s, format: eachFormat);
301 var reader = new Reader(eachMeta, eachFormat); 301 var newSerialization = eachMeta.read(serialized, format: eachFormat,
302 var newSerialization = reader.read(serialized, 302 externals: {"serialization_test.Node" : reflect(new Node('')).type}
303 {"serialization_test.Node" : reflect(new Node('')).type}); 303 );
304 runRoundTripTest((x) => newSerialization); 304 runRoundTripTest((x) => newSerialization);
305 } 305 }
306 } 306 }
307 }); 307 });
308 308
309 test("Verify we're not serializing lists twice if they're essential", () { 309 test("Verify we're not serializing lists twice if they're essential", () {
310 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); 310 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3");
311 n1.children = [n2, n3]; 311 n1.children = [n2, n3];
312 n2.parent = n1; 312 n2.parent = n1;
313 n3.parent = n1; 313 n3.parent = n1;
(...skipping 18 matching lines...) Expand all
332 test('Identity of equal objects preserved', () { 332 test('Identity of equal objects preserved', () {
333 Node n1 = new NodeEqualByName("foo"), 333 Node n1 = new NodeEqualByName("foo"),
334 n2 = new NodeEqualByName("foo"), 334 n2 = new NodeEqualByName("foo"),
335 n3 = new NodeEqualByName("3"); 335 n3 = new NodeEqualByName("3");
336 n1.children = [n2, n3]; 336 n1.children = [n2, n3];
337 n2.parent = n1; 337 n2.parent = n1;
338 n3.parent = n1; 338 n3.parent = n1;
339 var s = new Serialization() 339 var s = new Serialization()
340 ..selfDescribing = false 340 ..selfDescribing = false
341 ..addRuleFor(n1, constructorFields: ["name"]); 341 ..addRuleFor(n1, constructorFields: ["name"]);
342 var w = new Writer(s); 342 var m1 = writeAndReadBack(s, null, n1);
343 var r = new Reader(s);
344 var m1 = r.read(w.write(n1));
345 var m2 = m1.children.first; 343 var m2 = m1.children.first;
346 var m3 = m1.children.last; 344 var m3 = m1.children.last;
347 expect(m1, m2); 345 expect(m1, m2);
348 expect(identical(m1, m2), isFalse); 346 expect(identical(m1, m2), isFalse);
349 expect(m1 == m3, isFalse); 347 expect(m1 == m3, isFalse);
350 expect(identical(m2.parent, m3.parent), isTrue); 348 expect(identical(m2.parent, m3.parent), isTrue);
351 }); 349 });
352 350
353 test("Constant values as fields", () { 351 test("Constant values as fields", () {
354 var s = new Serialization() 352 var s = new Serialization()
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 test("Root is a Map", () { 428 test("Root is a Map", () {
431 // Note that we can't use the usual round-trip test because it has cycles. 429 // Note that we can't use the usual round-trip test because it has cycles.
432 var p1 = new Person()..name = 'Alice'..address = a1; 430 var p1 = new Person()..name = 'Alice'..address = a1;
433 // Use maps for one rule, lists for the other. 431 // Use maps for one rule, lists for the other.
434 var s = new Serialization() 432 var s = new Serialization()
435 ..addRuleFor(a1) 433 ..addRuleFor(a1)
436 ..addRuleFor(p1).configureForMaps(); 434 ..addRuleFor(p1).configureForMaps();
437 for (var eachFormat in formats) { 435 for (var eachFormat in formats) {
438 var w = s.newWriter(eachFormat); 436 var w = s.newWriter(eachFormat);
439 var output = w.write({"stuff" : p1}); 437 var output = w.write({"stuff" : p1});
440 var r = s.newReader(w.format); 438 var result = s.read(output, format: w.format);
Alan Knight 2013/06/26 19:26:29 I would say to leave a couple of these cases using
441 var result = r.read(output);
442 var p2 = result["stuff"]; 439 var p2 = result["stuff"];
443 expect(p2.name, "Alice"); 440 expect(p2.name, "Alice");
444 var a2 = p2.address; 441 var a2 = p2.address;
445 expect(a2.street, "N 34th"); 442 expect(a2.street, "N 34th");
446 expect(a2.city, "Seattle"); 443 expect(a2.city, "Seattle");
447 } 444 }
448 }); 445 });
449 446
450 test("Root is a List", () { 447 test("Root is a List", () {
451 var s = new Serialization(); 448 var s = new Serialization();
(...skipping 23 matching lines...) Expand all
475 test("Simple JSON format, round-trip with named objects", () { 472 test("Simple JSON format, round-trip with named objects", () {
476 // Note that we can't use the usual round-trip test because it has cycles. 473 // Note that we can't use the usual round-trip test because it has cycles.
477 var p1 = new Person()..name = 'Alice'..address = a1; 474 var p1 = new Person()..name = 'Alice'..address = a1;
478 // Use maps for one rule, lists for the other. 475 // Use maps for one rule, lists for the other.
479 var s = new Serialization() 476 var s = new Serialization()
480 ..selfDescribing = false 477 ..selfDescribing = false
481 ..addRule(new NamedObjectRule()) 478 ..addRule(new NamedObjectRule())
482 ..addRuleFor(a1) 479 ..addRuleFor(a1)
483 ..addRuleFor(p1).configureForMaps() 480 ..addRuleFor(p1).configureForMaps()
484 ..namedObjects["foo"] = a1; 481 ..namedObjects["foo"] = a1;
485 var writer = s.newWriter(const SimpleJsonFormat(storeRoundTripInfo: true)); 482 var format = const SimpleJsonFormat(storeRoundTripInfo: true);
486 var out = writer.write(p1); 483 var out = s.write(p1, format: format);
487 var reader = s.newReader(const SimpleJsonFormat(storeRoundTripInfo: true)); 484 var p2 = s.read(out, format: format, externals: {"foo" : 12});
488 var p2 = reader.read(out, {"foo" : 12});
489 expect(p2.name, "Alice"); 485 expect(p2.name, "Alice");
490 var a2 = p2.address; 486 var a2 = p2.address;
491 expect(a2, 12); 487 expect(a2, 12);
492 }); 488 });
493 489
494 test("More complicated Maps", () { 490 test("More complicated Maps", () {
495 var s = new Serialization()..selfDescribing = false; 491 var s = new Serialization()..selfDescribing = false;
496 var p1 = new Person()..name = 'Alice'..address = a1; 492 var p1 = new Person()..name = 'Alice'..address = a1;
497 var data = new Map(); 493 var data = new Map();
498 data["simple data"] = 1; 494 data["simple data"] = 1;
499 data[p1] = a1; 495 data[p1] = a1;
500 data[a1] = p1; 496 data[a1] = p1;
501 for (var eachFormat in formats) { 497 for (var eachFormat in formats) {
502 var output = s.write(data, eachFormat); 498 var output = s.write(data, format: eachFormat);
503 var reader = s.newReader(eachFormat); 499 var input = s.read(output, format: eachFormat);
504 var input = reader.read(output);
505 expect(input["simple data"], data["simple data"]); 500 expect(input["simple data"], data["simple data"]);
506 var p2 = input.keys.firstWhere((x) => x is Person); 501 var p2 = input.keys.firstWhere((x) => x is Person);
507 var a2 = input.keys.firstWhere((x) => x is Address); 502 var a2 = input.keys.firstWhere((x) => x is Address);
508 if (eachFormat is SimpleJsonFormat) { 503 if (eachFormat is SimpleJsonFormat) {
509 // JSON doesn't handle cycles, so these won't be identical. 504 // JSON doesn't handle cycles, so these won't be identical.
510 expect(input[p2] is Address, isTrue); 505 expect(input[p2] is Address, isTrue);
511 expect(input[a2] is Person, isTrue); 506 expect(input[a2] is Person, isTrue);
512 var a3 = input[p2]; 507 var a3 = input[p2];
513 expect(a3.city, a2.city); 508 expect(a3.city, a2.city);
514 expect(a3.state, a2.state); 509 expect(a3.state, a2.state);
515 expect(a3.state, a2.state); 510 expect(a3.state, a2.state);
516 var p3 = input[a2]; 511 var p3 = input[a2];
517 expect(p3.name, p2.name); 512 expect(p3.name, p2.name);
518 expect(p3.rank, p2.rank); 513 expect(p3.rank, p2.rank);
519 expect(p3.address.city, a2.city); 514 expect(p3.address.city, a2.city);
520 } else { 515 } else {
521 expect(input[p2], same(a2)); 516 expect(input[p2], same(a2));
522 expect(input[a2], same(p2)); 517 expect(input[a2], same(p2));
523 } 518 }
524 } 519 }
525 }); 520 });
526 521
527 test("Map with string keys stays that way", () { 522 test("Map with string keys stays that way", () {
528 var s = new Serialization()..addRuleFor(new Person()); 523 var s = new Serialization()..addRuleFor(new Person());
529 var data = {"abc" : 1, "def" : "ghi"}; 524 var data = {"abc" : 1, "def" : "ghi"};
530 data["person"] = new Person()..name = "Foo"; 525 data["person"] = new Person()..name = "Foo";
531 var output = s.write(data, const InternalMapFormat()); 526 var output = s.write(data, format: const InternalMapFormat());
532 var mapRule = s.rules.firstWhere((x) => x is MapRule); 527 var mapRule = s.rules.firstWhere((x) => x is MapRule);
533 var map = output["data"][mapRule.number][0]; 528 var map = output["data"][mapRule.number][0];
534 expect(map is Map, isTrue); 529 expect(map is Map, isTrue);
535 expect(map["abc"], 1); 530 expect(map["abc"], 1);
536 expect(map["def"], "ghi"); 531 expect(map["def"], "ghi");
537 expect(map["person"] is Reference, isTrue); 532 expect(map["person"] is Reference, isTrue);
538 }); 533 });
539 534
540 test("MirrorRule with lookup by qualified name rather than named object", () { 535 test("MirrorRule with lookup by qualified name rather than named object", () {
541 var s = new Serialization()..addRule(new MirrorRule()); 536 var s = new Serialization()..addRule(new MirrorRule());
(...skipping 14 matching lines...) Expand all
556 var port = spawnFunction(echo); 551 var port = spawnFunction(echo);
557 return port.call(output).then(verify); 552 return port.call(output).then(verify);
558 }); 553 });
559 } 554 }
560 555
561 /** 556 /**
562 * Verify serialized output that we have passed to an isolate and back. 557 * Verify serialized output that we have passed to an isolate and back.
563 */ 558 */
564 void verify(input) { 559 void verify(input) {
565 var s2 = nodeSerializerReflective(new Node("a")); 560 var s2 = nodeSerializerReflective(new Node("a"));
566 var reader = new Reader(s2); 561 var m2 = s2.read(input);
567 var m2 = reader.read(input);
568 var m1 = m2.parent; 562 var m1 = m2.parent;
569 expect(m1 is Node, isTrue); 563 expect(m1 is Node, isTrue);
570 var children = m1.children; 564 var children = m1.children;
571 expect(m1.name,"1"); 565 expect(m1.name,"1");
572 var m3 = m1.children.last; 566 var m3 = m1.children.last;
573 expect(m2.name, "2"); 567 expect(m2.name, "2");
574 expect(m3.name, "3"); 568 expect(m3.name, "3");
575 expect(m2.parent, m1); 569 expect(m2.parent, m1);
576 expect(m3.parent, m1); 570 expect(m3.parent, m1);
577 expect(m1.parent, isNull); 571 expect(m1.parent, isNull);
578 } 572 }
579 573
580 /****************************************************************************** 574 /******************************************************************************
581 * The end of the tests and the beginning of various helper functions to make 575 * The end of the tests and the beginning of various helper functions to make
582 * it easier to write the repetitive sections. 576 * it easier to write the repetitive sections.
583 ******************************************************************************/ 577 ******************************************************************************/
584 578
585 writeAndReadBack(Serialization s, Format format, object) { 579 writeAndReadBack(Serialization s, Format format, object) {
586 var w = s.newWriter(format); 580 var output = s.write(object, format: format);
587 var output = w.write(object); 581 return s.read(output, format: format);
588 var r = s.newReader(w.format);
589 return r.read(output);
590 } 582 }
591 583
592 /** Create a Serialization for serializing Serializations. */ 584 /** Create a Serialization for serializing Serializations. */
593 Serialization metaSerialization() { 585 Serialization metaSerialization() {
594 // Make some bogus rule instances so we have something to feed rule creation 586 // Make some bogus rule instances so we have something to feed rule creation
595 // and get their types. If only we had class literals implemented... 587 // and get their types. If only we had class literals implemented...
596 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); 588 var basicRule = new BasicRule(reflect(null).type, '', [], [], []);
597 589
598 var meta = new Serialization() 590 var meta = new Serialization()
599 ..selfDescribing = false 591 ..selfDescribing = false
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 * that's returned by the [serializerSetUp] function. 711 * that's returned by the [serializerSetUp] function.
720 */ 712 */
721 void runRoundTripTest(Function serializerSetUp) { 713 void runRoundTripTest(Function serializerSetUp) {
722 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); 714 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3");
723 n1.children = [n2, n3]; 715 n1.children = [n2, n3];
724 n2.parent = n1; 716 n2.parent = n1;
725 n3.parent = n1; 717 n3.parent = n1;
726 var s = serializerSetUp(n1); 718 var s = serializerSetUp(n1);
727 var output = s.write(n2); 719 var output = s.write(n2);
728 var s2 = serializerSetUp(n1); 720 var s2 = serializerSetUp(n1);
729 var reader = new Reader(s2); 721 var m2 = s2.read(output);
730 var m2 = reader.read(output);
731 var m1 = m2.parent; 722 var m1 = m2.parent;
732 expect(m1 is Node, isTrue); 723 expect(m1 is Node, isTrue);
733 var children = m1.children; 724 var children = m1.children;
734 expect(m1.name,"1"); 725 expect(m1.name,"1");
735 var m3 = m1.children.last; 726 var m3 = m1.children.last;
736 expect(m2.name, "2"); 727 expect(m2.name, "2");
737 expect(m3.name, "3"); 728 expect(m3.name, "3");
738 expect(m2.parent, m1); 729 expect(m2.parent, m1);
739 expect(m3.parent, m1); 730 expect(m3.parent, m1);
740 expect(m1.parent, isNull); 731 expect(m1.parent, isNull);
741 } 732 }
742 733
743 /** 734 /**
744 * Run a round-trip test on a simple of nodes, but using the flat format 735 * Run a round-trip test on a simple of nodes, but using the flat format
745 * rather than the maps. 736 * rather than the maps.
746 */ 737 */
747 void runRoundTripTestFlat(serializerSetUp) { 738 void runRoundTripTestFlat(serializerSetUp) {
748 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); 739 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3");
749 n1.children = [n2, n3]; 740 n1.children = [n2, n3];
750 n2.parent = n1; 741 n2.parent = n1;
751 n3.parent = n1; 742 n3.parent = n1;
752 var s = serializerSetUp(n1); 743 var s = serializerSetUp(n1);
753 var output = s.write(n2, const SimpleFlatFormat()); 744 var output = s.write(n2, format: const SimpleFlatFormat());
754 expect(output is List, isTrue); 745 expect(output is List, isTrue);
755 var s2 = serializerSetUp(n1); 746 var s2 = serializerSetUp(n1);
756 var reader = new Reader(s2, const SimpleFlatFormat()); 747 var m2 = s2.read(output, format: const SimpleFlatFormat());
757 var m2 = reader.read(output);
758 var m1 = m2.parent; 748 var m1 = m2.parent;
759 expect(m1 is Node, isTrue); 749 expect(m1 is Node, isTrue);
760 var children = m1.children; 750 var children = m1.children;
761 expect(m1.name,"1"); 751 expect(m1.name,"1");
762 var m3 = m1.children.last; 752 var m3 = m1.children.last;
763 expect(m2.name, "2"); 753 expect(m2.name, "2");
764 expect(m3.name, "3"); 754 expect(m3.name, "3");
765 expect(m2.parent, m1); 755 expect(m2.parent, m1);
766 expect(m3.parent, m1); 756 expect(m3.parent, m1);
767 expect(m1.parent, isNull); 757 expect(m1.parent, isNull);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 804
815 /** 805 /**
816 * Function used in an isolate to make sure that the output passes through 806 * Function used in an isolate to make sure that the output passes through
817 * isolate serialization properly. 807 * isolate serialization properly.
818 */ 808 */
819 void echo() { 809 void echo() {
820 port.receive((msg, reply) { 810 port.receive((msg, reply) {
821 reply.send(msg); 811 reply.send(msg);
822 }); 812 });
823 } 813 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698