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