1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 17, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module spec.serialization.Custom;
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 Foo
20 {
21 	int a;
22 	int b;
23 	
24 	void toData (Serializer serializer, Serializer.Data key)
25 	{
26 		i++;
27 		serializer.serialize(a, "x");
28 	}
29 
30 	void fromData (Serializer serializer, Serializer.Data key)
31 	{
32 		i++;
33 		a = serializer.deserialize!(int)("x");
34 	}
35 }
36 
37 class WithString
38 {
39 	string b;
40 
41 	void toData (Serializer serializer, Serializer.Data key)
42 	{
43 		j++;
44 		serializer.serialize(b, "y");
45 	}
46 
47 	void fromData (Serializer serializer, Serializer.Data key)
48 	{
49 		j++;
50 		b = serializer.deserialize!(string)("y");
51 	}
52 }
53 
54 Foo foo;
55 int i;
56 
57 WithString withString;
58 int j;
59 
60 unittest
61 {
62 	archive = new XmlArchive!(char);
63 	serializer = new Serializer(archive);
64 	
65 	foo = new Foo;
66 	foo.a = 3;
67 	foo.b = 4;
68 	i = 3;
69 
70 	withString = new WithString;
71 	withString.b = "a string";
72 	j = 3;
73 
74 	describe! "serialize object using custom serialization methods" in {
75 		it! "should return a custom serialized object" in {
76 			serializer.serialize(foo);
77 
78 			assert(archive.data().containsDefaultXmlContent());
79 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Custom.Foo" type="spec.serialization.Custom.Foo" key="0" id="0"`));
80 			assert(archive.data().containsXmlTag("int", `key="x" id="1"`));
81 			
82 			assert(i == 4);
83 		};
84 	};
85 	
86 	describe! "deserialize object using custom serialization methods" in {
87 		it! "short return a custom deserialized object equal to the original object" in {
88 			auto f = serializer.deserialize!(Foo)(archive.untypedData);
89 
90 			assert(foo.a == f.a);
91 			
92 			assert(i == 5);
93 		};
94 	};
95 
96 	describe! "serialize object with string using custom serialization" in {
97 		it! "serailzes the object" in {
98 			serializer.reset();
99 
100 			serializer.serialize(withString);
101 
102 			assert(archive.data().containsDefaultXmlContent());
103 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Custom.WithString" type="spec.serialization.Custom.WithString" key="0" id="0"`));
104 			assert(archive.data().containsXmlTag("string", `type="immutable(char)" length="8" key="y" id="1"`, "a string"));
105 
106 			assert(j == 4);
107 		};
108 	};
109 
110 	describe! "deserialize object with string using custom serialization" in {
111 		it! "returns an object equal to the original object" in {
112 			auto a = serializer.deserialize!(WithString)(archive.untypedData);
113 
114 			assert(withString.b == a.b);
115 			assert(j == 5);
116 		};
117 	};
118 }