1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 7, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module spec.serialization.BaseClass;
8 
9 import dspec.Dsl;
10 
11 import mambo.core..string;
12 import mambo.serialization.Serializer;
13 import mambo.serialization.archives.XmlArchive;
14 import spec.serialization.Util;
15 
16 Serializer serializer;
17 XmlArchive!(char) archive;
18 
19 class Base
20 {
21 	int a;
22 	int[] c;
23 	
24 	int getA ()
25 	{
26 		return a;
27 	}
28 	
29 	int getB ()
30 	{
31 		return a;
32 	}
33 }
34 
35 class Sub : Base
36 {
37 	int b;
38 
39 	override int getB ()
40 	{
41 		return b;
42 	}
43 }
44 
45 Sub sub;
46 Base base;
47 
48 unittest
49 {
50 	archive = new XmlArchive!(char);
51 	serializer = new Serializer(archive);
52 
53 	sub = new Sub;
54 	sub.a = 3;
55 	sub.b = 4;
56 	base = sub;
57 
58 	describe! "serialize subclass through a base class reference" in {
59 		it! "should return serialized subclass with the static type \"Base\" and the runtime type \"tests.BaseClass.Sub\"" in {
60 			Serializer.register!(Sub);
61 			serializer.serialize(base);
62 	
63 			assert(archive.data().containsDefaultXmlContent());
64 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.BaseClass.Sub" type="spec.serialization.BaseClass.Base" key="0" id="0"`));
65 			assert(archive.data().containsXmlTag("int", `key="b" id="1"`, "4"));
66 			assert(archive.data().containsXmlTag("base", `type="spec.serialization.BaseClass.Base" key="1" id="2"`));
67 			assert(archive.data().containsXmlTag("int", `key="a" id="3"`, "3"));
68 			assert(archive.data().containsXmlTag("array", `type="inout(int)" length="0" key="c" id="4"`, true));
69 		};
70 	};
71 	
72 	describe! "deserialize subclass through a base class reference" in {
73 		it! "should return a deserialized subclass with the static type \"Base\" and the runtime type \"tests.BaseClass.Sub\"" in {
74 			auto subDeserialized = serializer.deserialize!(Base)(archive.untypedData);
75 
76 			assert(sub.a == subDeserialized.getA);
77 			assert(sub.b == subDeserialized.getB);
78 			
79 			Serializer.resetRegisteredTypes;
80 		};
81 	};
82 }