1 /** 2 * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved. 3 * Authors: Jacob Carlborg 4 * Version: Initial created: Aug 20, 2011 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module tests.NonSerialized; 8 9 import orange.serialization.Serializer; 10 import orange.serialization.Serializable; 11 import orange.serialization.archives.XmlArchive; 12 import orange.test.UnitTester; 13 import tests.Util; 14 15 Serializer serializer; 16 XmlArchive!(char) archive; 17 18 class Bar 19 { 20 mixin NonSerialized; 21 22 int c; 23 } 24 25 @nonSerialized class Baz 26 { 27 int c; 28 } 29 30 class Foo 31 { 32 int a; 33 int b; 34 @nonSerialized int c; 35 Bar bar; 36 Baz baz; 37 38 mixin NonSerialized!(a); 39 } 40 41 Foo foo; 42 43 unittest 44 { 45 archive = new XmlArchive!(char); 46 serializer = new Serializer(archive); 47 48 foo = new Foo; 49 foo.a = 3; 50 foo.b = 4; 51 foo.c = 5; 52 53 foo.bar = new Bar; 54 foo.baz = new Baz; 55 56 describe("serialize object with a non-serialized field") in { 57 it("should return serialized object with only one serialized field") 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.NonSerialized.Foo" type="tests.NonSerialized.Foo" key="0" id="0"> 63 <int key="b" id="1">4</int> 64 </object> 65 </data> 66 </archive> 67 xml"; 68 serializer.serialize(foo); 69 70 auto data = archive.data; 71 assert(expected.equalToXml(data)); 72 73 assert(!data.contains(`key="a"`)); 74 assert(!data.contains(`key="c"`)); 75 76 assert(!data.contains(`runtimeType="tests.NonSerialized.Bar"`)); 77 assert(!data.contains(`runtimeType="tests.NonSerialized.Baz"`)); 78 }; 79 }; 80 81 describe("deserialize object with a non-serialized field") in { 82 it("short return deserialized object equal to the original object, where only one field is deserialized") in { 83 auto f = serializer.deserialize!(Foo)(archive.untypedData); 84 85 assert(f.a == foo.a.init); 86 assert(f.b == foo.b); 87 assert(f.c == foo.c.init); 88 assert(f.bar is foo.bar.init); 89 assert(f.baz is foo.baz.init); 90 }; 91 }; 92 }