testPrintStyle.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "testBase.h"
  2. static RyanJson_t createObjectWithIntMember(const char *key, int32_t value)
  3. {
  4. RyanJson_t obj = RyanJsonCreateObject();
  5. TEST_ASSERT_NOT_NULL(obj);
  6. TEST_ASSERT_TRUE(RyanJsonAddIntToObject(obj, key, value));
  7. return obj;
  8. }
  9. static RyanJsonPrintStyle makeStyle(const char *indent, const char *newline, uint8_t spaceAfterColon, RyanJsonBool_e format)
  10. {
  11. RyanJsonPrintStyle style = {
  12. .indent = indent,
  13. .indentLen = (uint32_t)strlen(indent),
  14. .newline = newline,
  15. .newlineLen = (uint32_t)strlen(newline),
  16. .spaceAfterColon = spaceAfterColon,
  17. .format = format,
  18. };
  19. return style;
  20. }
  21. static void testPrintWithStyleNullStyleGuard(void)
  22. {
  23. RyanJson_t json = RyanJsonCreateObject();
  24. TEST_ASSERT_NOT_NULL(json);
  25. TEST_ASSERT_NULL(RyanJsonPrintWithStyle(json, 10, NULL, NULL));
  26. RyanJsonDelete(json);
  27. }
  28. static void testPrintPreallocatedWithStyleNullArgs(void)
  29. {
  30. RyanJson_t obj = createObjectWithIntMember("a", 1);
  31. RyanJsonPrintStyle style = makeStyle(" ", "\n", 1, RyanJsonTrue);
  32. char buf[64] = {0};
  33. TEST_ASSERT_NULL(RyanJsonPrintPreallocatedWithStyle(NULL, buf, sizeof(buf), &style, NULL));
  34. TEST_ASSERT_NULL(RyanJsonPrintPreallocatedWithStyle(obj, NULL, sizeof(buf), &style, NULL));
  35. TEST_ASSERT_NULL(RyanJsonPrintPreallocatedWithStyle(obj, buf, sizeof(buf), NULL, NULL));
  36. TEST_ASSERT_NULL(RyanJsonPrintPreallocatedWithStyle(obj, buf, 0, &style, NULL));
  37. RyanJsonDelete(obj);
  38. }
  39. static void testPrintPreallocatedWithStyleTooSmall(void)
  40. {
  41. RyanJson_t obj = createObjectWithIntMember("a", 1);
  42. RyanJsonPrintStyle style = makeStyle(" ", "\n", 1, RyanJsonTrue);
  43. char tinyBuf[2] = {0};
  44. char *out = RyanJsonPrintPreallocatedWithStyle(obj, tinyBuf, sizeof(tinyBuf), &style, NULL);
  45. TEST_ASSERT_NULL_MESSAGE(out, "WithStyle 预分配缓冲区过小应返回 NULL");
  46. RyanJsonDelete(obj);
  47. }
  48. static void testPrintPreallocatedWithStyleSuccessAndLen(void)
  49. {
  50. RyanJson_t obj = createObjectWithIntMember("a", 1);
  51. RyanJsonPrintStyle style = makeStyle(" ", "\n", 2, RyanJsonTrue);
  52. char buf[64] = {0};
  53. uint32_t len = 0;
  54. char *out = RyanJsonPrintPreallocatedWithStyle(obj, buf, sizeof(buf), &style, &len);
  55. TEST_ASSERT_NOT_NULL(out);
  56. TEST_ASSERT_EQUAL_STRING("{\n \"a\": 1\n}", out);
  57. TEST_ASSERT_EQUAL_UINT32((uint32_t)strlen(out), len);
  58. RyanJsonDelete(obj);
  59. }
  60. static void testPrintCrazy(void)
  61. {
  62. RyanJson_t emptyObj = RyanJsonCreateObject();
  63. TEST_ASSERT_NOT_NULL(emptyObj);
  64. char *s = RyanJsonPrint(emptyObj, 0, RyanJsonFalse, NULL);
  65. TEST_ASSERT_EQUAL_STRING("{}", s);
  66. RyanJsonFree(s);
  67. RyanJsonDelete(emptyObj);
  68. RyanJson_t emptyArr = RyanJsonCreateArray();
  69. TEST_ASSERT_NOT_NULL(emptyArr);
  70. s = RyanJsonPrint(emptyArr, 0, RyanJsonFalse, NULL);
  71. TEST_ASSERT_EQUAL_STRING("[]", s);
  72. RyanJsonFree(s);
  73. RyanJsonDelete(emptyArr);
  74. RyanJson_t json = RyanJsonCreateObject();
  75. TEST_ASSERT_NOT_NULL(json);
  76. TEST_ASSERT_TRUE(RyanJsonAddStringToObject(json, "k", "v"));
  77. s = RyanJsonPrint(json, 0, RyanJsonFalse, NULL);
  78. TEST_ASSERT_EQUAL_STRING("{\"k\":\"v\"}", s);
  79. RyanJsonFree(s);
  80. RyanJsonDelete(json);
  81. RyanJson_t obj = createObjectWithIntMember("a", 1);
  82. RyanJsonPrintStyle crazyStyle = makeStyle("--------", "\n\n", 4, RyanJsonTrue);
  83. uint32_t len = 0;
  84. s = RyanJsonPrintWithStyle(obj, 10, &crazyStyle, &len);
  85. TEST_ASSERT_NOT_NULL(s);
  86. TEST_ASSERT_EQUAL_UINT32((uint32_t)strlen(s), len);
  87. TEST_ASSERT_NOT_NULL(strstr(s, "--------\"a\": 1"));
  88. RyanJsonFree(s);
  89. RyanJsonDelete(obj);
  90. }
  91. static void testPrintDefault(void)
  92. {
  93. RyanJson_t json = RyanJsonParse("{\"a\":1,\"b\":true}");
  94. TEST_ASSERT_NOT_NULL(json);
  95. char *defaultFormat = RyanJsonPrint(json, 256, RyanJsonTrue, NULL);
  96. TEST_ASSERT_NOT_NULL(defaultFormat);
  97. TEST_ASSERT_NOT_NULL_MESSAGE(strstr(defaultFormat, "\t\"a\": 1"), "默认格式化缩进或冒号空格错误");
  98. RyanJsonFree(defaultFormat);
  99. RyanJsonDelete(json);
  100. }
  101. static void testPrintCustomStyle(void)
  102. {
  103. RyanJson_t json = RyanJsonParse("{\"a\":1,\"b\":true}");
  104. TEST_ASSERT_NOT_NULL(json);
  105. RyanJsonPrintStyle style = makeStyle(" ", "\r\n", 2, RyanJsonTrue);
  106. uint32_t len = 0;
  107. char *customPrint = RyanJsonPrintWithStyle(json, 256, &style, &len);
  108. TEST_ASSERT_NOT_NULL(customPrint);
  109. TEST_ASSERT_NOT_NULL_MESSAGE(strstr(customPrint, "\r\n \"a\": 1"), "自定义格式化特征片段错误");
  110. TEST_ASSERT_EQUAL_UINT32_MESSAGE((uint32_t)strlen(customPrint), len, "返回长度不一致");
  111. RyanJsonFree(customPrint);
  112. RyanJsonDelete(json);
  113. }
  114. static void testPrintWithStyleCrlfTabIndent(void)
  115. {
  116. RyanJson_t json = RyanJsonParse("{\"a\":1}");
  117. TEST_ASSERT_NOT_NULL(json);
  118. RyanJsonPrintStyle style = makeStyle("\t", "\r\n", 1, RyanJsonTrue);
  119. char *out = RyanJsonPrintWithStyle(json, 64, &style, NULL);
  120. TEST_ASSERT_NOT_NULL(out);
  121. TEST_ASSERT_EQUAL_STRING("{\r\n\t\"a\": 1\r\n}", out);
  122. RyanJsonFree(out);
  123. RyanJsonDelete(json);
  124. }
  125. static void testPrintPreallocatedWithStyleExactFitDouble(void)
  126. {
  127. // Double 节点的 WithStyle 预分配在不同配置下可能需要额外工作区,
  128. // 因此这里只强制验证“有足够 headroom 时必须成功”。
  129. RyanJson_t obj = RyanJsonParse("{\"pi\":3.14}");
  130. TEST_ASSERT_NOT_NULL(obj);
  131. RyanJsonPrintStyle style = makeStyle(" ", "\n", 1, RyanJsonTrue);
  132. uint32_t expectLen = 0;
  133. char *expect = RyanJsonPrintWithStyle(obj, 64, &style, &expectLen);
  134. TEST_ASSERT_NOT_NULL(expect);
  135. char buf[64] = {0};
  136. TEST_ASSERT_TRUE_MESSAGE(expectLen + 1U <= sizeof(buf), "测试缓冲区长度不足");
  137. char *out = RyanJsonPrintPreallocatedWithStyle(obj, buf, expectLen + 1U, &style, NULL);
  138. if (NULL != out) { TEST_ASSERT_EQUAL_STRING(expect, out); }
  139. char headroomBuf[256] = {0};
  140. out = RyanJsonPrintPreallocatedWithStyle(obj, headroomBuf, sizeof(headroomBuf), &style, NULL);
  141. TEST_ASSERT_NOT_NULL(out);
  142. TEST_ASSERT_EQUAL_STRING(expect, out);
  143. RyanJsonFree(expect);
  144. RyanJsonDelete(obj);
  145. }
  146. void testPrintStyleRunner(void)
  147. {
  148. UnitySetTestFile(__FILE__);
  149. RUN_TEST(testPrintWithStyleNullStyleGuard);
  150. RUN_TEST(testPrintPreallocatedWithStyleNullArgs);
  151. RUN_TEST(testPrintPreallocatedWithStyleTooSmall);
  152. RUN_TEST(testPrintPreallocatedWithStyleSuccessAndLen);
  153. RUN_TEST(testPrintCrazy);
  154. RUN_TEST(testPrintDefault);
  155. RUN_TEST(testPrintCustomStyle);
  156. RUN_TEST(testPrintWithStyleCrlfTabIndent);
  157. RUN_TEST(testPrintPreallocatedWithStyleExactFitDouble);
  158. }