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.NonIntrusiveStruct; 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 struct Foo 18 { 19 private int a_; 20 private int b_; 21 22 @disable this(this); 23 @disable ref Foo opAssign () (auto ref Foo other); 24 25 int a () { return a_; } 26 int a (int a) { return a_ = a; } 27 int b () { return b_; } 28 int b (int b) { return b_ = b; } 29 } 30 31 Foo foo; 32 int i; 33 34 void toData (ref Foo foo, Serializer serializer, Serializer.Data key) 35 { 36 i++; 37 serializer.serialize(foo.a, "a"); 38 serializer.serialize(foo.b, "b"); 39 } 40 41 void fromData (ref Foo foo, Serializer serializer, Serializer.Data key) 42 { 43 i++; 44 foo.a = serializer.deserialize!(int)("a"); 45 foo.b = serializer.deserialize!(int)("b"); 46 } 47 48 unittest 49 { 50 archive = new XmlArchive!(char); 51 serializer = new Serializer(archive); 52 53 foo.a = 3; 54 foo.b = 4; 55 i = 3; 56 57 describe("serialize object using a non-intrusive method") in { 58 it("should return a custom serialized object") in { 59 auto expected = q"xml 60 <?xml version="1.0" encoding="UTF-8"?> 61 <archive version="1.0.0" type="org.dsource.orange.xml"> 62 <data> 63 <struct runtimeType="tests.NonIntrusiveStruct.Foo" type="tests.NonIntrusiveStruct.Foo" key="0" id="0"> 64 <int key="a" id="1">3</int> 65 <int key="b" id="2">4</int> 66 </struct> 67 </data> 68 </archive> 69 xml"; 70 Serializer.registerSerializer(&toData); 71 Serializer.registerDeserializer(&fromData); 72 73 serializer.serialize(foo); 74 75 assert(i == 4); 76 assert(expected.equalToXml(archive.data)); 77 }; 78 }; 79 80 describe("deserialize object using a non-intrusive method") in { 81 it("short return a custom deserialized object equal to the original object") in { 82 scope (exit) 83 Serializer.resetSerializers; 84 85 auto f = serializer.deserialize!Foo(archive.untypedData); 86 87 assert(foo.a == f.a); 88 assert(foo.b == f.b); 89 90 assert(i == 5); 91 }; 92 }; 93 }