1 | /* $Id: tstRTCRest-1.cpp 74161 2018-09-09 14:12:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - REST C++ classes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/cpp/restbase.h>
|
---|
32 |
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/message.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/test.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Global Variables *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | static RTTEST g_hTest;
|
---|
43 |
|
---|
44 |
|
---|
45 | static char *toJson(RTCRestObjectBase const *pObj)
|
---|
46 | {
|
---|
47 | RTCString str;
|
---|
48 | RTCRestOutputToString Dst(&str, false);
|
---|
49 | pObj->serializeAsJson(Dst);
|
---|
50 |
|
---|
51 | static char s_szReturnBuffer[4096];
|
---|
52 | RTStrCopy(s_szReturnBuffer, sizeof(s_szReturnBuffer), str.c_str());
|
---|
53 | return s_szReturnBuffer;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | static int deserializeFromJson(RTCRestObjectBase *pObj, const char *pszJson, PRTERRINFOSTATIC pErrInfo, const char *pszName)
|
---|
58 | {
|
---|
59 | RTJSONVAL hValue;
|
---|
60 | RTTESTI_CHECK_RC_OK_RET(RTJsonParseFromString(&hValue, pszJson, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL), rcCheck);
|
---|
61 | RTCRestJsonPrimaryCursor Cursor(hValue, pszName, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
|
---|
62 | return pObj->deserializeFromJson(Cursor.m_Cursor);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | static int fromString(RTCRestObjectBase *pObj, const char *pszString, PRTERRINFOSTATIC pErrInfo, const char *pszName)
|
---|
67 | {
|
---|
68 | RTCString strValue(pszString);
|
---|
69 | return pObj->fromString(strValue, pszName, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | static void testBool(void)
|
---|
74 | {
|
---|
75 | RTTestSub(g_hTest, "RTCRestBool");
|
---|
76 |
|
---|
77 | {
|
---|
78 | RTCRestBool obj1;
|
---|
79 | RTTESTI_CHECK(obj1.m_fValue == false);
|
---|
80 | RTTESTI_CHECK(obj1.isNull() == false);
|
---|
81 | RTTESTI_CHECK(strcmp(obj1.typeName(), "bool") == 0);
|
---|
82 | RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_Bool);
|
---|
83 | }
|
---|
84 |
|
---|
85 | {
|
---|
86 | RTCRestBool obj2(true);
|
---|
87 | RTTESTI_CHECK(obj2.m_fValue == true);
|
---|
88 | RTTESTI_CHECK(obj2.isNull() == false);
|
---|
89 | }
|
---|
90 |
|
---|
91 | {
|
---|
92 | RTCRestBool obj2(false);
|
---|
93 | RTTESTI_CHECK(obj2.m_fValue == false);
|
---|
94 | RTTESTI_CHECK(obj2.isNull() == false);
|
---|
95 | }
|
---|
96 |
|
---|
97 | {
|
---|
98 | /* Value assignments: */
|
---|
99 | RTCRestBool obj3;
|
---|
100 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
101 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
102 | obj3.assignValue(true);
|
---|
103 | RTTESTI_CHECK(obj3.m_fValue == true);
|
---|
104 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
105 |
|
---|
106 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
107 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
108 | obj3.assignValue(false);
|
---|
109 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
110 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
111 |
|
---|
112 | obj3.assignValue(true);
|
---|
113 | RTTESTI_CHECK(obj3.m_fValue == true);
|
---|
114 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
115 |
|
---|
116 | RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
|
---|
117 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
118 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
119 |
|
---|
120 | obj3.assignValue(true);
|
---|
121 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
122 | RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
|
---|
123 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
124 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
125 |
|
---|
126 | /* Copy assignments: */
|
---|
127 | RTCRestBool obj3True(true);
|
---|
128 | RTTESTI_CHECK(obj3True.m_fValue == true);
|
---|
129 | RTTESTI_CHECK(obj3True.isNull() == false);
|
---|
130 | RTCRestBool obj3False(false);
|
---|
131 | RTTESTI_CHECK(obj3False.m_fValue == false);
|
---|
132 | RTTESTI_CHECK(obj3False.isNull() == false);
|
---|
133 | RTCRestBool obj3Null;
|
---|
134 | obj3Null.setNull();
|
---|
135 | RTTESTI_CHECK(obj3Null.m_fValue == false);
|
---|
136 | RTTESTI_CHECK(obj3Null.isNull() == true);
|
---|
137 |
|
---|
138 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
139 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3True), VINF_SUCCESS);
|
---|
140 | RTTESTI_CHECK(obj3.m_fValue == true);
|
---|
141 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
142 |
|
---|
143 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
|
---|
144 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
145 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
146 |
|
---|
147 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3False), VINF_SUCCESS);
|
---|
148 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
149 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
150 |
|
---|
151 | obj3 = obj3Null;
|
---|
152 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
153 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
154 |
|
---|
155 | obj3 = obj3True;
|
---|
156 | RTTESTI_CHECK(obj3.m_fValue == true);
|
---|
157 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
158 |
|
---|
159 | obj3 = obj3Null;
|
---|
160 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
161 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
162 |
|
---|
163 | obj3 = obj3False;
|
---|
164 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
165 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
166 |
|
---|
167 | /* setNull implies resetToDefault: */
|
---|
168 | obj3 = obj3True;
|
---|
169 | RTTESTI_CHECK(obj3.m_fValue == true);
|
---|
170 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
171 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
172 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
173 | RTTESTI_CHECK(obj3.m_fValue == false);
|
---|
174 |
|
---|
175 | /* Copy constructors: */
|
---|
176 | {
|
---|
177 | RTCRestBool obj3a(obj3True);
|
---|
178 | RTTESTI_CHECK(obj3a.m_fValue == true);
|
---|
179 | RTTESTI_CHECK(obj3a.isNull() == false);
|
---|
180 | }
|
---|
181 | {
|
---|
182 | RTCRestBool obj3b(obj3False);
|
---|
183 | RTTESTI_CHECK(obj3b.m_fValue == false);
|
---|
184 | RTTESTI_CHECK(obj3b.isNull() == false);
|
---|
185 | }
|
---|
186 | {
|
---|
187 | RTCRestBool obj3c(obj3Null);
|
---|
188 | RTTESTI_CHECK(obj3c.m_fValue == false);
|
---|
189 | RTTESTI_CHECK(obj3c.isNull() == true);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* Serialization to json: */
|
---|
193 | const char *pszJson = toJson(&obj3True);
|
---|
194 | RTTESTI_CHECK_MSG(strcmp(pszJson, "true") == 0, ("pszJson=%s\n", pszJson));
|
---|
195 | pszJson = toJson(&obj3False);
|
---|
196 | RTTESTI_CHECK_MSG(strcmp(pszJson, "false") == 0, ("pszJson=%s\n", pszJson));
|
---|
197 | pszJson = toJson(&obj3Null);
|
---|
198 | RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
|
---|
199 |
|
---|
200 | /* Serialization to string. */
|
---|
201 | RTCString str;
|
---|
202 | str = "lead-in:";
|
---|
203 | RTTESTI_CHECK_RC(obj3True.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
204 | RTTESTI_CHECK_MSG(str.equals("lead-in:true"), ("str=%s\n", str.c_str()));
|
---|
205 | RTTESTI_CHECK_RC(obj3True.toString(&str), VINF_SUCCESS);
|
---|
206 | RTTESTI_CHECK_MSG(str.equals("true"), ("str=%s\n", str.c_str()));
|
---|
207 |
|
---|
208 | str = "lead-in:";
|
---|
209 | RTTESTI_CHECK_RC(obj3False.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
210 | RTTESTI_CHECK_MSG(str.equals("lead-in:false"), ("str=%s\n", str.c_str()));
|
---|
211 | RTTESTI_CHECK_RC(obj3False.toString(&str), VINF_SUCCESS);
|
---|
212 | RTTESTI_CHECK_MSG(str.equals("false"), ("str=%s\n", str.c_str()));
|
---|
213 |
|
---|
214 | str = "lead-in:";
|
---|
215 | RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
216 | RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
|
---|
217 | RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
|
---|
218 | RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* deserialize: */
|
---|
222 | RTERRINFOSTATIC ErrInfo;
|
---|
223 | {
|
---|
224 | RTCRestBool obj4;
|
---|
225 | obj4.setNull();
|
---|
226 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
227 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
228 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
229 |
|
---|
230 | obj4.setNull();
|
---|
231 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "true", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
232 | RTTESTI_CHECK(obj4.m_fValue == true);
|
---|
233 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
234 |
|
---|
235 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
236 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
237 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
238 |
|
---|
239 | /* object goes to default state on failure: */
|
---|
240 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "0", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
|
---|
241 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
242 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
243 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
244 |
|
---|
245 | obj4.assignValue(true);
|
---|
246 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"false\"", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
|
---|
247 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
248 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
249 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
250 |
|
---|
251 | obj4.setNull();
|
---|
252 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
|
---|
253 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
254 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
255 |
|
---|
256 | obj4.setNull();
|
---|
257 | RTTESTI_CHECK_RC(fromString(&obj4, "true", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
258 | RTTESTI_CHECK(obj4.m_fValue == true);
|
---|
259 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
260 |
|
---|
261 | RTTESTI_CHECK_RC(fromString(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
262 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
263 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
264 |
|
---|
265 | obj4.m_fValue = true;
|
---|
266 | RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
267 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
268 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
269 |
|
---|
270 | obj4.setNull();
|
---|
271 | RTTESTI_CHECK_RC(fromString(&obj4, " TrUe ", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
272 | RTTESTI_CHECK(obj4.m_fValue == true);
|
---|
273 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
274 |
|
---|
275 | RTTESTI_CHECK_RC(fromString(&obj4, "\tfAlSe;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
276 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
277 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
278 |
|
---|
279 | RTTESTI_CHECK_RC(fromString(&obj4, "\r\nfAlSe\n;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
280 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
281 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
282 |
|
---|
283 | RTTESTI_CHECK_RC(fromString(&obj4, "\r\tNuLl\n;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
284 | RTTESTI_CHECK(obj4.m_fValue == false);
|
---|
285 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
286 |
|
---|
287 | RTTESTI_CHECK_RC(fromString(&obj4, "1", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_UNABLE_TO_PARSE_STRING_AS_BOOL);
|
---|
288 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
289 |
|
---|
290 | RTTESTI_CHECK_RC(fromString(&obj4, "0", NULL, RT_XSTR(__LINE__)), VERR_REST_UNABLE_TO_PARSE_STRING_AS_BOOL);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | class Int64Constants
|
---|
295 | {
|
---|
296 | public:
|
---|
297 | const char *getSubName() const { return "RTCRestInt64"; }
|
---|
298 | int64_t getMin() const { return INT64_MIN; }
|
---|
299 | const char *getMinStr() const { return "-9223372036854775808"; }
|
---|
300 | int64_t getMax() const { return INT64_MAX; }
|
---|
301 | const char *getMaxStr() const { return "9223372036854775807"; }
|
---|
302 | const char *getTypeName() const { return "int64_t"; }
|
---|
303 | RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int64; }
|
---|
304 | };
|
---|
305 |
|
---|
306 | class Int32Constants
|
---|
307 | {
|
---|
308 | public:
|
---|
309 | const char *getSubName() const { return "RTCRestInt32"; }
|
---|
310 | int32_t getMin() const { return INT32_MIN; }
|
---|
311 | const char *getMinStr() const { return "-2147483648"; }
|
---|
312 | int32_t getMax() const { return INT32_MAX; }
|
---|
313 | const char *getMaxStr() const { return "2147483647"; }
|
---|
314 | const char *getTypeName() const { return "int32_t"; }
|
---|
315 | RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int32; }
|
---|
316 | };
|
---|
317 |
|
---|
318 | class Int16Constants
|
---|
319 | {
|
---|
320 | public:
|
---|
321 | const char *getSubName() const { return "RTCRestInt16"; }
|
---|
322 | int16_t getMin() const { return INT16_MIN; }
|
---|
323 | const char *getMinStr() const { return "-32768"; }
|
---|
324 | int16_t getMax() const { return INT16_MAX; }
|
---|
325 | const char *getMaxStr() const { return "32767"; }
|
---|
326 | const char *getTypeName() const { return "int16_t"; }
|
---|
327 | RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int16; }
|
---|
328 | };
|
---|
329 |
|
---|
330 | template<typename RestType, typename IntType, typename ConstantClass>
|
---|
331 | void testInteger(void)
|
---|
332 | {
|
---|
333 | ConstantClass const Consts;
|
---|
334 | RTTestSub(g_hTest, Consts.getSubName());
|
---|
335 |
|
---|
336 | {
|
---|
337 | RestType obj1;
|
---|
338 | RTTESTI_CHECK(obj1.m_iValue == 0);
|
---|
339 | RTTESTI_CHECK(obj1.isNull() == false);
|
---|
340 | RTTESTI_CHECK(strcmp(obj1.typeName(), Consts.getTypeName()) == 0);
|
---|
341 | RTTESTI_CHECK(obj1.typeClass() == Consts.getTypeClass());
|
---|
342 | }
|
---|
343 |
|
---|
344 | {
|
---|
345 | RestType obj2(2398);
|
---|
346 | RTTESTI_CHECK(obj2.m_iValue == 2398);
|
---|
347 | RTTESTI_CHECK(obj2.isNull() == false);
|
---|
348 | }
|
---|
349 |
|
---|
350 | {
|
---|
351 | RestType obj2(-7345);
|
---|
352 | RTTESTI_CHECK(obj2.m_iValue == -7345);
|
---|
353 | RTTESTI_CHECK(obj2.isNull() == false);
|
---|
354 | }
|
---|
355 |
|
---|
356 | {
|
---|
357 | /* Value assignments: */
|
---|
358 | RestType obj3;
|
---|
359 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
360 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
361 | obj3.assignValue(-1);
|
---|
362 | RTTESTI_CHECK(obj3.m_iValue == -1);
|
---|
363 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
364 |
|
---|
365 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
366 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
367 | obj3.assignValue(42);
|
---|
368 | RTTESTI_CHECK(obj3.m_iValue == 42);
|
---|
369 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
370 |
|
---|
371 | obj3.assignValue(Consts.getMax());
|
---|
372 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
|
---|
373 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
374 |
|
---|
375 | obj3.assignValue(Consts.getMin());
|
---|
376 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
|
---|
377 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
378 |
|
---|
379 | RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
|
---|
380 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
381 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
382 |
|
---|
383 | obj3.assignValue(42);
|
---|
384 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
385 | RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
|
---|
386 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
387 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
388 |
|
---|
389 | /* Copy assignments: */
|
---|
390 | RestType obj3Max(Consts.getMax());
|
---|
391 | RTTESTI_CHECK(obj3Max.m_iValue == Consts.getMax());
|
---|
392 | RTTESTI_CHECK(obj3Max.isNull() == false);
|
---|
393 | RestType obj3Min(Consts.getMin());
|
---|
394 | RTTESTI_CHECK(obj3Min.m_iValue == Consts.getMin());
|
---|
395 | RTTESTI_CHECK(obj3Min.isNull() == false);
|
---|
396 | RestType obj3Null;
|
---|
397 | obj3Null.setNull();
|
---|
398 | RTTESTI_CHECK(obj3Null.m_iValue == 0);
|
---|
399 | RTTESTI_CHECK(obj3Null.isNull() == true);
|
---|
400 |
|
---|
401 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
402 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3Max), VINF_SUCCESS);
|
---|
403 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
|
---|
404 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
405 |
|
---|
406 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
|
---|
407 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
408 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
409 |
|
---|
410 | RTTESTI_CHECK_RC(obj3.assignCopy(obj3Min), VINF_SUCCESS);
|
---|
411 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
|
---|
412 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
413 |
|
---|
414 | obj3 = obj3Null;
|
---|
415 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
416 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
417 |
|
---|
418 | obj3 = obj3Max;
|
---|
419 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
|
---|
420 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
421 |
|
---|
422 | obj3 = obj3Null;
|
---|
423 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
424 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
425 |
|
---|
426 | obj3 = obj3Min;
|
---|
427 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
|
---|
428 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
429 |
|
---|
430 | /* setNull implies resetToDefault: */
|
---|
431 | obj3 = obj3Max;
|
---|
432 | RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
|
---|
433 | RTTESTI_CHECK(obj3.isNull() == false);
|
---|
434 | RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
|
---|
435 | RTTESTI_CHECK(obj3.isNull() == true);
|
---|
436 | RTTESTI_CHECK(obj3.m_iValue == 0);
|
---|
437 |
|
---|
438 | /* Copy constructors: */
|
---|
439 | {
|
---|
440 | RestType obj3a(obj3Max);
|
---|
441 | RTTESTI_CHECK(obj3a.m_iValue == Consts.getMax());
|
---|
442 | RTTESTI_CHECK(obj3a.isNull() == false);
|
---|
443 | }
|
---|
444 | {
|
---|
445 | RestType obj3b(obj3Min);
|
---|
446 | RTTESTI_CHECK(obj3b.m_iValue == Consts.getMin());
|
---|
447 | RTTESTI_CHECK(obj3b.isNull() == false);
|
---|
448 | }
|
---|
449 | {
|
---|
450 | RestType obj3c(obj3Null);
|
---|
451 | RTTESTI_CHECK(obj3c.m_iValue == 0);
|
---|
452 | RTTESTI_CHECK(obj3c.isNull() == true);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /* Serialization to json: */
|
---|
456 | const char *pszJson = toJson(&obj3Max);
|
---|
457 | RTTESTI_CHECK_MSG(strcmp(pszJson, Consts.getMaxStr()) == 0, ("pszJson=%s\n", pszJson));
|
---|
458 | pszJson = toJson(&obj3Min);
|
---|
459 | RTTESTI_CHECK_MSG(strcmp(pszJson, Consts.getMinStr()) == 0, ("pszJson=%s\n", pszJson));
|
---|
460 | pszJson = toJson(&obj3Null);
|
---|
461 | RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
|
---|
462 |
|
---|
463 | /* Serialization to string. */
|
---|
464 | RTCString str;
|
---|
465 | RTCString strExpect;
|
---|
466 | str = "lead-in:";
|
---|
467 | RTTESTI_CHECK_RC(obj3Max.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
468 | strExpect.printf("lead-in:%s", Consts.getMaxStr());
|
---|
469 | RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
|
---|
470 | RTTESTI_CHECK_RC(obj3Max.toString(&str), VINF_SUCCESS);
|
---|
471 | RTTESTI_CHECK_MSG(str.equals(Consts.getMaxStr()), ("str=%s\n", str.c_str()));
|
---|
472 |
|
---|
473 | str = "lead-in:";
|
---|
474 | RTTESTI_CHECK_RC(obj3Min.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
475 | strExpect.printf("lead-in:%s", Consts.getMinStr());
|
---|
476 | RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
|
---|
477 | RTTESTI_CHECK_RC(obj3Min.toString(&str), VINF_SUCCESS);
|
---|
478 | RTTESTI_CHECK_MSG(str.equals(Consts.getMinStr()), ("str=%s\n", str.c_str()));
|
---|
479 |
|
---|
480 | str = "lead-in:";
|
---|
481 | RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
|
---|
482 | RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
|
---|
483 | RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
|
---|
484 | RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
|
---|
485 | }
|
---|
486 |
|
---|
487 | /* deserialize: */
|
---|
488 | RTERRINFOSTATIC ErrInfo;
|
---|
489 | {
|
---|
490 | /* from json: */
|
---|
491 | RestType obj4;
|
---|
492 | obj4.setNull();
|
---|
493 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
494 | RTTESTI_CHECK(obj4.m_iValue == 42);
|
---|
495 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
496 |
|
---|
497 | obj4.setNull();
|
---|
498 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "-22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
499 | RTTESTI_CHECK(obj4.m_iValue == -22);
|
---|
500 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
501 |
|
---|
502 | obj4.setNull();
|
---|
503 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, Consts.getMaxStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
504 | RTTESTI_CHECK(obj4.m_iValue == Consts.getMax());
|
---|
505 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
506 |
|
---|
507 | obj4.setNull();
|
---|
508 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, Consts.getMinStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
509 | RTTESTI_CHECK(obj4.m_iValue == Consts.getMin());
|
---|
510 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
511 |
|
---|
512 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
513 | RTTESTI_CHECK(obj4.m_iValue == false);
|
---|
514 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
515 |
|
---|
516 | /* object goes to default state on failure: */
|
---|
517 | obj4.assignValue(Consts.getMin());
|
---|
518 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "0.0", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
|
---|
519 | RTTESTI_CHECK(obj4.m_iValue == 0);
|
---|
520 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
521 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
522 |
|
---|
523 | obj4.assignValue(Consts.getMax());
|
---|
524 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"false\"", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
|
---|
525 | RTTESTI_CHECK(obj4.m_iValue == 0);
|
---|
526 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
527 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
528 |
|
---|
529 | obj4.setNull();
|
---|
530 | RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
|
---|
531 | RTTESTI_CHECK(obj4.m_iValue == 0);
|
---|
532 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
533 |
|
---|
534 | /* from string: */
|
---|
535 | obj4.setNull();
|
---|
536 | RTTESTI_CHECK_RC(fromString(&obj4, "22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
537 | RTTESTI_CHECK(obj4.m_iValue == 22);
|
---|
538 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
539 |
|
---|
540 | RTTESTI_CHECK_RC(fromString(&obj4, "-42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
541 | RTTESTI_CHECK(obj4.m_iValue == -42);
|
---|
542 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
543 |
|
---|
544 | RTTESTI_CHECK_RC(fromString(&obj4, Consts.getMaxStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
545 | RTTESTI_CHECK(obj4.m_iValue == Consts.getMax());
|
---|
546 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
547 |
|
---|
548 | RTTESTI_CHECK_RC(fromString(&obj4, Consts.getMinStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
549 | RTTESTI_CHECK(obj4.m_iValue == Consts.getMin());
|
---|
550 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
551 |
|
---|
552 | obj4.m_iValue = 33;
|
---|
553 | RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
554 | RTTESTI_CHECK(obj4.m_iValue == 0);
|
---|
555 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
556 |
|
---|
557 | obj4.m_iValue = 33;
|
---|
558 | RTTESTI_CHECK_RC(fromString(&obj4, " nULl;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
559 | RTTESTI_CHECK(obj4.m_iValue == 0);
|
---|
560 | RTTESTI_CHECK(obj4.isNull() == true);
|
---|
561 |
|
---|
562 | obj4.setNull();
|
---|
563 | RTTESTI_CHECK_RC(fromString(&obj4, " 0x42 ", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
564 | RTTESTI_CHECK(obj4.m_iValue == 0x42);
|
---|
565 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
566 |
|
---|
567 | RTTESTI_CHECK_RC(fromString(&obj4, "\t010\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
568 | RTTESTI_CHECK(obj4.m_iValue == 8);
|
---|
569 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
570 |
|
---|
571 | RTTESTI_CHECK_RC(fromString(&obj4, "\r\t0X4FDB\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
|
---|
572 | RTTESTI_CHECK(obj4.m_iValue == 0x4fdb);
|
---|
573 | RTTESTI_CHECK(obj4.isNull() == false);
|
---|
574 |
|
---|
575 | RTTESTI_CHECK_RC(fromString(&obj4, "1.1", &ErrInfo, RT_XSTR(__LINE__)), VERR_TRAILING_CHARS);
|
---|
576 | RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
|
---|
577 |
|
---|
578 | RTTESTI_CHECK_RC(fromString(&obj4, "false", NULL, RT_XSTR(__LINE__)), VERR_NO_DIGITS);
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 |
|
---|
583 | int main()
|
---|
584 | {
|
---|
585 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTRest-1", &g_hTest);
|
---|
586 | if (rcExit == RTEXITCODE_SUCCESS )
|
---|
587 | {
|
---|
588 | testBool();
|
---|
589 | testInteger<RTCRestInt64, int64_t, Int64Constants>();
|
---|
590 | testInteger<RTCRestInt32, int32_t, Int32Constants>();
|
---|
591 | testInteger<RTCRestInt16, int16_t, Int16Constants>();
|
---|
592 |
|
---|
593 |
|
---|
594 | rcExit = RTTestSummaryAndDestroy(g_hTest);
|
---|
595 | }
|
---|
596 | return rcExit;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|