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

Unified Diff: pkg/serialization/test/serialization_test.dart

Issue 11553012: Better ability to have hand-written custom rules and various cleanups. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years 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 side-by-side diff with in-line comments
Download patch
Index: pkg/serialization/test/serialization_test.dart
diff --git a/pkg/serialization/test/serialization_test.dart b/pkg/serialization/test/serialization_test.dart
index 345500aafab7ae8a33b4caabaf8806c1515080b1..3aa7011e02db509d089190642b056b26e068e11f 100644
--- a/pkg/serialization/test/serialization_test.dart
+++ b/pkg/serialization/test/serialization_test.dart
@@ -220,7 +220,7 @@ main() {
runRoundTripTest(nodeSerializerReflective);
});
- test('round-trip hard-coded', () {
+ test('round-trip ClosureRule', () {
runRoundTripTest(nodeSerializerNonReflective);
});
@@ -236,6 +236,14 @@ main() {
runRoundTripTest(nodeSerializerUsingMaps);
});
+ test('round-trip with Node CustomRule', () {
+ runRoundTripTestFlat(nodeSerializerCustom);
+ });
+
+ test('round-trip with Node CustomRule, to maps', () {
+ runRoundTripTest(nodeSerializerCustom);
+ });
+
test('eating your own tail', () {
// Create a meta-serializer, that serializes serializations, then
// use it to serialize a basic serialization, then run a test on the
@@ -246,7 +254,7 @@ main() {
var meta = metaSerialization();
var serialized = meta.write(s);
var s2 = new Reader(meta)
- .readOne(serialized, {"Node" : reflect(new Node('')).type});
+ .read(serialized, {"Node" : reflect(new Node('')).type});
runRoundTripTest((x) => s2);
});
@@ -285,7 +293,7 @@ main() {
..addRuleFor(n1, constructorFields: ["name"]);
var w = new Writer(s);
var r = new Reader(s);
- var m1 = r.read(w.write(n1)).first;
+ var m1 = r.read(w.write(n1));
var m2 = m1.children.first;
var m3 = m1.children.last;
expect(m1, m2);
@@ -305,14 +313,13 @@ main() {
Serialization metaSerialization() {
// Make some bogus rule instances so we have something to feed rule creation
// and get their types. If only we had class literals implemented...
- var closureRule = new ClosureToMapRule.stub([].runtimeType);
var basicRule = new BasicRule(reflect(null).type, '', [], [], []);
var meta = new Serialization()
..selfDescribing = false
..addRuleFor(new ListRule())
..addRuleFor(new PrimitiveRule())
- // TODO(alanknight): Handle the ClosureToMapRule as well.
+ // TODO(alanknight): Handle CustomRule as well.
// Note that we're passing in a constant for one of the fields.
..addRuleFor(basicRule,
constructorFields: ['typeWrapped',
@@ -323,7 +330,8 @@ Serialization metaSerialization() {
(InstanceMirror s, List rules) {
rules.forEach((x) => s.reflectee.addRule(x));
})
- ..addRule(new ClassMirrorRule());
+ ..addRule(new NamedObjectRule())
+ ..addRule(new MirrorRule());
return meta;
}
@@ -332,7 +340,7 @@ Serialization metaSerialization() {
* reader.
*/
readBackSimple(Serialization s, object, Reader reader) {
- var rule = s.rulesFor(object)[0];
+ var rule = s.rulesFor(object, null)[0];
reader.inflateForRule(rule);
var list2 = reader.allObjectsForRule(rule)[0];
return list2;
@@ -354,7 +362,7 @@ Reader setUpReader(aSerialization, sampleData) {
Serialization nodeSerializerReflective(Node n) {
return new Serialization()
..addRuleFor(n, constructorFields: ["name"])
- ..externalObjects['Node'] = reflect(new Node('')).type;
+ ..namedObjects['Node'] = reflect(new Node('')).type;
}
/**
@@ -364,7 +372,16 @@ Serialization nodeSerializerReflective(Node n) {
Serialization nodeSerializerUsingMaps(Node n) {
return new Serialization()
..addRuleFor(n, constructorFields: ["name"]).configureForMaps()
- ..externalObjects['Node'] = reflect(new Node('')).type;
+ ..namedObjects['Node'] = reflect(new Node('')).type;
+}
+
+/**
+ * Return a serialization for Node objects but using Maps for the internal
+ * representation rather than lists.
+ */
+Serialization nodeSerializerCustom(Node n) {
+ return new Serialization()
+ ..addRule(new NodeRule());
}
/**
@@ -381,18 +398,18 @@ Serialization nodeSerializerWithEssentialParent(Node n) {
constructor: "parentEssential",
constructorFields: ["parent"])
..addDefaultRules()
- ..externalObjects['Node'] = reflect(new Node('')).type
+ ..namedObjects['Node'] = reflect(new Node('')).type
..selfDescribing = false;
return s;
}
/** Return a serialization for Node objects using a ClosureToMapRule. */
Serialization nodeSerializerNonReflective(Node n) {
- var rule = new ClosureToMapRule(
+ var rule = new ClosureRule(
n.runtimeType,
(o) => {"name" : o.name, "children" : o.children, "parent" : o.parent},
(map) => new Node(map["name"]),
- (map, object) {
+ (object, map) {
object
..children = map["children"]
..parent = map["parent"];
@@ -415,7 +432,7 @@ runRoundTripTest(Function serializerSetUp) {
var output = s.write(n2);
var s2 = serializerSetUp(n1);
var reader = new Reader(s2);
- var m2 = reader.readOne(output);
+ var m2 = reader.read(output);
var m1 = m2.parent;
expect(m1 is Node, isTrue);
var children = m1.children;
@@ -442,7 +459,7 @@ runRoundTripTestFlat(serializerSetUp) {
expect(output is List, isTrue);
var s2 = serializerSetUp(n1);
var reader = new Reader(s2);
- var m2 = reader.readFlat(output).first;
+ var m2 = reader.readFlat(output);
var m1 = m2.parent;
expect(m1 is Node, isTrue);
var children = m1.children;
@@ -456,7 +473,18 @@ runRoundTripTestFlat(serializerSetUp) {
}
/** Extract the state from [object] using the rules in [s] and return it. */
-states(Object object, Serialization s) {
- var rules = s.rulesFor(object);
+states(object, Serialization s) {
+ var rules = s.rulesFor(object, null);
return rules.map((x) => x.extractState(object, doNothing));
+}
+
+/** A hard-coded rule for serializing Node instances. */
+class NodeRule extends CustomRule {
+ bool appliesTo(instance, _) => instance is Node;
+ getState(instance) => [instance.parent, instance.name, instance.children];
+ create(state) => new Node(state[1]);
+ setState(Node node, state) {
+ node.parent = state[0];
+ node.children = state[2];
+ }
}

Powered by Google App Engine
This is Rietveld 408576698