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

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

Issue 12210151: Fix modification of map while iterating in SimpleJsonFormat (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes from review comments Created 7 years, 10 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
« no previous file with comments | « pkg/serialization/lib/src/format.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 ..addRuleFor(a1) 379 ..addRuleFor(a1)
380 ..addRuleFor(p1).configureForMaps(); 380 ..addRuleFor(p1).configureForMaps();
381 var p2 = writeAndReadBack(s, 381 var p2 = writeAndReadBack(s,
382 new SimpleJsonFormat(storeRoundTripInfo: true), p1); 382 new SimpleJsonFormat(storeRoundTripInfo: true), p1);
383 expect(p2.name, "Alice"); 383 expect(p2.name, "Alice");
384 var a2 = p2.address; 384 var a2 = p2.address;
385 expect(a2.street, "N 34th"); 385 expect(a2.street, "N 34th");
386 expect(a2.city, "Seattle"); 386 expect(a2.city, "Seattle");
387 }); 387 });
388 388
389 test("Straight JSON format, non-string key", () {
390 // This tests what happens if we have a key that's not a string. That's
391 // not allowed by json, so we don't actually turn it into a json string,
392 // but someone might reasonably convert to a json-able structure without
393 // going through the string representation.
394 var p1 = new Person()..name = 'Alice'..address = a1;
395 var s = new Serialization()
396 ..addRule(new PersonRuleReturningMapWithNonStringKey());
397 var p2 = writeAndReadBack(s,
398 new SimpleJsonFormat(storeRoundTripInfo: true), p1);
399 expect(p2.name, "Alice");
400 expect(p2.address.street, "N 34th");
401 });
402
389 test("Root is a Map", () { 403 test("Root is a Map", () {
390 // Note that we can't use the usual round-trip test because it has cycles. 404 // Note that we can't use the usual round-trip test because it has cycles.
391 var p1 = new Person()..name = 'Alice'..address = a1; 405 var p1 = new Person()..name = 'Alice'..address = a1;
392 // Use maps for one rule, lists for the other. 406 // Use maps for one rule, lists for the other.
393 var s = new Serialization() 407 var s = new Serialization()
394 ..addRuleFor(a1) 408 ..addRuleFor(a1)
395 ..addRuleFor(p1).configureForMaps(); 409 ..addRuleFor(p1).configureForMaps();
396 for (var eachFormat in formats) { 410 for (var eachFormat in formats) {
397 var w = s.newWriter(eachFormat); 411 var w = s.newWriter(eachFormat);
398 var output = w.write({"stuff" : p1}); 412 var output = w.write({"stuff" : p1});
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 /** A hard-coded rule for serializing Node instances. */ 711 /** A hard-coded rule for serializing Node instances. */
698 class NodeRule extends CustomRule { 712 class NodeRule extends CustomRule {
699 bool appliesTo(instance, _) => instance.runtimeType == Node; 713 bool appliesTo(instance, _) => instance.runtimeType == Node;
700 getState(instance) => [instance.parent, instance.name, instance.children]; 714 getState(instance) => [instance.parent, instance.name, instance.children];
701 create(state) => new Node(state[1]); 715 create(state) => new Node(state[1]);
702 setState(Node node, state) { 716 setState(Node node, state) {
703 node.parent = state[0]; 717 node.parent = state[0];
704 node.children = state[2]; 718 node.children = state[2];
705 } 719 }
706 } 720 }
721
722 /**
723 * This is a rather silly rule which stores the address data in a map,
724 * but inverts the keys and values, so we look up values and find the
725 * corresponding key. This will lead to maps that aren't allowed in JSON,
726 * and which have keys that need to be dereferenced.
727 */
728 class PersonRuleReturningMapWithNonStringKey extends CustomRule {
729 appliesTo(instance, _) => instance is Person;
730 getState(instance) {
731 return new Map()
732 ..[instance.name] = "name"
733 ..[instance.address] = "address";
734 }
735 create(state) => new Person();
736 setState(Person a, state) {
737 a.name = findValue("name", state);
738 a.address = findValue("address", state);
739 }
740 findValue(String key, Map state) {
741 var answer;
742 for (var each in state.keys) {
743 var value = state[each];
744 if (value == key) return each;
745 }
746 return null;
747 }
748 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/src/format.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698