tests/test_json.c

changeset 937
10123f4d5618
equal deleted inserted replaced
936:9b9385fcdfd5 937:10123f4d5618
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/test.h"
30
31 #include "cx/json.h"
32
33 CX_TEST(test_json_simple_object) {
34 cxstring text = cx_str(
35 "{\n"
36 "\t\"message\":\"success\",\n"
37 "\t\"position\":{\n"
38 "\t\t\"longitude\":-94.7099,\n"
39 "\t\t\"latitude\":51.5539\n"
40 "\t},\n"
41 "\t\"timestamp\":1729348561,\n"
42 "\t\"alive\":true\n"
43 "}"
44 );
45
46 CX_TEST_DO {
47 int result;
48
49 CxJson json;
50 cxJsonInit(&json);
51 cxJsonFill(&json, text.ptr, text.length);
52
53 // parse the big fat object
54 CxJsonValue *obj;
55 result = cxJsonNext(&json, &obj);
56 CX_TEST_ASSERT(result == 1);
57
58 // check the contents
59 CX_TEST_ASSERT(cxJsonIsObject(obj));
60
61 CxJsonValue *message = cxJsonObjGet(obj, "message");
62 CX_TEST_ASSERT(cxJsonIsString(message));
63 CX_TEST_ASSERT(0 == cx_strcmp(
64 cx_strcast(cxJsonAsString(message)),
65 cx_str("success"))
66 );
67
68 CxJsonValue *position = cxJsonObjGet(obj, "position");
69 CX_TEST_ASSERT(cxJsonIsObject(position));
70 CxJsonValue *longitude = cxJsonObjGet(position, "longitude");
71 CX_TEST_ASSERT(cxJsonIsNumber(longitude));
72 CX_TEST_ASSERT(cxJsonAsDouble(longitude) == -94.7099);
73 CxJsonValue *latitude = cxJsonObjGet(position, "latitude");
74 CX_TEST_ASSERT(cxJsonIsNumber(latitude));
75 CX_TEST_ASSERT(cxJsonAsDouble(latitude) == 51.5539);
76
77 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp");
78 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
79 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
80
81 CxJsonValue *alive = cxJsonObjGet(obj, "alive");
82 CX_TEST_ASSERT(cxJsonIsBool(alive));
83 CX_TEST_ASSERT(cxJsonIsTrue(alive));
84 CX_TEST_ASSERT(!cxJsonIsFalse(alive));
85 CX_TEST_ASSERT(cxJsonAsBool(alive));
86
87 // this recursively frees everything else
88 cxJsonValueFree(obj);
89
90 // we only have one object that already contained all the data
91 result = cxJsonNext(&json, &obj);
92 CX_TEST_ASSERT(result == 0);
93
94 cxJsonDestroy(&json);
95 }
96 }
97
98 CxTestSuite *cx_test_suite_json(void) {
99 CxTestSuite *suite = cx_test_suite_new("json");
100
101 cx_test_register(suite, test_json_simple_object);
102
103 return suite;
104 }
105

mercurial