| 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 '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'; |
| 11 import 'package:serialization/src/mirrors_helpers.dart'; | 11 import 'package:serialization/src/mirrors_helpers.dart'; |
| 12 import 'dart:isolate'; | 12 import 'dart:isolate'; |
| 13 | 13 |
| 14 part 'test_models.dart'; | 14 part 'test_models.dart'; |
| 15 | 15 |
| 16 main() { | 16 void main() { |
| 17 var p1 = new Person(); | 17 var p1 = new Person(); |
| 18 var a1 = new Address(); | 18 var a1 = new Address(); |
| 19 a1.street = 'N 34th'; | 19 a1.street = 'N 34th'; |
| 20 a1.city = 'Seattle'; | 20 a1.city = 'Seattle'; |
| 21 | 21 |
| 22 var formats = [const InternalMapFormat(), | 22 var formats = [const InternalMapFormat(), |
| 23 const SimpleFlatFormat(), const SimpleMapFormat(), | 23 const SimpleFlatFormat(), const SimpleMapFormat(), |
| 24 const SimpleJsonFormat(storeRoundTripInfo: true)]; | 24 const SimpleJsonFormat(storeRoundTripInfo: true)]; |
| 25 | 25 |
| 26 test('Basic extraction of a simple object', () { | 26 test('Basic extraction of a simple object', () { |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 }); | 547 }); |
| 548 | 548 |
| 549 test('round-trip, default format, pass to isolate', () { | 549 test('round-trip, default format, pass to isolate', () { |
| 550 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 550 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
| 551 n1.children = [n2, n3]; | 551 n1.children = [n2, n3]; |
| 552 n2.parent = n1; | 552 n2.parent = n1; |
| 553 n3.parent = n1; | 553 n3.parent = n1; |
| 554 var s = nodeSerializerReflective(n1); | 554 var s = nodeSerializerReflective(n1); |
| 555 var output = s.write(n2); | 555 var output = s.write(n2); |
| 556 var port = spawnFunction(echo); | 556 var port = spawnFunction(echo); |
| 557 port.call(output).then(expectAsync1((input) => verify(input))); | 557 return port.call(output).then(verify); |
| 558 }); | 558 }); |
| 559 } | 559 } |
| 560 | 560 |
| 561 /** | 561 /** |
| 562 * Verify serialized output that we have passed to an isolate and back. | 562 * Verify serialized output that we have passed to an isolate and back. |
| 563 */ | 563 */ |
| 564 verify(input) { | 564 void verify(input) { |
| 565 var s2 = nodeSerializerReflective(new Node("a")); | 565 var s2 = nodeSerializerReflective(new Node("a")); |
| 566 var reader = new Reader(s2); | 566 var reader = new Reader(s2); |
| 567 var m2 = reader.read(input); | 567 var m2 = reader.read(input); |
| 568 var m1 = m2.parent; | 568 var m1 = m2.parent; |
| 569 expect(m1 is Node, isTrue); | 569 expect(m1 is Node, isTrue); |
| 570 var children = m1.children; | 570 var children = m1.children; |
| 571 expect(m1.name,"1"); | 571 expect(m1.name,"1"); |
| 572 var m3 = m1.children.last; | 572 var m3 = m1.children.last; |
| 573 expect(m2.name, "2"); | 573 expect(m2.name, "2"); |
| 574 expect(m3.name, "3"); | 574 expect(m3.name, "3"); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 }); | 711 }); |
| 712 return new Serialization() | 712 return new Serialization() |
| 713 ..selfDescribing = false | 713 ..selfDescribing = false |
| 714 ..addRule(rule); | 714 ..addRule(rule); |
| 715 } | 715 } |
| 716 | 716 |
| 717 /** | 717 /** |
| 718 * Run a round-trip test on a simple tree of nodes, using a serialization | 718 * Run a round-trip test on a simple tree of nodes, using a serialization |
| 719 * that's returned by the [serializerSetUp] function. | 719 * that's returned by the [serializerSetUp] function. |
| 720 */ | 720 */ |
| 721 runRoundTripTest(Function serializerSetUp) { | 721 void runRoundTripTest(Function serializerSetUp) { |
| 722 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 722 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
| 723 n1.children = [n2, n3]; | 723 n1.children = [n2, n3]; |
| 724 n2.parent = n1; | 724 n2.parent = n1; |
| 725 n3.parent = n1; | 725 n3.parent = n1; |
| 726 var s = serializerSetUp(n1); | 726 var s = serializerSetUp(n1); |
| 727 var output = s.write(n2); | 727 var output = s.write(n2); |
| 728 var s2 = serializerSetUp(n1); | 728 var s2 = serializerSetUp(n1); |
| 729 var reader = new Reader(s2); | 729 var reader = new Reader(s2); |
| 730 var m2 = reader.read(output); | 730 var m2 = reader.read(output); |
| 731 var m1 = m2.parent; | 731 var m1 = m2.parent; |
| 732 expect(m1 is Node, isTrue); | 732 expect(m1 is Node, isTrue); |
| 733 var children = m1.children; | 733 var children = m1.children; |
| 734 expect(m1.name,"1"); | 734 expect(m1.name,"1"); |
| 735 var m3 = m1.children.last; | 735 var m3 = m1.children.last; |
| 736 expect(m2.name, "2"); | 736 expect(m2.name, "2"); |
| 737 expect(m3.name, "3"); | 737 expect(m3.name, "3"); |
| 738 expect(m2.parent, m1); | 738 expect(m2.parent, m1); |
| 739 expect(m3.parent, m1); | 739 expect(m3.parent, m1); |
| 740 expect(m1.parent, isNull); | 740 expect(m1.parent, isNull); |
| 741 } | 741 } |
| 742 | 742 |
| 743 /** | 743 /** |
| 744 * Run a round-trip test on a simple of nodes, but using the flat format | 744 * Run a round-trip test on a simple of nodes, but using the flat format |
| 745 * rather than the maps. | 745 * rather than the maps. |
| 746 */ | 746 */ |
| 747 runRoundTripTestFlat(serializerSetUp) { | 747 void runRoundTripTestFlat(serializerSetUp) { |
| 748 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 748 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
| 749 n1.children = [n2, n3]; | 749 n1.children = [n2, n3]; |
| 750 n2.parent = n1; | 750 n2.parent = n1; |
| 751 n3.parent = n1; | 751 n3.parent = n1; |
| 752 var s = serializerSetUp(n1); | 752 var s = serializerSetUp(n1); |
| 753 var output = s.write(n2, const SimpleFlatFormat()); | 753 var output = s.write(n2, const SimpleFlatFormat()); |
| 754 expect(output is List, isTrue); | 754 expect(output is List, isTrue); |
| 755 var s2 = serializerSetUp(n1); | 755 var s2 = serializerSetUp(n1); |
| 756 var reader = new Reader(s2, const SimpleFlatFormat()); | 756 var reader = new Reader(s2, const SimpleFlatFormat()); |
| 757 var m2 = reader.read(output); | 757 var m2 = reader.read(output); |
| 758 var m1 = m2.parent; | 758 var m1 = m2.parent; |
| 759 expect(m1 is Node, isTrue); | 759 expect(m1 is Node, isTrue); |
| 760 var children = m1.children; | 760 var children = m1.children; |
| 761 expect(m1.name,"1"); | 761 expect(m1.name,"1"); |
| 762 var m3 = m1.children.last; | 762 var m3 = m1.children.last; |
| 763 expect(m2.name, "2"); | 763 expect(m2.name, "2"); |
| 764 expect(m3.name, "3"); | 764 expect(m3.name, "3"); |
| 765 expect(m2.parent, m1); | 765 expect(m2.parent, m1); |
| 766 expect(m3.parent, m1); | 766 expect(m3.parent, m1); |
| 767 expect(m1.parent, isNull); | 767 expect(m1.parent, isNull); |
| 768 } | 768 } |
| 769 | 769 |
| 770 /** Extract the state from [object] using the rules in [s] and return it. */ | 770 /** Extract the state from [object] using the rules in [s] and return it. */ |
| 771 states(object, Serialization s) { | 771 List states(object, Serialization s) { |
| 772 var rules = s.rulesFor(object, null); | 772 var rules = s.rulesFor(object, null); |
| 773 return rules.map((x) => x.extractState(object, doNothing, null)).toList(); | 773 return rules.map((x) => x.extractState(object, doNothing, null)).toList(); |
| 774 } | 774 } |
| 775 | 775 |
| 776 /** A hard-coded rule for serializing Node instances. */ | 776 /** A hard-coded rule for serializing Node instances. */ |
| 777 class NodeRule extends CustomRule { | 777 class NodeRule extends CustomRule { |
| 778 bool appliesTo(instance, _) => instance.runtimeType == Node; | 778 bool appliesTo(instance, _) => instance.runtimeType == Node; |
| 779 getState(instance) => [instance.parent, instance.name, instance.children]; | 779 getState(instance) => [instance.parent, instance.name, instance.children]; |
| 780 create(state) => new Node(state[1]); | 780 create(state) => new Node(state[1]); |
| 781 void setState(Node node, state) { | 781 void setState(Node node, state) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 809 if (value == key) return each; | 809 if (value == key) return each; |
| 810 } | 810 } |
| 811 return null; | 811 return null; |
| 812 } | 812 } |
| 813 } | 813 } |
| 814 | 814 |
| 815 /** | 815 /** |
| 816 * Function used in an isolate to make sure that the output passes through | 816 * Function used in an isolate to make sure that the output passes through |
| 817 * isolate serialization properly. | 817 * isolate serialization properly. |
| 818 */ | 818 */ |
| 819 echo() { | 819 void echo() { |
| 820 port.receive((msg, reply) { | 820 port.receive((msg, reply) { |
| 821 reply.send(msg); | 821 reply.send(msg); |
| 822 }); | 822 }); |
| 823 } | 823 } |
| OLD | NEW |