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.Primitive;
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 H
20 {
21 	bool bool_;
22 	byte byte_;
23 	//cdouble cdouble_; // currently not suppported by to!()
24 	//cent cent_; // currently not implemented but a reserved keyword
25 	//cfloat cfloat_; // currently not suppported by to!()
26 	char char_;
27 	//creal creal_; // currently not suppported by to!()
28 	dchar dchar_;
29 	double double_;
30 	float float_;
31 	//idouble idouble_; // currently not suppported by to!()
32 	//ifloat ifloat_; // currently not suppported by to!()
33 	int int_;
34 	//ireal ireal_;  // currently not suppported by to!()
35 	long long_;
36 	real real_;
37 	short short_;
38 	ubyte ubyte_;
39 	//ucent ucent_; // currently not implemented but a reserved keyword
40 	uint uint_;
41 	ulong ulong_;
42 	ushort ushort_;
43 	wchar wchar_;
44 	
45 	override equals_t opEquals (Object other)
46 	{
47 		if (auto o =  cast(H) other)
48 		{
49 			return bool_ == o.bool_ &&
50 				   byte_ == o.byte_ &&
51 				   //cdouble_ == o.cdouble_ && // currently not suppported by to!()
52 				   //cent_ == o.cent_ && // currently not implemented but a reserved keyword
53 				   //cfloat_ == o.cfloat_ && // currently not suppported by to!()
54 				   char_ == o.char_ &&
55 				   //creal_ == o.creal_ && // currently not suppported by to!()
56 				   dchar_ == o.dchar_ &&
57 				   double_ == o.double_ &&
58 				   float_ == o.float_ &&
59 				   //idouble_ == o.idouble_ && // currently not suppported by to!()
60 				   //ifloat_ == o.ifloat_ && // currently not suppported by to!()
61 				   int_ == o.int_ &&
62 				   //ireal_ == o.ireal_ &&  // currently not suppported by to!()
63 				   long_ == o.long_ &&
64 				   real_ == o.real_ &&
65 				   short_ == o.short_ &&
66 				   ubyte_ == o.ubyte_ &&
67 				   //ucent_ == o.ucent_ && // currently not implemented but a reserved keyword
68 				   uint_ == o.uint_ &&
69 				   ulong_ == o.ulong_ &&
70 				   ushort_ == o.ushort_ &&
71 				   wchar_ == o.wchar_;
72 		}
73 		
74 		return false;
75 	}
76 }
77 
78 H h;
79 
80 unittest
81 {
82 	archive = new XmlArchive!(char);
83 	serializer = new Serializer(archive);
84 
85 	h = new H;	
86 	h.bool_ = true;
87 	h.byte_ = 1;
88 	h.char_ = 'a';
89 	//h.cdouble_ = 0.0 + 0.0 * 1.0i; // currently not supported by to!() 
90 	//h.cfloat_ = 0.0f + 0.0f * 1.0i; // currently not supported by to!() 
91 	//h.creal_ = 0.0 + 0.0 * 1.0i; // currently not supported by to!() 
92 	h.dchar_ = 'b';
93 	h.double_ = 0.0;
94 	h.float_ = 0.0f;
95 	//h.idouble_ = 0.0 * 1.0i; // currently not supported by to!() 
96 	//h.ifloat_ = 0.0f * 1.0i; // currently not supported by to!()
97 	h.int_ = 1;
98 	//h.ireal_ = 0.0 * 1.0i; // currently not supported by to!()
99 	h.long_ = 1L;
100 	h.real_ = 0.0;
101 	h.short_ = 1;
102 	h.ubyte_ = 1U;
103 	h.uint_ = 1U;
104 	h.ulong_ = 1LU;
105 	h.ushort_ = 1U;
106 	h.wchar_ = 'c';
107 
108 	enum zero = "0x0p+0";
109 
110 	describe! "serialize primitives" in {
111 		it! "should return serialized primitives" in {
112 			serializer.reset;
113 			serializer.serialize(h);
114 
115 			assert(archive.data().containsDefaultXmlContent());
116 			assert(archive.data().containsXmlTag("object", `runtimeType="spec.serialization.Primitive.H" type="spec.serialization.Primitive.H" key="0" id="0"`));
117 			assert(archive.data().containsXmlTag("bool", `key="bool_" id="1"`, "true"));
118 			assert(archive.data().containsXmlTag("byte", `key="byte_" id="2"`, "1"));
119 			assert(archive.data().containsXmlTag("char", `key="char_" id="3"`, "a"));
120 			assert(archive.data().containsXmlTag("dchar", `key="dchar_" id="4"`, "b"));
121 			assert(archive.data().containsXmlTag("double", `key="double_" id="5"`, zero));
122 			assert(archive.data().containsXmlTag("float", `key="float_" id="6"`, zero));
123 			assert(archive.data().containsXmlTag("int", `key="int_" id="7"`, "1"));
124 			assert(archive.data().containsXmlTag("long", `key="long_" id="8"`, "1"));
125 			assert(archive.data().containsXmlTag("real", `key="real_" id="9"`, zero));
126 			assert(archive.data().containsXmlTag("short", `key="short_" id="10"`, "1"));
127 			assert(archive.data().containsXmlTag("ubyte", `key="ubyte_" id="11"`, "1"));
128 			assert(archive.data().containsXmlTag("uint", `key="uint_" id="12"`, "1"));
129 			assert(archive.data().containsXmlTag("ulong", `key="ulong_" id="13"`, "1"));
130 			assert(archive.data().containsXmlTag("ushort", `key="ushort_" id="14"`, "1"));
131 			assert(archive.data().containsXmlTag("wchar", `key="wchar_" id="15"`, "c"));
132 		};
133 	};
134 	
135 	describe! "deserialize primitives" in {
136 		it! "should return deserialized primitives equal to the original primitives" in {
137 			auto hDeserialized = serializer.deserialize!(H)(archive.untypedData);
138 			assert(h == hDeserialized);
139 		};
140 	};
141 }