1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Aug 6, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module spec.serialization.Pointer;
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 F
20 {
21 	int value;
22 	int* ptr;
23 	int* ptr2;
24 }
25 
26 F f;
27 F fDeserialized;
28 int pointee;
29 
30 class OutOfOrder
31 {
32 	int* ptr;
33 	int value;
34 	int* ptr2;
35 }
36 
37 OutOfOrder outOfOrder;
38 OutOfOrder outOfOrderDeserialized;
39 int outOfOrderPointee;
40 
41 unittest
42 {
43 	archive = new XmlArchive!(char);
44 	serializer = new Serializer(archive);
45 
46 	pointee = 3;
47 	f = new F;
48 	f.value = 9;
49 	f.ptr = &f.value;
50 	f.ptr2 = &pointee;
51 
52 	outOfOrderPointee = 3;
53 	outOfOrder = new OutOfOrder;
54 	outOfOrder.value = 9;
55 	outOfOrder.ptr = &outOfOrder.value;
56 	outOfOrder.ptr2 = &outOfOrderPointee;
57 
58 	describe! "serialize pointer" in {
59 		it! "should return a serialized pointer" in {
60 			serializer.reset();
61 			serializer.serialize(f);
62 
63 			assert(archive.data().containsDefaultXmlContent());
64 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Pointer.F" type="spec.serialization.Pointer.F" key="0" id="0"`));
65 			assert(archive.data().containsXmlTag("int", `key="value" id="1"`, "9"));
66 			assert(archive.data().containsXmlTag("pointer", `key="ptr" id="2"`));
67 			assert(archive.data().containsXmlTag("reference", `key="1"`, "1"));
68 			assert(archive.data().containsXmlTag("pointer", `key="ptr2" id="3"`));
69 			assert(archive.data().containsXmlTag("int", `key="2" id="4"`, "3"));
70 		};
71 	};
72 	
73 	describe! "deserialize pointer" in {
74 		fDeserialized = serializer.deserialize!(F)(archive.untypedData);
75 	
76 		it! "should return a deserialized pointer equal to the original pointer" in {
77 			assert(*f.ptr == *fDeserialized.ptr);
78 		};
79 		
80 		it! "the pointer should point to the deserialized value" in {
81 			assert(fDeserialized.ptr == &fDeserialized.value);
82 		};
83 	};
84 
85 	describe! "serialize pointer out of order" in {
86 		it! "should return a serialized pointer" in {
87 			serializer.reset();
88 			serializer.serialize(outOfOrder);
89 
90 			assert(archive.data().containsDefaultXmlContent());
91 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Pointer.OutOfOrder" type="spec.serialization.Pointer.OutOfOrder" key="0" id="0"`));
92 			assert(archive.data().containsXmlTag("pointer", `key="ptr" id="1"`));
93 			assert(archive.data().containsXmlTag("int", `key="1" id="2"`, "9"));
94 			assert(archive.data().containsXmlTag("reference", `key="value"`, "1"));
95 			assert(archive.data().containsXmlTag("pointer", `key="ptr2" id="4"`));
96 			assert(archive.data().containsXmlTag("int", `key="2" id="5"`, "3"));
97 		};
98 	};
99 
100 	describe! "deserialize pointer out of order" in {
101 		outOfOrderDeserialized = serializer.deserialize!(OutOfOrder)(archive.untypedData);
102 
103 		it! "should return a deserialized pointer equal to the original pointer" in {
104 			assert(*outOfOrder.ptr == *outOfOrderDeserialized.ptr);
105 		};
106 
107 		it! "the pointer should point to the deserialized value" in {
108 			assert(outOfOrderDeserialized.ptr == &outOfOrderDeserialized.value);
109 		};
110 	};
111 }