run-stringify.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "common.h"
  2. static char buf1[256], buf2[256];
  3. /* Used for pass and fail messages */
  4. static char *quote_string(const char *str, char buf[256])
  5. {
  6. char *out = buf;
  7. *out++ = '"';
  8. for (; *str != 0; str++) {
  9. if (out - buf > 256 - 5) {
  10. /* String is too long. End it with `...' */
  11. out = buf + 256 - 5;
  12. *out++ = '.';
  13. *out++ = '.';
  14. *out++ = '.';
  15. break;
  16. }
  17. switch (*str) {
  18. case '\t':
  19. *out++ = '\\';
  20. *out++ = 't';
  21. break;
  22. case '\n':
  23. *out++ = '\\';
  24. *out++ = 'n';
  25. break;
  26. case '"':
  27. *out++ = '\\';
  28. *out++ = '"';
  29. break;
  30. case '\\':
  31. *out++ = '\\';
  32. *out++ = '\\';
  33. break;
  34. default:
  35. *out++ = *str;
  36. break;
  37. }
  38. }
  39. *out++ = '"';
  40. *out = 0;
  41. return buf;
  42. }
  43. static void test_stringify(const char *input, const char *expected)
  44. {
  45. JsonNode *node = NULL;
  46. char *enc = NULL;
  47. char *strn = NULL;
  48. char *str = NULL;
  49. node = json_decode(input);
  50. if (node == NULL) {
  51. fail("Failed to decode %s", input);
  52. goto end;
  53. }
  54. enc = json_encode(node);
  55. if (strcmp(enc, input) != 0) {
  56. fail("%s re-encodes to %s. Either encode/decode is broken, or the input string needs to be normalized", input, enc);
  57. goto end;
  58. }
  59. strn = json_stringify(node, NULL);
  60. if (strcmp(strn, enc) != 0) {
  61. fail("json_stringify with NULL space produced a different string than json_encode");
  62. goto end;
  63. }
  64. str = json_stringify(node, "\t");
  65. if (strcmp(str, expected) != 0) {
  66. fail("Expected %s, but json_stringify produced %s",
  67. quote_string(expected, buf1), quote_string(str, buf2));
  68. goto end;
  69. }
  70. pass("stringify %s", input);
  71. end:
  72. json_delete(node);
  73. free(enc);
  74. free(strn);
  75. free(str);
  76. }
  77. int main(void)
  78. {
  79. (void) chomp;
  80. plan_tests(9);
  81. test_stringify("[]", "[]");
  82. test_stringify("[1]", "[\n\t1\n]");
  83. test_stringify("[1,2,3]", "[\n\t1,\n\t2,\n\t3\n]");
  84. test_stringify("[[]]", "[\n\t[]\n]");
  85. test_stringify("[[1,2],[3,4]]", "[\n\t[\n\t\t1,\n\t\t2\n\t],\n\t[\n\t\t3,\n\t\t4\n\t]\n]");
  86. test_stringify("{}", "{}");
  87. test_stringify("{\"one\":1}", "{\n\t\"one\": 1\n}");
  88. test_stringify("{\"one\":1,\"t*\":[2,3,10]}", "{\n\t\"one\": 1,\n\t\"t*\": [\n\t\t2,\n\t\t3,\n\t\t10\n\t]\n}");
  89. test_stringify("{\"a\":{\"1\":1,\"2\":2},\"b\":{\"3\":[null,false,true,\"\\f\"]}}",
  90. "{\n\t\"a\": {\n\t\t\"1\": 1,\n\t\t\"2\": 2\n\t},\n\t\"b\": {\n\t\t\"3\": [\n\t\t\tnull,\n\t\t\tfalse,\n\t\t\ttrue,\n\t\t\t\"\\f\"\n\t\t]\n\t}\n}");
  91. return exit_status();
  92. }