1 /** 2 * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Aug 18, 2011 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module tests.NonIntrusive; 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 x; 20 } 21 22 class Foo : Base 23 { 24 private int a_; 25 private int b_; 26 27 int a () { return a_; } 28 int a (int a) { return a_ = a; } 29 int b () { return b_; } 30 int b (int b) { return b_ = b; } 31 } 32 33 Foo foo; 34 int i; 35 36 void toData (Foo foo, Serializer serializer, Serializer.Data key) 37 { 38 i++; 39 serializer.serialize(foo.a, "a"); 40 serializer.serialize(foo.b, "b"); 41 serializer.serializeBase(foo); 42 } 43 44 void fromData (ref Foo foo, Serializer serializer, Serializer.Data key) 45 { 46 i++; 47 foo.a = serializer.deserialize!(int)("a"); 48 foo.b = serializer.deserialize!(int)("b"); 49 serializer.deserializeBase(foo); 50 } 51 52 unittest 53 { 54 archive = new XmlArchive!(char); 55 serializer = new Serializer(archive); 56 57 foo = new Foo; 58 foo.a = 3; 59 foo.b = 4; 60 foo.x = 5; 61 i = 3; 62 63 describe("serialize object using a non-intrusive method") in { 64 it("should return a custom serialized object") in { 65 auto expected = q"xml 66 <?xml version="1.0" encoding="UTF-8"?> 67 <archive version="1.0.0" type="org.dsource.orange.xml"> 68 <data> 69 <object runtimeType="tests.NonIntrusive.Foo" type="tests.NonIntrusive.Foo" key="0" id="0"> 70 <int key="a" id="1">3</int> 71 <int key="b" id="2">4</int> 72 <base type="tests.NonIntrusive.Base" key="1" id="3"> 73 <int key="x" id="4">5</int> 74 </base> 75 </object> 76 </data> 77 </archive> 78 xml"; 79 Serializer.registerSerializer(&toData); 80 Serializer.registerDeserializer(&fromData); 81 82 serializer.serialize(foo); 83 84 assert(i == 4); 85 assert(expected.equalToXml(archive.data)); 86 }; 87 }; 88 89 describe("deserialize object using a non-intrusive method") in { 90 it("short return a custom deserialized object equal to the original object") in { 91 auto f = serializer.deserialize!(Foo)(archive.untypedData); 92 93 assert(foo.a == f.a); 94 assert(foo.b == f.b); 95 assert(foo.x == f.x); 96 97 assert(i == 5); 98 99 Serializer.resetSerializers; 100 }; 101 }; 102 }