1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Dec 20, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module spec.serialization.OverrideSerializer;
8 
9 import dspec.Dsl;
10 
11 import mambo.core._;
12 import mambo.serialization.Serializer;
13 import mambo.serialization.archives.XmlArchive;
14 import spec.serialization.Util;
15 
16 Serializer serializer;
17 XmlArchive!(char) archive;
18 
19 class Base
20 {
21 	int x;
22 }
23 
24 class Foo : Base
25 {
26 	private int a_;
27 	private int b_;
28 	
29 	int a () { return a_; }
30 	int a (int a) { return a_ = a; }
31 	int b () { return b_; }
32 	int b (int b) { return b_ = b; }
33 }
34 
35 Foo foo;
36 int i;
37 
38 void toData (Foo foo, Serializer serializer, Serializer.Data key)
39 {
40 	i += 4;
41 }
42 
43 void fromData (ref Foo foo, Serializer serializer, Serializer.Data key)
44 {
45 	i += 4;
46 }
47 
48 void overrideToData (Foo foo, Serializer serializer, Serializer.Data key)
49 {
50 	i++;
51 	serializer.serialize(foo.a, "a");
52 	serializer.serialize(foo.b, "b");
53 	serializer.serializeBase(foo);
54 }
55 
56 void overrideFromData (ref Foo foo, Serializer serializer, Serializer.Data key)
57 {
58 	i++;
59 	foo.a = serializer.deserialize!(int)("a");
60 	foo.b = serializer.deserialize!(int)("b");
61 	serializer.deserializeBase(foo);
62 }
63 
64 unittest
65 {
66 	archive = new XmlArchive!(char);
67 	serializer = new Serializer(archive);
68 	
69 	foo = new Foo;
70 	foo.a = 3;
71 	foo.b = 4;
72 	foo.x = 5;
73 	i = 3;
74 
75 	describe! "serialize object using an overridden serializer" in {
76 		it! "should return a custom serialized object" in {
77 			Serializer.registerSerializer!(Foo)(&toData);
78 			Serializer.registerDeserializer!(Foo)(&fromData);
79 			
80 			serializer.overrideSerializer!(Foo)(&overrideToData);
81 			serializer.overrideDeserializer!(Foo)(&overrideFromData);
82 			
83 			serializer.serialize(foo);
84 
85 			assert(archive.data().containsDefaultXmlContent());
86 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.OverrideSerializer.Foo" type="spec.serialization.OverrideSerializer.Foo" key="0" id="0"`));
87 			assert(archive.data().containsXmlTag("int", `key="a" id="1"`, "3"));
88 			assert(archive.data().containsXmlTag("int", `key="b" id="2"`, "4"));
89 			
90 			assert(archive.data().containsXmlTag("base", `type="spec.serialization.OverrideSerializer.Base" key="1" id="3"`));
91 			assert(archive.data().containsXmlTag("int", `key="x" id="4"`, "5"));
92 			
93 			assert(i == 4);
94 		};
95 	};
96 	
97 	describe! "deserialize object using an overridden deserializer" in {
98 		it! "short return a custom deserialized object equal to the original object" in {
99 			auto f = serializer.deserialize!(Foo)(archive.untypedData);
100 
101 			assert(foo.a == f.a);
102 			assert(foo.b == f.b);
103 			assert(foo.x == f.x);
104 
105 			assert(i == 5);
106 			
107 			Serializer.resetSerializers;
108 		};
109 	};
110 }