1 /**
2  * Copyright: Copyright (c) 2012 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Nov 7, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module tests.NonMutable;
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 B
18 {
19     int a;
20 
21     pure this (int a)
22     {
23         this.a = a;
24     }
25 
26     override equals_t opEquals (Object other)
27     {
28         if (auto o = cast(B) other)
29             return a == o.a;
30 
31         return false;
32     }
33 }
34 
35 class A
36 {
37     const int a;
38     immutable int b;
39     immutable string c;
40     immutable B d;
41     immutable(int)* e;
42 
43     this (int a, int b, string c, immutable B d, immutable(int)* e)
44     {
45         this.a = a;
46         this.b = b;
47         this.c = c;
48         this.d = d;
49         this.e = e;
50     }
51 
52     override equals_t opEquals (Object other)
53     {
54         if (auto o = cast(A) other)
55             return a == o.a &&
56                 b == o.b &&
57                 c == o.c &&
58                 d == o.d &&
59                 *e == *o.e;
60 
61         return false;
62     }
63 }
64 
65 A a;
66 immutable int ptr = 3;
67 
68 class CTFEFieldsIssue35
69 {
70     public immutable int FIRST;
71     public immutable int SECOND1;
72     public bool someFlag;
73 
74     this ()
75     {
76         FIRST = 1;
77         SECOND1 = 1;
78     }
79 }
80 
81 unittest
82 {
83     archive = new XmlArchive!(char);
84     serializer = new Serializer(archive);
85 
86     a = new A(1, 2, "str", new immutable(B)(3), &ptr);
87 
88     describe("serialize object with immutable and const fields") in {
89         it("should return a serialized object") in {
90             auto expected = q"xml
91 <?xml version="1.0" encoding="UTF-8"?>
92 <archive version="1.0.0" type="org.dsource.orange.xml">
93     <data>
94         <object runtimeType="tests.NonMutable.A" type="tests.NonMutable.A" key="0" id="0">
95             <int key="a" id="1">1</int>
96             <int key="b" id="2">2</int>
97             <object runtimeType="tests.NonMutable.B" type="immutable(tests.NonMutable.B)" key="d" id="4">
98                 <int key="a" id="5">3</int>
99             </object>
100             <pointer key="e" id="6">
101                 <int key="1" id="7">3</int>
102             </pointer>
103             <string type="immutable(char)" length="3" key="c" id="3">str</string>
104         </object>
105     </data>
106 </archive>
107 xml";
108             serializer.reset;
109             serializer.serialize(a);
110 
111             assert(expected.equalToXml(archive.data));
112         };
113     };
114 
115     describe("deserialize object") in {
116         it("should return a deserialized object equal to the original object") in {
117             auto aDeserialized = serializer.deserialize!(A)(archive.untypedData);
118             assert(a == aDeserialized);
119         };
120     };
121 
122     describe("serializing object with CTFE fields") in {
123         it("should compile") in {
124             assert(__traits(compiles, {
125                 serializer.serialize(new CTFEFieldsIssue35);
126             }));
127         };
128     };
129 }