testStandardPreprocess.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "testBase.h"
  2. static void testStandardWhitespaceCharsetAndBomPolicy(void)
  3. {
  4. // 仅保留标准空白集合的正向语义与 BOM 拒绝策略。
  5. const char *wrapped = " \n\t\r{\"a\":1}\r\t\n ";
  6. const char *end = NULL;
  7. RyanJson_t doc = RyanJsonParseOptions(wrapped, (uint32_t)strlen(wrapped), RyanJsonTrue, &end);
  8. TEST_ASSERT_NOT_NULL_MESSAGE(doc, "标准空白包裹的 Json 在 strict 终止下应解析成功");
  9. TEST_ASSERT_NOT_NULL(end);
  10. TEST_ASSERT_EQUAL_CHAR('\0', *end);
  11. TEST_ASSERT_EQUAL_INT(1, RyanJsonGetIntValue(RyanJsonGetObjectToKey(doc, "a")));
  12. RyanJsonDelete(doc);
  13. const uint8_t bomText[] = {0xEF, 0xBB, 0xBF, '{', '"', 'a', '"', ':', '1', '}', '\0'};
  14. TEST_ASSERT_NULL_MESSAGE(RyanJsonParse((const char *)bomText), "带 UTF-8 BOM 前缀的文本应解析失败");
  15. TEST_ASSERT_NULL_MESSAGE(RyanJsonParseOptions((const char *)bomText, (uint32_t)(sizeof(bomText) - 1U), RyanJsonTrue, NULL),
  16. "带 UTF-8 BOM 前缀的切片在 strict 终止下应解析失败");
  17. }
  18. static void testStandardRejectCommentWithoutMinifyButAcceptAfterMinify(void)
  19. {
  20. // 标准 Json 不接受注释;Minify 预处理后才可进入正常解析路径。
  21. char withComment[] = " {\"a\":1,/*comment*/\"b\":\"//keep\"} ";
  22. TEST_ASSERT_NULL_MESSAGE(RyanJsonParse(withComment), "标准 Json 解析不应接受注释语法");
  23. uint32_t minLen = RyanJsonMinify(withComment, (int32_t)strlen(withComment));
  24. TEST_ASSERT_EQUAL_STRING("{\"a\":1,\"b\":\"//keep\"}", withComment);
  25. TEST_ASSERT_EQUAL_UINT32((uint32_t)strlen(withComment), minLen);
  26. RyanJson_t doc = RyanJsonParse(withComment);
  27. TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Minify 预处理后的文本应可解析");
  28. TEST_ASSERT_EQUAL_INT(1, RyanJsonGetIntValue(RyanJsonGetObjectToKey(doc, "a")));
  29. TEST_ASSERT_EQUAL_STRING("//keep", RyanJsonGetStringValue(RyanJsonGetObjectToKey(doc, "b")));
  30. RyanJsonDelete(doc);
  31. }
  32. void testStandardPreprocessRunner(void)
  33. {
  34. UnitySetTestFile(__FILE__);
  35. RUN_TEST(testStandardWhitespaceCharsetAndBomPolicy);
  36. RUN_TEST(testStandardRejectCommentWithoutMinifyButAcceptAfterMinify);
  37. }