tests/test_json.c

changeset 1556
afdaa70034f8
parent 1548
12315ee158ad
equal deleted inserted replaced
1555:8972247f54e8 1556:afdaa70034f8
139 v = cxJsonObjGet(obj, "mystring"); 139 v = cxJsonObjGet(obj, "mystring");
140 CX_TEST_ASSERT(cxJsonIsString(v)); 140 CX_TEST_ASSERT(cxJsonIsString(v));
141 CX_TEST_ASSERT(0 == cx_strcmp(cxJsonAsCxMutStr(v), "test")); 141 CX_TEST_ASSERT(0 == cx_strcmp(cxJsonAsCxMutStr(v), "test"));
142 } 142 }
143 cxJsonValueFree(obj); 143 cxJsonValueFree(obj);
144 }
145
146 CX_TEST(test_json_from_string) {
147 cxstring text = cx_str(
148 "{\n"
149 "\t\"message\":\"success\",\n"
150 "\t\"position\":{\n"
151 "\t\t\"longitude\":-94.7099,\n"
152 "\t\t\"latitude\":51.5539\n"
153 "\t},\n"
154 "\t\"timestamp\":1729348561,\n"
155 "\t\"alive\":true\n"
156 "}"
157 );
158
159 CX_TEST_DO {
160 CxJsonValue *obj;
161 CX_TEST_ASSERT(cxJsonFromString(NULL, text, &obj) == CX_JSON_NO_ERROR);
162
163 // check the contents
164 CX_TEST_ASSERT(cxJsonIsObject(obj));
165
166 CxJsonValue *message = cxJsonObjGet(obj, "message");
167 CX_TEST_ASSERT(cxJsonIsString(message));
168 CX_TEST_ASSERT(0 == cx_strcmp(
169 cxJsonAsCxString(message),
170 "success")
171 );
172
173 CxJsonValue *position = cxJsonObjGet(obj, "position");
174 CX_TEST_ASSERT(cxJsonIsObject(position));
175 CxJsonValue *longitude = cxJsonObjGet(position, "longitude");
176 CX_TEST_ASSERT(cxJsonIsNumber(longitude));
177 CX_TEST_ASSERT(!cxJsonIsInteger(longitude));
178 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(longitude), -94.7099));
179 CX_TEST_ASSERT(cxJsonAsInteger(longitude) == -94);
180 CxJsonValue *latitude = cxJsonObjGet(position, "latitude");
181 CX_TEST_ASSERT(cxJsonIsNumber(latitude));
182 CX_TEST_ASSERT(!cxJsonIsInteger(latitude));
183 CX_TEST_ASSERT(0 == cx_vcmp_double(cxJsonAsDouble(latitude), 51.5539));
184 CX_TEST_ASSERT(cxJsonAsInteger(latitude) == 51);
185
186 CxJsonValue *timestamp = cxJsonObjGet(obj, "timestamp");
187 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
188 CX_TEST_ASSERT(cxJsonIsNumber(timestamp));
189 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
190 CX_TEST_ASSERT(cxJsonAsDouble(timestamp) == 1729348561.0);
191
192 CxJsonValue *alive = cxJsonObjGet(obj, "alive");
193 CX_TEST_ASSERT(cxJsonIsBool(alive));
194 CX_TEST_ASSERT(cxJsonIsTrue(alive));
195 CX_TEST_ASSERT(!cxJsonIsFalse(alive));
196 CX_TEST_ASSERT(cxJsonAsBool(alive));
197
198 cxJsonValueFree(obj);
199 }
144 } 200 }
145 201
146 CX_TEST(test_json_escaped_strings) { 202 CX_TEST(test_json_escaped_strings) {
147 cxstring text = cx_str( 203 cxstring text = cx_str(
148 "{\n" 204 "{\n"
1460 CxTestSuite *suite = cx_test_suite_new("json"); 1516 CxTestSuite *suite = cx_test_suite_new("json");
1461 1517
1462 cx_test_register(suite, test_json_init_default); 1518 cx_test_register(suite, test_json_init_default);
1463 cx_test_register(suite, test_json_simple_object); 1519 cx_test_register(suite, test_json_simple_object);
1464 cx_test_register(suite, test_json_large_object); 1520 cx_test_register(suite, test_json_large_object);
1521 cx_test_register(suite, test_json_from_string);
1465 cx_test_register(suite, test_json_escaped_strings); 1522 cx_test_register(suite, test_json_escaped_strings);
1466 cx_test_register(suite, test_json_escaped_unicode_strings); 1523 cx_test_register(suite, test_json_escaped_unicode_strings);
1467 cx_test_register(suite, test_json_escaped_unicode_malformed); 1524 cx_test_register(suite, test_json_escaped_unicode_malformed);
1468 cx_test_register(suite, test_json_escaped_end_of_string); 1525 cx_test_register(suite, test_json_escaped_end_of_string);
1469 cx_test_register(suite, test_json_object_incomplete_token); 1526 cx_test_register(suite, test_json_object_incomplete_token);

mercurial