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 tests.Custom;
8 
9 import orange.serialization.Serializer;
10 import orange.serialization.archives.XmlArchive;
11 import orange.test.UnitTester;
12 import tests.Util;
13 
14 Serializer serializer;
15 XmlArchive!(char) archive;
16 
17 class Foo
18 {
19     int a;
20     int b;
21 
22     void toData (Serializer serializer, Serializer.Data key)
23     {
24         i++;
25         serializer.serialize(a, "x");
26     }
27 
28     void fromData (Serializer serializer, Serializer.Data key)
29     {
30         i++;
31         a = serializer.deserialize!(int)("x");
32     }
33 }
34 
35 Foo foo;
36 int i;
37 
38 unittest
39 {
40     archive = new XmlArchive!(char);
41     serializer = new Serializer(archive);
42 
43     foo = new Foo;
44     foo.a = 3;
45     foo.b = 4;
46     i = 3;
47 
48     describe("serialize object using custom serialization methods") in {
49         it("should return a custom serialized object") in {
50             auto expected = q"xml
51 <?xml version="1.0" encoding="UTF-8"?>
52 <archive version="1.0.0" type="org.dsource.orange.xml">
53     <data>
54         <object runtimeType="tests.Custom.Foo" type="tests.Custom.Foo" key="0" id="0">
55             <int key="x" id="1">3</int>
56         </object>
57     </data>
58 </archive>
59 xml";
60             serializer.serialize(foo);
61 
62             assert(expected.equalToXml(archive.data));
63         };
64     };
65 
66     describe("deserialize object using custom serialization methods") in {
67         it("short return a custom deserialized object equal to the original object") in {
68             auto f = serializer.deserialize!(Foo)(archive.untypedData);
69 
70             assert(foo.a == f.a);
71 
72             assert(i == 5);
73         };
74     };
75 }