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.BaseClass; 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 int[] c; 21 22 int getA () 23 { 24 return a; 25 } 26 27 int getB () 28 { 29 return a; 30 } 31 } 32 33 class Sub : Base 34 { 35 int b; 36 37 override int getB () 38 { 39 return b; 40 } 41 } 42 43 Sub sub; 44 Base base; 45 46 unittest 47 { 48 archive = new XmlArchive!(char); 49 serializer = new Serializer(archive); 50 51 sub = new Sub; 52 sub.a = 3; 53 sub.b = 4; 54 base = sub; 55 56 describe("serialize subclass through a base class reference") in { 57 it("should return serialized subclass with the static type \"Base\" and the runtime type \"tests.BaseClass.Sub\"") in { 58 auto expected = q"xml 59 <?xml version="1.0" encoding="UTF-8"?> 60 <archive version="1.0.0" type="org.dsource.orange.xml"> 61 <data> 62 <object runtimeType="tests.BaseClass.Sub" type="tests.BaseClass.Base" key="0" id="0"> 63 <int key="b" id="1">4</int> 64 <base type="tests.BaseClass.Base" key="1" id="2"> 65 <int key="a" id="3">3</int> 66 <array type="inout(int)" length="0" key="c" id="4"/> 67 </base> 68 </object> 69 </data> 70 </archive> 71 xml"; 72 73 Serializer.register!(Sub); 74 serializer.serialize(base); 75 76 assert(expected.equalToXml(archive.data)); 77 }; 78 }; 79 80 describe("deserialize subclass through a base class reference") in { 81 it("should return a deserialized subclass with the static type \"Base\" and the runtime type \"tests.BaseClass.Sub\"") in { 82 auto subDeserialized = serializer.deserialize!(Base)(archive.untypedData); 83 84 assert(sub.a == subDeserialized.getA); 85 assert(sub.b == subDeserialized.getB); 86 87 Serializer.resetRegisteredTypes; 88 }; 89 }; 90 }