tests/test_json.c

changeset 939
0bb7258366a0
parent 937
10123f4d5618
equal deleted inserted replaced
938:9d02bb5dcc3c 939:0bb7258366a0
93 93
94 cxJsonDestroy(&json); 94 cxJsonDestroy(&json);
95 } 95 }
96 } 96 }
97 97
98 CX_TEST(test_json_object_incomplete_token) {
99 cxstring text = cx_str(
100 "{\"message\":\"success\" , \"__timestamp\":1729348561}");
101 cxstring parts[16];
102 size_t nparts = 0; // split the json text into mulple parts
103 for(size_t i=0;i<text.length;i+=4) {
104 parts[nparts++] = cx_strsubsl(text, i, 4);
105 }
106
107 CX_TEST_DO {
108 int result;
109
110 CxJson json;
111 cxJsonInit(&json);
112 CxJsonValue *obj;
113
114 size_t part = 0;
115 while(part < nparts) {
116 cxJsonFill(&json, parts[part].ptr, parts[part].length);
117 part++;
118 result = cxJsonNext(&json, &obj);
119
120 if(result != 0) {
121 break;
122 }
123 }
124
125 CX_TEST_ASSERT(result == 1);
126 CX_TEST_ASSERT(part == nparts);
127 CX_TEST_ASSERT(obj);
128
129 CxJsonValue *message = cxJsonObjGet(obj, "message");
130 CX_TEST_ASSERT(cxJsonIsString(message));
131 CX_TEST_ASSERT(0 == cx_strcmp(
132 cx_strcast(cxJsonAsString(message)),
133 cx_str("success"))
134 );
135 CxJsonValue *timestamp = cxJsonObjGet(obj, "__timestamp");
136 CX_TEST_ASSERT(message->type == CX_JSON_STRING);
137 CX_TEST_ASSERT(cxJsonIsInteger(timestamp));
138 CX_TEST_ASSERT(cxJsonAsInteger(timestamp) == 1729348561);
139
140 // this recursively frees everything else
141 cxJsonValueFree(obj);
142
143 // we only have one object that already contained all the data
144 result = cxJsonNext(&json, &obj);
145 CX_TEST_ASSERT(result == 0);
146
147 cxJsonDestroy(&json);
148 }
149 }
150
98 CxTestSuite *cx_test_suite_json(void) { 151 CxTestSuite *cx_test_suite_json(void) {
99 CxTestSuite *suite = cx_test_suite_new("json"); 152 CxTestSuite *suite = cx_test_suite_new("json");
100 153
101 cx_test_register(suite, test_json_simple_object); 154 cx_test_register(suite, test_json_simple_object);
155 cx_test_register(suite, test_json_object_incomplete_token);
102 156
103 return suite; 157 return suite;
104 } 158 }
105 159

mercurial