tests/test_json.c

changeset 1119
ff4d7e76f85a
parent 1117
54df904472b0
equal deleted inserted replaced
1118:9fa87f9882ba 1119:ff4d7e76f85a
892 CxJsonWriter writer = cxJsonWriterCompact(); 892 CxJsonWriter writer = cxJsonWriterCompact();
893 CxBuffer buf; 893 CxBuffer buf;
894 cxBufferInit(&buf, NULL, 32, NULL, 0); 894 cxBufferInit(&buf, NULL, 32, NULL, 0);
895 CX_TEST_DO { 895 CX_TEST_DO {
896 // test default settings (6 digits) 896 // test default settings (6 digits)
897 cxJsonWrite(&buf,num, cxBufferWriteFunc, &writer); 897 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
898 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592"))); 898 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("3.141592")));
899 899
900 // test too many digits 900 // test too many digits
901 cxBufferReset(&buf); 901 cxBufferReset(&buf);
902 writer.frac_max_digits = 200; 902 writer.frac_max_digits = 200;
935 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer); 935 cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer);
936 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("5.1122e+23"))); 936 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), CX_STR("5.1122e+23")));
937 } 937 }
938 cxBufferDestroy(&buf); 938 cxBufferDestroy(&buf);
939 cxJsonValueFree(num); 939 cxJsonValueFree(num);
940 }
941
942 CX_TEST(test_json_write_string_escape) {
943 /**
944 * According to RFC-8259 we have to test the following characters:
945 * " quotation mark
946 * \ reverse solidus
947 * / solidus
948 * b backspace
949 * f form feed
950 * n line feed
951 * r carriage return
952 * t tab
953 * And all other control characters must be encoded uXXXX - in our example the bell character.
954 * Also, all unicode characters are encoded that way - in our example the 'ö'.
955 */
956 CxJsonValue* str = cxJsonCreateString(NULL,
957 "hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f");
958 CxJsonWriter writer = cxJsonWriterCompact();
959 CxBuffer buf;
960 cxBufferInit(&buf, NULL, 128, NULL, 0);
961 CX_TEST_DO {
962 cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer);
963 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
964 CX_STR("\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\"")));
965 }
966 cxBufferDestroy(&buf);
967 cxJsonValueFree(str);
968 }
969
970 CX_TEST(test_json_write_name_escape) {
971 CxJsonValue* obj = cxJsonCreateObj(NULL);
972 cxJsonObjPutLiteral(obj,
973 CX_STR("hello\twörld\r\nthis/is\\a \"string\"\b in \a string\f"), CX_JSON_TRUE);
974 CxJsonWriter writer = cxJsonWriterCompact();
975 CxBuffer buf;
976 cxBufferInit(&buf, NULL, 128, NULL, 0);
977 CX_TEST_DO {
978 cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer);
979 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
980 CX_STR("{\"hello\\tw\\u00c3\\u00b6rld\\r\\nthis\\/is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}")));
981 }
982 cxBufferDestroy(&buf);
983 cxJsonValueFree(obj);
940 } 984 }
941 985
942 CxTestSuite *cx_test_suite_json(void) { 986 CxTestSuite *cx_test_suite_json(void) {
943 CxTestSuite *suite = cx_test_suite_new("json"); 987 CxTestSuite *suite = cx_test_suite_new("json");
944 988
960 cx_test_register(suite, test_json_write_default_format); 1004 cx_test_register(suite, test_json_write_default_format);
961 cx_test_register(suite, test_json_write_pretty_default_spaces); 1005 cx_test_register(suite, test_json_write_pretty_default_spaces);
962 cx_test_register(suite, test_json_write_pretty_default_tabs); 1006 cx_test_register(suite, test_json_write_pretty_default_tabs);
963 cx_test_register(suite, test_json_write_pretty_preserve_order); 1007 cx_test_register(suite, test_json_write_pretty_preserve_order);
964 cx_test_register(suite, test_json_write_frac_max_digits); 1008 cx_test_register(suite, test_json_write_frac_max_digits);
1009 cx_test_register(suite, test_json_write_string_escape);
1010 cx_test_register(suite, test_json_write_name_escape);
965 1011
966 return suite; 1012 return suite;
967 } 1013 }
968 1014

mercurial