tests/test_json.c

changeset 1139
7dfa5bcf39ee
parent 1130
5bcb725119b6
equal deleted inserted replaced
1138:29672c777a28 1139:7dfa5bcf39ee
137 CX_TEST_ASSERT(0 == cx_strcmp( 137 CX_TEST_ASSERT(0 == cx_strcmp(
138 cxJsonAsCxString(object), 138 cxJsonAsCxString(object),
139 CX_STR("{\n\t\"object\":null\n}")) 139 CX_STR("{\n\t\"object\":null\n}"))
140 ); 140 );
141 cxJsonValueFree(obj); 141 cxJsonValueFree(obj);
142 }
143 cxJsonDestroy(&json);
144 }
145
146 CX_TEST(test_json_escaped_end_of_string) {
147 CxJson json;
148 cxJsonInit(&json, NULL);
149 CX_TEST_DO {
150 // first test, normal scenario
151 cxJsonFill(&json, "\"a \\\"test\\\" string\"");
152 CxJsonValue *val;
153 CxJsonStatus result = cxJsonNext(&json, &val);
154 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
155 CX_TEST_ASSERT(cxJsonIsString(val));
156 CX_TEST_ASSERT(0 == cx_strcmp(
157 cxJsonAsCxString(val),
158 cx_str("a \"test\" string"))
159 );
160 cxJsonValueFree(val);
161
162 // second test - uncompleted token with hanging escape char
163 cxJsonFill(&json, "\"a \\\"test\\");
164 result = cxJsonNext(&json, &val);
165 CX_TEST_ASSERT(result == CX_JSON_INCOMPLETE_DATA);
166 cxJsonFill(&json, "\" string\"");
167 result = cxJsonNext(&json, &val);
168 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
169 CX_TEST_ASSERT(cxJsonIsString(val));
170 CX_TEST_ASSERT(0 == cx_strcmp(
171 cxJsonAsCxString(val),
172 cx_str("a \"test\" string"))
173 );
174 cxJsonValueFree(val);
142 } 175 }
143 cxJsonDestroy(&json); 176 cxJsonDestroy(&json);
144 } 177 }
145 178
146 CX_TEST(test_json_object_incomplete_token) { 179 CX_TEST(test_json_object_incomplete_token) {
1007 CxTestSuite *suite = cx_test_suite_new("json"); 1040 CxTestSuite *suite = cx_test_suite_new("json");
1008 1041
1009 cx_test_register(suite, test_json_init_default); 1042 cx_test_register(suite, test_json_init_default);
1010 cx_test_register(suite, test_json_simple_object); 1043 cx_test_register(suite, test_json_simple_object);
1011 cx_test_register(suite, test_json_escaped_strings); 1044 cx_test_register(suite, test_json_escaped_strings);
1045 cx_test_register(suite, test_json_escaped_end_of_string);
1012 cx_test_register(suite, test_json_object_incomplete_token); 1046 cx_test_register(suite, test_json_object_incomplete_token);
1013 cx_test_register(suite, test_json_token_wrongly_completed); 1047 cx_test_register(suite, test_json_token_wrongly_completed);
1014 cx_test_register(suite, test_json_object_error); 1048 cx_test_register(suite, test_json_object_error);
1015 cx_test_register(suite, test_json_subsequent_fill); 1049 cx_test_register(suite, test_json_subsequent_fill);
1016 cx_test_register(suite, test_json_large_nesting_depth); 1050 cx_test_register(suite, test_json_large_nesting_depth);

mercurial