1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 7, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module tests.Subclass;
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 Base
18 {
19     int a;
20 }
21 
22 class Sub : Base
23 {
24     int b;
25 }
26 
27 Sub sub;
28 
29 unittest
30 {
31     archive = new XmlArchive!(char);
32     serializer = new Serializer(archive);
33 
34     sub = new Sub;
35     sub.a = 3;
36     sub.b = 4;
37 
38     describe("serialize a subclass") in {
39         it("should return serialized subclass") in {
40             auto expected = q"xml
41 <?xml version="1.0" encoding="UTF-8"?>
42 <archive version="1.0.0" type="org.dsource.orange.xml">
43     <data>
44         <object runtimeType="tests.Subclass.Sub" type="tests.Subclass.Sub" key="0" id="0">
45             <int key="b" id="1">4</int>
46             <base type="tests.Subclass.Base" key="1" id="2">
47                 <int key="a" id="3">3</int>
48             </base>
49         </object>
50     </data>
51 </archive>
52 xml";
53             serializer.reset;
54             serializer.serialize(sub);
55 
56             assert(expected.equalToXml(archive.data));
57         };
58     };
59 
60     describe("deserialize class with a base class") in {
61         it("should return a deserialized string equal to the original string") in {
62             auto subDeserialized = serializer.deserialize!(Sub)(archive.untypedData);
63 
64             assert(sub.a == subDeserialized.a);
65             assert(sub.b == subDeserialized.b);
66         };
67     };
68 }