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 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:serialization/serialization.dart'; | 8 import 'package:serialization/serialization.dart'; |
9 import 'package:serialization/src/serialization_helpers.dart'; | 9 import 'package:serialization/src/serialization_helpers.dart'; |
10 import 'package:serialization/src/mirrors_helpers.dart'; | 10 import 'package:serialization/src/mirrors_helpers.dart'; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 185 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
186 n1.children = [n2, n3]; | 186 n1.children = [n2, n3]; |
187 n2.parent = n1; | 187 n2.parent = n1; |
188 n3.parent = n1; | 188 n3.parent = n1; |
189 | 189 |
190 test('Trace a cyclical structure', () { | 190 test('Trace a cyclical structure', () { |
191 var s = new Serialization(); | 191 var s = new Serialization(); |
192 var trace = new Trace(new Writer(s)); | 192 var trace = new Trace(new Writer(s)); |
193 trace.writer.trace = trace; | 193 trace.writer.trace = trace; |
194 trace.trace(n1); | 194 trace.trace(n1); |
195 var all = trace.writer.references.keys; | 195 var all = trace.writer.references.keys.toSet(); |
196 expect(all.length, 4); | 196 expect(all.length, 4); |
197 expect(all.contains(n1), isTrue); | 197 expect(all.contains(n1), isTrue); |
198 expect(all.contains(n2), isTrue); | 198 expect(all.contains(n2), isTrue); |
199 expect(all.contains(n3), isTrue); | 199 expect(all.contains(n3), isTrue); |
200 expect(all.contains(n1.children), isTrue); | 200 expect(all.contains(n1.children), isTrue); |
201 }); | 201 }); |
202 | 202 |
203 test('Flatten references in a cyclical structure', () { | 203 test('Flatten references in a cyclical structure', () { |
204 var s = new Serialization(); | 204 var s = new Serialization(); |
205 var w = new Writer(s); | 205 var w = new Writer(s); |
206 w.trace = new Trace(w); | 206 w.trace = new Trace(w); |
207 w.write(n1); | 207 w.write(n1); |
208 expect(w.states.length, 4); // prims, lists, essential lists, basic | 208 expect(w.states.length, 4); // prims, lists, essential lists, basic |
209 var children = 0, name = 1, parent = 2; | 209 var children = 0, name = 1, parent = 2; |
210 List rootNode = w.states[3].filter((x) => x[name] == "1"); | 210 List rootNode = w.states[3].where((x) => x[name] == "1").toList(); |
211 rootNode = rootNode.first; | 211 rootNode = rootNode.first; |
212 expect(rootNode[parent], isNull); | 212 expect(rootNode[parent], isNull); |
213 var list = w.states[1].first; | 213 var list = w.states[1].first; |
214 expect(w.stateForReference(rootNode[children]), list); | 214 expect(w.stateForReference(rootNode[children]), list); |
215 var parentNode = w.stateForReference(list[0])[parent]; | 215 var parentNode = w.stateForReference(list[0])[parent]; |
216 expect(w.stateForReference(parentNode), rootNode); | 216 expect(w.stateForReference(parentNode), rootNode); |
217 }); | 217 }); |
218 | 218 |
219 test('round-trip', () { | 219 test('round-trip', () { |
220 runRoundTripTest(nodeSerializerReflective); | 220 runRoundTripTest(nodeSerializerReflective); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 expect(m2.name, "2"); | 483 expect(m2.name, "2"); |
484 expect(m3.name, "3"); | 484 expect(m3.name, "3"); |
485 expect(m2.parent, m1); | 485 expect(m2.parent, m1); |
486 expect(m3.parent, m1); | 486 expect(m3.parent, m1); |
487 expect(m1.parent, isNull); | 487 expect(m1.parent, isNull); |
488 } | 488 } |
489 | 489 |
490 /** Extract the state from [object] using the rules in [s] and return it. */ | 490 /** Extract the state from [object] using the rules in [s] and return it. */ |
491 states(object, Serialization s) { | 491 states(object, Serialization s) { |
492 var rules = s.rulesFor(object, null); | 492 var rules = s.rulesFor(object, null); |
493 return rules.map((x) => x.extractState(object, doNothing)); | 493 return rules.mappedBy((x) => x.extractState(object, doNothing)).toList(); |
494 } | 494 } |
495 | 495 |
496 /** A hard-coded rule for serializing Node instances. */ | 496 /** A hard-coded rule for serializing Node instances. */ |
497 class NodeRule extends CustomRule { | 497 class NodeRule extends CustomRule { |
498 bool appliesTo(instance, _) => instance is Node; | 498 bool appliesTo(instance, _) => instance is Node; |
499 getState(instance) => [instance.parent, instance.name, instance.children]; | 499 getState(instance) => [instance.parent, instance.name, instance.children]; |
500 create(state) => new Node(state[1]); | 500 create(state) => new Node(state[1]); |
501 setState(Node node, state) { | 501 setState(Node node, state) { |
502 node.parent = state[0]; | 502 node.parent = state[0]; |
503 node.children = state[2]; | 503 node.children = state[2]; |
504 } | 504 } |
505 } | 505 } |
OLD | NEW |