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'; |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 test('Slightly further with a simple object', () { | 42 test('Slightly further with a simple object', () { |
43 var p1 = new Person()..name = 'Alice'..address = a1; | 43 var p1 = new Person()..name = 'Alice'..address = a1; |
44 var s = new Serialization() | 44 var s = new Serialization() |
45 ..addRuleFor(p1).configureForMaps() | 45 ..addRuleFor(p1).configureForMaps() |
46 ..addRuleFor(a1).configureForMaps(); | 46 ..addRuleFor(a1).configureForMaps(); |
47 // TODO(alanknight): Need a better API for getting to flat state without | 47 // TODO(alanknight): Need a better API for getting to flat state without |
48 // actually writing. | 48 // actually writing. |
49 var w = new Writer(s); | 49 var w = new Writer(s); |
50 w.write(p1); | 50 w.write(p1); |
51 var personRule = s.rules.firstMatching( | 51 var personRule = s.rules.firstWhere( |
52 (x) => x is BasicRule && x.type == reflect(p1).type); | 52 (x) => x is BasicRule && x.type == reflect(p1).type); |
53 var flatPerson = w.states[personRule.number].first; | 53 var flatPerson = w.states[personRule.number].first; |
54 var primStates = w.states.first; | 54 var primStates = w.states.first; |
55 expect(primStates.isEmpty, true); | 55 expect(primStates.isEmpty, true); |
56 expect(flatPerson["name"], "Alice"); | 56 expect(flatPerson["name"], "Alice"); |
57 var ref = flatPerson["address"]; | 57 var ref = flatPerson["address"]; |
58 expect(ref is Reference, true); | 58 expect(ref is Reference, true); |
59 var addressRule = s.rules.firstMatching( | 59 var addressRule = s.rules.firstWhere( |
60 (x) => x is BasicRule && x.type == reflect(a1).type); | 60 (x) => x is BasicRule && x.type == reflect(a1).type); |
61 expect(ref.ruleNumber, addressRule.number); | 61 expect(ref.ruleNumber, addressRule.number); |
62 expect(ref.objectNumber, 0); | 62 expect(ref.objectNumber, 0); |
63 expect(w.states[addressRule.number].first['street'], 'N 34th'); | 63 expect(w.states[addressRule.number].first['street'], 'N 34th'); |
64 }); | 64 }); |
65 | 65 |
66 test('exclude fields', () { | 66 test('exclude fields', () { |
67 var s = new Serialization() | 67 var s = new Serialization() |
68 ..addRuleFor(a1, | 68 ..addRuleFor(a1, |
69 excludeFields: ['state', 'zip']).configureForMaps(); | 69 excludeFields: ['state', 'zip']).configureForMaps(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 expect(all.contains(n1.children), isTrue); | 206 expect(all.contains(n1.children), isTrue); |
207 }); | 207 }); |
208 | 208 |
209 test('Flatten references in a cyclical structure', () { | 209 test('Flatten references in a cyclical structure', () { |
210 var s = new Serialization(); | 210 var s = new Serialization(); |
211 var w = new Writer(s); | 211 var w = new Writer(s); |
212 w.trace = new Trace(w); | 212 w.trace = new Trace(w); |
213 w.write(n1); | 213 w.write(n1); |
214 expect(w.states.length, 5); // prims, lists, essential lists, basic | 214 expect(w.states.length, 5); // prims, lists, essential lists, basic |
215 var children = 0, name = 1, parent = 2; | 215 var children = 0, name = 1, parent = 2; |
216 var nodeRule = s.rules.firstMatching((x) => x is BasicRule); | 216 var nodeRule = s.rules.firstWhere((x) => x is BasicRule); |
217 List rootNode = w.states[nodeRule.number].where( | 217 List rootNode = w.states[nodeRule.number].where( |
218 (x) => x[name] == "1").toList(); | 218 (x) => x[name] == "1").toList(); |
219 rootNode = rootNode.first; | 219 rootNode = rootNode.first; |
220 expect(rootNode[parent], isNull); | 220 expect(rootNode[parent], isNull); |
221 var list = w.states[1].first; | 221 var list = w.states[1].first; |
222 expect(w.stateForReference(rootNode[children]), list); | 222 expect(w.stateForReference(rootNode[children]), list); |
223 var parentNode = w.stateForReference(list[0])[parent]; | 223 var parentNode = w.stateForReference(list[0])[parent]; |
224 expect(w.stateForReference(parentNode), rootNode); | 224 expect(w.stateForReference(parentNode), rootNode); |
225 }); | 225 }); |
226 | 226 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 var p1 = new Person()..name = 'Alice'..address = a1; | 469 var p1 = new Person()..name = 'Alice'..address = a1; |
470 var data = new Map(); | 470 var data = new Map(); |
471 data["simple data"] = 1; | 471 data["simple data"] = 1; |
472 data[p1] = a1; | 472 data[p1] = a1; |
473 data[a1] = p1; | 473 data[a1] = p1; |
474 for (var eachFormat in formats) { | 474 for (var eachFormat in formats) { |
475 var output = s.write(data, eachFormat); | 475 var output = s.write(data, eachFormat); |
476 var reader = s.newReader(eachFormat); | 476 var reader = s.newReader(eachFormat); |
477 var input = reader.read(output); | 477 var input = reader.read(output); |
478 expect(input["simple data"], data["simple data"]); | 478 expect(input["simple data"], data["simple data"]); |
479 var p2 = input.keys.firstMatching((x) => x is Person); | 479 var p2 = input.keys.firstWhere((x) => x is Person); |
480 var a2 = input.keys.firstMatching((x) => x is Address); | 480 var a2 = input.keys.firstWhere((x) => x is Address); |
481 if (eachFormat is SimpleJsonFormat) { | 481 if (eachFormat is SimpleJsonFormat) { |
482 // JSON doesn't handle cycles, so these won't be identical. | 482 // JSON doesn't handle cycles, so these won't be identical. |
483 expect(input[p2] is Address, isTrue); | 483 expect(input[p2] is Address, isTrue); |
484 expect(input[a2] is Person, isTrue); | 484 expect(input[a2] is Person, isTrue); |
485 var a3 = input[p2]; | 485 var a3 = input[p2]; |
486 expect(a3.city, a2.city); | 486 expect(a3.city, a2.city); |
487 expect(a3.state, a2.state); | 487 expect(a3.state, a2.state); |
488 expect(a3.state, a2.state); | 488 expect(a3.state, a2.state); |
489 var p3 = input[a2]; | 489 var p3 = input[a2]; |
490 expect(p3.name, p2.name); | 490 expect(p3.name, p2.name); |
491 expect(p3.rank, p2.rank); | 491 expect(p3.rank, p2.rank); |
492 expect(p3.address.city, a2.city); | 492 expect(p3.address.city, a2.city); |
493 } else { | 493 } else { |
494 expect(input[p2], same(a2)); | 494 expect(input[p2], same(a2)); |
495 expect(input[a2], same(p2)); | 495 expect(input[a2], same(p2)); |
496 } | 496 } |
497 } | 497 } |
498 }); | 498 }); |
499 | 499 |
500 test("Map with string keys stays that way", () { | 500 test("Map with string keys stays that way", () { |
501 var s = new Serialization()..addRuleFor(new Person()); | 501 var s = new Serialization()..addRuleFor(new Person()); |
502 var data = {"abc" : 1, "def" : "ghi"}; | 502 var data = {"abc" : 1, "def" : "ghi"}; |
503 data["person"] = new Person()..name = "Foo"; | 503 data["person"] = new Person()..name = "Foo"; |
504 var output = s.write(data, new SimpleMapFormat()); | 504 var output = s.write(data, new SimpleMapFormat()); |
505 var mapRule = s.rules.firstMatching((x) => x is MapRule); | 505 var mapRule = s.rules.firstWhere((x) => x is MapRule); |
506 var map = output["data"][mapRule.number][0]; | 506 var map = output["data"][mapRule.number][0]; |
507 expect(map is Map, isTrue); | 507 expect(map is Map, isTrue); |
508 expect(map["abc"], 1); | 508 expect(map["abc"], 1); |
509 expect(map["def"], "ghi"); | 509 expect(map["def"], "ghi"); |
510 expect(new Reader(s).asReference(map["person"]) is Reference, isTrue); | 510 expect(new Reader(s).asReference(map["person"]) is Reference, isTrue); |
511 }); | 511 }); |
512 | 512 |
513 } | 513 } |
514 | 514 |
515 /****************************************************************************** | 515 /****************************************************************************** |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 } | 739 } |
740 findValue(String key, Map state) { | 740 findValue(String key, Map state) { |
741 var answer; | 741 var answer; |
742 for (var each in state.keys) { | 742 for (var each in state.keys) { |
743 var value = state[each]; | 743 var value = state[each]; |
744 if (value == key) return each; | 744 if (value == key) return each; |
745 } | 745 } |
746 return null; | 746 return null; |
747 } | 747 } |
748 } | 748 } |
OLD | NEW |