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