1 /** 2 * Copyright: Copyright (c) 2013 Jacob Carlborg. All rights reserved. 3 * Authors: Juan Manuel 4 * Version: Initial created: Apr 14, 2013 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module tests.CustomWithString; 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 string b; 21 22 void toData (Serializer serializer, Serializer.Data key) 23 { 24 i++; 25 serializer.serialize(a, "x"); 26 serializer.serialize(b, "y"); 27 } 28 29 void fromData (Serializer serializer, Serializer.Data key) 30 { 31 i++; 32 a = serializer.deserialize!(int)("x"); 33 b = serializer.deserialize!(string)("y"); 34 } 35 } 36 37 Foo foo; 38 int i; 39 40 unittest 41 { 42 archive = new XmlArchive!(char); 43 serializer = new Serializer(archive); 44 45 foo = new Foo; 46 foo.a = 3; 47 foo.b = "a string"; 48 i = 3; 49 50 describe("serialize object using custom serialization methods") in { 51 it("should return a custom serialized object") in { 52 auto expected = q"xml 53 <?xml version="1.0" encoding="UTF-8"?> 54 <archive version="1.0.0" type="org.dsource.orange.xml"> 55 <data> 56 <object runtimeType="tests.CustomWithString.Foo" type="tests.CustomWithString.Foo" key="0" id="0"> 57 <int key="x" id="1">3</int> 58 <string type="immutable(char)" length="8" key="y" id="2">a string</string> 59 </object> 60 </data> 61 </archive> 62 xml"; 63 serializer.serialize(foo); 64 65 assert(expected.equalToXml(archive.data)); 66 }; 67 }; 68 69 describe("deserialize object using custom serialization methods") in { 70 it("should deserialize the string field properly") in { 71 auto f = serializer.deserialize!(Foo)(archive.untypedData); 72 73 assert(foo.a == f.a); 74 assert(foo.b == f.b); 75 76 assert(i == 5); 77 }; 78 }; 79 }