Index: pkg/serialization/test/serialization_test.dart |
diff --git a/pkg/serialization/test/serialization_test.dart b/pkg/serialization/test/serialization_test.dart |
index 206651bca61c89222eb29440daf1e192540710e7..bfc700101074286202c6846804373fb05aba42d1 100644 |
--- a/pkg/serialization/test/serialization_test.dart |
+++ b/pkg/serialization/test/serialization_test.dart |
@@ -4,6 +4,7 @@ |
library serialization_test; |
+import 'dart:json' as json; |
import 'package:unittest/unittest.dart'; |
import 'package:serialization/serialization.dart'; |
import 'package:serialization/src/serialization_helpers.dart'; |
@@ -236,6 +237,10 @@ main() { |
runRoundTripTest(nodeSerializerUsingMaps); |
}); |
+ test('round-trip, flat format, using maps', () { |
+ runRoundTripTestFlat(nodeSerializerUsingMaps); |
+ }); |
+ |
test('round-trip with Node CustomRule', () { |
runRoundTripTestFlat(nodeSerializerCustom); |
}); |
@@ -309,7 +314,7 @@ main() { |
constructor: 'withData', |
constructorFields: ["street", "Kirkland", "WA", "98103"], |
fields: []); |
- var out = s.write(a1); |
+ String out = s.write(a1); |
var newAddress = s.read(out); |
expect(newAddress.street, a1.street); |
expect(newAddress.city, "Kirkland"); |
@@ -317,6 +322,64 @@ main() { |
expect(newAddress.zip, "98103"); |
}); |
+// test("Straight JSON format", () { |
Jennifer Messerly
2013/01/11 02:21:53
what's the status of this test?
Alan Knight
2013/01/11 19:18:11
Oops. Restored.
|
+// var s = new Serialization(); |
+// var writer = s.newWriter(new SimpleJSONFormat()); |
+// var out = writer.write(a1); |
+// var reconstituted = JSON.parse(out); |
+// expect(reconstituted.length, 4); |
+// expect(reconstituted[0], "Seattle"); |
+// }); |
+ |
+ test("Straight JSON format, nested objects", () { |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ var s = new Serialization() |
+ ..addRuleFor(a1).configureForMaps() |
+ ..addRuleFor(p1).configureForMaps(); |
+ var writer = s.newWriter(new SimpleJSONFormat(true)); |
+ var out = writer.write(p1); |
+ var reconstituted = json.parse(out); |
Jennifer Messerly
2013/01/11 02:21:53
it would be interesting to see the full JSON map h
Alan Knight
2013/01/11 19:18:11
Nice. Done.
|
+ expect(reconstituted.length, 5); |
+ expect(reconstituted["name"], "Alice"); |
+ var address = reconstituted["address"]; |
+ expect(address.length, 5); |
+ expect(address["street"], "N 34th"); |
+ }); |
+ |
+ test("Straight JSON format, round-trip", () { |
+ // Note that we can't use the usual round-trip test because it has cycles. |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ // Use maps for one rule, lists for the other. |
+ var s = new Serialization() |
+ ..addRuleFor(a1) |
+ ..addRuleFor(p1).configureForMaps(); |
+ var writer = s.newWriter(new SimpleJSONFormat(true)); |
+ var out = writer.write(p1); |
+ var reader = s.newReader(new SimpleJSONFormat(true)); |
+ var p2 = reader.read(out); |
+ expect(p2.name, "Alice"); |
+ var a2 = p2.address; |
+ expect(a2.street, "N 34th"); |
+ expect(a2.city, "Seattle"); |
+ }); |
+ |
+ test("Straight JSON format, round-trip with named objects", () { |
+ // Note that we can't use the usual round-trip test because it has cycles. |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ // Use maps for one rule, lists for the other. |
+ var s = new Serialization() |
+ ..addRule(new NamedObjectRule()) |
+ ..addRuleFor(a1) |
+ ..addRuleFor(p1).configureForMaps() |
+ ..namedObjects["foo"] = a1; |
+ var writer = s.newWriter(new SimpleJSONFormat(true)); |
+ var out = writer.write(p1); |
+ var reader = s.newReader(new SimpleJSONFormat(true)); |
+ var p2 = reader.read(out, {"foo" : 12}); |
+ expect(p2.name, "Alice"); |
+ var a2 = p2.address; |
+ expect(a2, 12); |
+ }); |
} |
/****************************************************************************** |
@@ -355,9 +418,9 @@ Serialization metaSerialization() { |
* reader. |
*/ |
readBackSimple(Serialization s, object, Reader reader) { |
- var rule = s.rulesFor(object, null)[0]; |
+ var rule = s.rulesFor(object, null).first; |
reader.inflateForRule(rule); |
- var list2 = reader.allObjectsForRule(rule)[0]; |
+ var list2 = reader.allObjectsForRule(rule).first; |
return list2; |
} |
@@ -470,11 +533,11 @@ runRoundTripTestFlat(serializerSetUp) { |
n2.parent = n1; |
n3.parent = n1; |
var s = serializerSetUp(n1); |
- var output = s.writeFlat(n2); |
+ var output = s.write(n2, new SimpleFlatFormat()); |
expect(output is List, isTrue); |
var s2 = serializerSetUp(n1); |
- var reader = new Reader(s2); |
- var m2 = reader.readFlat(output); |
+ var reader = new Reader(s2, new SimpleFlatFormat()); |
+ var m2 = reader.read(output); |
var m1 = m2.parent; |
expect(m1 is Node, isTrue); |
var children = m1.children; |
@@ -495,7 +558,7 @@ states(object, Serialization s) { |
/** A hard-coded rule for serializing Node instances. */ |
class NodeRule extends CustomRule { |
- bool appliesTo(instance, _) => instance is Node; |
+ bool appliesTo(instance, _) => instance.runtimeType == Node; |
getState(instance) => [instance.parent, instance.name, instance.children]; |
create(state) => new Node(state[1]); |
setState(Node node, state) { |