| 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 '../../unittest/lib/unittest.dart'; | 7 import '../../unittest/lib/unittest.dart'; |
| 8 import '../lib/serialization.dart'; | 8 import '../lib/serialization.dart'; |
| 9 import '../lib/src/serialization_helpers.dart'; | 9 import '../lib/src/serialization_helpers.dart'; |
| 10 import '../lib/src/mirrors_helpers.dart'; | 10 import '../lib/src/mirrors_helpers.dart'; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 runRoundTripTest((x) => s2); | 258 runRoundTripTest((x) => s2); |
| 259 }); | 259 }); |
| 260 | 260 |
| 261 test("Verify we're not serializing lists twice if they're essential", () { | 261 test("Verify we're not serializing lists twice if they're essential", () { |
| 262 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); | 262 Node n1 = new Node("1"), n2 = new Node("2"), n3 = new Node("3"); |
| 263 n1.children = [n2, n3]; | 263 n1.children = [n2, n3]; |
| 264 n2.parent = n1; | 264 n2.parent = n1; |
| 265 n3.parent = n1; | 265 n3.parent = n1; |
| 266 var s = new Serialization() | 266 var s = new Serialization() |
| 267 ..addRuleFor(n1, constructorFields: ["name"]). | 267 ..addRuleFor(n1, constructorFields: ["name"]). |
| 268 specialTreatmentFor("children", (parent, child) => | 268 setFieldWith("children", (parent, child) => |
| 269 parent.reflectee.children = child); | 269 parent.reflectee.children = child); |
| 270 var w = new Writer(s); | 270 var w = new Writer(s); |
| 271 w.write(n1); | 271 w.write(n1); |
| 272 expect(w.rules[2] is ListRuleEssential, isTrue); | 272 expect(w.rules[2] is ListRuleEssential, isTrue); |
| 273 expect(w.rules[1] is ListRule, isTrue); | 273 expect(w.rules[1] is ListRule, isTrue); |
| 274 expect(w.states[1].length, 0); | 274 expect(w.states[1].length, 0); |
| 275 expect(w.states[2].length, 1); | 275 expect(w.states[2].length, 1); |
| 276 s = new Serialization() | 276 s = new Serialization() |
| 277 ..addRuleFor(n1, constructorFields: ["name"]); | 277 ..addRuleFor(n1, constructorFields: ["name"]); |
| 278 w = new Writer(s); | 278 w = new Writer(s); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 295 var r = new Reader(s); | 295 var r = new Reader(s); |
| 296 var m1 = r.read(w.write(n1)); | 296 var m1 = r.read(w.write(n1)); |
| 297 var m2 = m1.children.first; | 297 var m2 = m1.children.first; |
| 298 var m3 = m1.children.last; | 298 var m3 = m1.children.last; |
| 299 expect(m1, m2); | 299 expect(m1, m2); |
| 300 expect(identical(m1, m2), isFalse); | 300 expect(identical(m1, m2), isFalse); |
| 301 expect(m1 == m3, isFalse); | 301 expect(m1 == m3, isFalse); |
| 302 expect(identical(m2.parent, m3.parent), isTrue); | 302 expect(identical(m2.parent, m3.parent), isTrue); |
| 303 }); | 303 }); |
| 304 | 304 |
| 305 test("Constant values as fields", () { |
| 306 var s = new Serialization() |
| 307 ..selfDescribing = false |
| 308 ..addRuleFor(a1, |
| 309 constructor: 'with', |
| 310 constructorFields: ["street", "Kirkland", "WA", "98103"], |
| 311 fields: []); |
| 312 var out = s.write(a1); |
| 313 var newAddress = s.read(out); |
| 314 expect(newAddress.street, a1.street); |
| 315 expect(newAddress.city, "Kirkland"); |
| 316 expect(newAddress.state, "WA"); |
| 317 expect(newAddress.zip, "98103"); |
| 318 }); |
| 319 |
| 305 } | 320 } |
| 306 | 321 |
| 307 /****************************************************************************** | 322 /****************************************************************************** |
| 308 * The end of the tests and the beginning of various helper functions to make | 323 * The end of the tests and the beginning of various helper functions to make |
| 309 * it easier to write the repetitive sections. | 324 * it easier to write the repetitive sections. |
| 310 ******************************************************************************/ | 325 ******************************************************************************/ |
| 311 | 326 |
| 312 /** Create a Serialization for serializing Serializations. */ | 327 /** Create a Serialization for serializing Serializations. */ |
| 313 Serialization metaSerialization() { | 328 Serialization metaSerialization() { |
| 314 // Make some bogus rule instances so we have something to feed rule creation | 329 // Make some bogus rule instances so we have something to feed rule creation |
| 315 // and get their types. If only we had class literals implemented... | 330 // and get their types. If only we had class literals implemented... |
| 316 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); | 331 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); |
| 317 | 332 |
| 318 var meta = new Serialization() | 333 var meta = new Serialization() |
| 319 ..selfDescribing = false | 334 ..selfDescribing = false |
| 320 ..addRuleFor(new ListRule()) | 335 ..addRuleFor(new ListRule()) |
| 321 ..addRuleFor(new PrimitiveRule()) | 336 ..addRuleFor(new PrimitiveRule()) |
| 322 // TODO(alanknight): Handle CustomRule as well. | 337 // TODO(alanknight): Handle CustomRule as well. |
| 323 // Note that we're passing in a constant for one of the fields. | 338 // Note that we're passing in a constant for one of the fields. |
| 324 ..addRuleFor(basicRule, | 339 ..addRuleFor(basicRule, |
| 325 constructorFields: ['type', | 340 constructorFields: ['type', |
| 326 'constructorName', | 341 'constructorName', |
| 327 'constructorFields', 'regularFields', []], | 342 'constructorFields', 'regularFields', []], |
| 328 fields: []) | 343 fields: []) |
| 329 ..addRuleFor(new Serialization()).specialTreatmentFor('rules', | 344 ..addRuleFor(new Serialization()).setFieldWith('rules', |
| 330 (InstanceMirror s, List rules) { | 345 (InstanceMirror s, List rules) { |
| 331 rules.forEach((x) => s.reflectee.addRule(x)); | 346 rules.forEach((x) => s.reflectee.addRule(x)); |
| 332 }) | 347 }) |
| 333 ..addRule(new NamedObjectRule()) | 348 ..addRule(new NamedObjectRule()) |
| 334 ..addRule(new MirrorRule()); | 349 ..addRule(new MirrorRule()); |
| 335 return meta; | 350 return meta; |
| 336 } | 351 } |
| 337 | 352 |
| 338 /** | 353 /** |
| 339 * Read back a simple object, assumed to be the only one of its class in the | 354 * Read back a simple object, assumed to be the only one of its class in the |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 /** A hard-coded rule for serializing Node instances. */ | 496 /** A hard-coded rule for serializing Node instances. */ |
| 482 class NodeRule extends CustomRule { | 497 class NodeRule extends CustomRule { |
| 483 bool appliesTo(instance, _) => instance is Node; | 498 bool appliesTo(instance, _) => instance is Node; |
| 484 getState(instance) => [instance.parent, instance.name, instance.children]; | 499 getState(instance) => [instance.parent, instance.name, instance.children]; |
| 485 create(state) => new Node(state[1]); | 500 create(state) => new Node(state[1]); |
| 486 setState(Node node, state) { | 501 setState(Node node, state) { |
| 487 node.parent = state[0]; | 502 node.parent = state[0]; |
| 488 node.children = state[2]; | 503 node.children = state[2]; |
| 489 } | 504 } |
| 490 } | 505 } |
| OLD | NEW |