1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Nov 13, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module tests.CircularReference;
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 A
18 {
19     B b;
20     int x;
21 }
22 
23 class B
24 {
25     A a;
26     int y;
27 }
28 
29 A a;
30 B b;
31 
32 unittest
33 {
34     archive = new XmlArchive!(char);
35     serializer = new Serializer(archive);
36 
37     a = new A;
38     a.x = 3;
39 
40     b = new B;
41     b.y = 4;
42 
43     b.a = a;
44     a.b = b;
45 
46     describe("serialize objects with circular reference") in {
47         it("should return a serialized object") in {
48             auto expected = q"xml
49 <?xml version="1.0" encoding="UTF-8"?>
50 <archive version="1.0.0" type="org.dsource.orange.xml">
51     <data>
52         <object runtimeType="tests.CircularReference.A" type="tests.CircularReference.A" key="0" id="0">
53             <object runtimeType="tests.CircularReference.B" type="tests.CircularReference.B" key="b" id="1">
54                 <reference key="a">0</reference>
55                 <int key="y" id="3">4</int>
56             </object>
57             <int key="x" id="4">3</int>
58         </object>
59     </data>
60 </archive>
61 xml";
62             serializer.reset;
63             serializer.serialize(a);
64 
65             assert(expected.equalToXml(archive.data));
66         };
67     };
68 
69     describe("deserialize objects with circular reference") in {
70         it("should return a deserialized object equal to the original object") in {
71             auto aDeserialized = serializer.deserialize!(A)(archive.untypedData);
72 
73             assert(a is a.b.a);
74             assert(a.x == aDeserialized.x);
75             assert(a.b.y == aDeserialized.b.y);
76         };
77     };
78 }