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 spec.serialization.NonMutable;
8 
9 import dspec.Dsl;
10 
11 import mambo.core._;
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 B
20 {
21 	int a;
22 
23 	pure this (int a)
24 	{
25 		this.a = a;
26 	}
27 
28 	override equals_t opEquals (Object other)
29 	{
30 		if (auto o = cast(B) other)
31 			return a == o.a;
32 
33 		return false;
34 	}
35 }
36 
37 class A
38 {
39 	const int a;
40 	immutable int b;
41 	immutable string c;
42 	immutable B d;
43 	immutable(int)* e;
44 
45 	this (int a, int b, string c, immutable B d, immutable(int)* e)
46 	{
47 		this.a = a;
48 		this.b = b;
49 		this.c = c;
50 		this.d = d;
51 		this.e = e;
52 	}
53 
54 	override equals_t opEquals (Object other)
55 	{
56 		if (auto o = cast(A) other)
57 			return a == o.a &&
58 				b == o.b &&
59 				c == o.c &&
60 				d == o.d &&
61 				*e == *o.e;
62 
63 		return false;
64 	}
65 }
66 
67 class CTFEFields
68 {
69 	public immutable int FIRST;
70 	public immutable int SECOND;
71 	public bool someFlag;
72 
73 	this ()
74 	{
75 		FIRST = 1;
76 		SECOND = 1;
77 	}
78 }
79 
80 A a;
81 immutable int ptr = 3;
82 
83 unittest
84 {
85 	archive = new XmlArchive!(char);
86 	serializer = new Serializer(archive);
87 
88 	a = new A(1, 2, "str", new immutable(B)(3), &ptr);
89 
90 	describe! "serialize object with immutable and const fields" in {
91 		it! "should return a serialized object" in {
92 			serializer.reset;
93 			serializer.serialize(a);
94 
95 			version (D_Version2) string stringElementType = "immutable(char)";
96 			else string stringElementType = "char";
97 
98 			assert(archive.data().containsDefaultXmlContent());
99 			assert(archive.data().contains(`<object runtimeType="spec.serialization.NonMutable.A" type="spec.serialization.NonMutable.A" key="0" id="0">`));
100 
101 			assert(archive.data().containsXmlTag("int", `key="a" id="1"`, "1"));
102 			assert(archive.data().containsXmlTag("int", `key="b" id="2"`, "2"));
103 			assert(archive.data().containsXmlTag("string", `type="` ~ stringElementType ~ `" length="3" key="c" id="3"`, "str"));
104 
105 			assert(archive.data().contains(`<object runtimeType="spec.serialization.NonMutable.B" type="immutable(spec.serialization.NonMutable.B)" key="d" id="4">`));
106 
107 			assert(archive.data().containsXmlTag("pointer", `key="e" id="6"`));
108 			assert(archive.data().containsXmlTag("int", `key="1" id="7"`, "3"));
109 		};
110 	};
111 	
112 	describe! "deserialize object" in {
113 		it! "should return a deserialized object equal to the original object" in {
114 			auto aDeserialized = serializer.deserialize!(A)(archive.untypedData);
115 			assert(a == aDeserialized);
116 		};
117 	};
118 
119 	describe! "serializing object with CTFE fields" in {
120 	    it! "should compile" in {
121 	        assert(__traits(compiles, {
122 	            serializer.serialize(new CTFEFields);
123 	        }));
124 	    };
125 	};
126 }