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.String;
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 C
18 {
19     string str;
20     wstring wstr;
21     dstring dstr;
22 }
23 
24 C c;
25 C u;
26 
27 unittest
28 {
29     archive = new XmlArchive!(char);
30     serializer = new Serializer(archive);
31 
32     c = new C;
33     c.str = "foo";
34     c.wstr = "bar";
35     c.dstr = "foobar";
36 
37     describe("serialize strings") in {
38         it("should return serialized strings") in {
39             auto expected2066 = q"xml
40 <?xml version="1.0" encoding="UTF-8"?>
41 <archive version="1.0.0" type="org.dsource.orange.xml">
42     <data>
43         <object runtimeType="tests.String.C" type="tests.String.C" key="0" id="0">
44             <string type="immutable(wchar)" length="3" key="wstr" id="2">bar</string>
45             <string type="immutable(char)" length="3" key="str" id="1">foo</string>
46             <string type="immutable(dchar)" length="6" key="dstr" id="3">foobar</string>
47         </object>
48     </data>
49 </archive>
50 xml";
51 
52             auto expected2067 = q"xml
53 <?xml version="1.0" encoding="UTF-8"?>
54 <archive version="1.0.0" type="org.dsource.orange.xml">
55     <data>
56         <object runtimeType="tests.String.C" type="tests.String.C" id="0" key="0">
57             <string id="2" type="immutable(wchar)" length="3" key="wstr">bar</string>
58             <string id="3" type="immutable(dchar)" length="6" key="dstr">foobar</string>
59             <string id="1" type="immutable(char)" length="3" key="str">foo</string>
60         </object>
61     </data>
62 </archive>
63 xml";
64             static if (__VERSION__ >= 2067) auto expected = expected2067;
65             else auto expected = expected2066;
66 
67             serializer.reset;
68             serializer.serialize(c);
69 
70             assert(expected.equalToXml(archive.data));
71         };
72     };
73 
74     describe("deserialize string") in {
75         it("should return a deserialized string equal to the original string") in {
76             auto cDeserialized = serializer.deserialize!(C)(archive.untypedData);
77 
78             assert(c.str == cDeserialized.str);
79             assert(c.wstr == cDeserialized.wstr);
80             assert(c.dstr == cDeserialized.dstr);
81         };
82     };
83 
84     u = new C;
85     u.str = "foo åäö";
86     u.wstr = "foo ÅÄÖ";
87     u.dstr = "foo åäö ÅÄÖ";
88 
89     describe("serialize Unicode strings") in {
90         it("should return a serialized string containing proper Unicode") in {
91             auto expected2066 = q"xml
92 <?xml version="1.0" encoding="UTF-8"?>
93 <archive version="1.0.0" type="org.dsource.orange.xml">
94     <data>
95         <object runtimeType="tests.String.C" type="tests.String.C" key="0" id="0">
96             <string type="immutable(wchar)" length="7" key="wstr" id="2">foo ÅÄÖ</string>
97             <string type="immutable(char)" length="10" key="str" id="1">foo åäö</string>
98             <string type="immutable(dchar)" length="11" key="dstr" id="3">foo åäö ÅÄÖ</string>
99         </object>
100     </data>
101 </archive>
102 xml";
103 
104             auto expected2067 = q"xml
105 <?xml version="1.0" encoding="UTF-8"?>
106 <archive version="1.0.0" type="org.dsource.orange.xml">
107     <data>
108         <object runtimeType="tests.String.C" type="tests.String.C" id="0" key="0">
109             <string id="2" type="immutable(wchar)" length="7" key="wstr">foo ÅÄÖ</string>
110             <string id="3" type="immutable(dchar)" length="11" key="dstr">foo åäö ÅÄÖ</string>
111             <string id="1" type="immutable(char)" length="10" key="str">foo åäö</string>
112         </object>
113     </data>
114 </archive>
115 xml";
116             static if (__VERSION__ >= 2067) auto expected = expected2067;
117             else auto expected = expected2066;
118 
119             serializer.reset;
120             serializer.serialize(u);
121 
122             assert(expected.equalToXml(archive.data));
123         };
124     };
125 
126     describe("deserialize Unicode string") in {
127         it("should return a deserialize Unicode string equal to the original strings") in {
128             auto uDeserialized = serializer.deserialize!(C)(archive.untypedData);
129 
130             assert(u.str == uDeserialized.str);
131             assert(u.wstr == uDeserialized.wstr);
132             assert(u.dstr == uDeserialized.dstr);
133         };
134     };
135 }