testUsageRoundtrip.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "testBase.h"
  2. static void assertUsageRoundtripDocument(const char *jsonText)
  3. {
  4. RyanJson_t root = RyanJsonParse(jsonText);
  5. TEST_ASSERT_NOT_NULL(root);
  6. char *printed = RyanJsonPrint(root, 128, RyanJsonFalse, NULL);
  7. TEST_ASSERT_NOT_NULL(printed);
  8. RyanJson_t reparsed = RyanJsonParse(printed);
  9. TEST_ASSERT_NOT_NULL(reparsed);
  10. TEST_ASSERT_TRUE(RyanJsonCompare(root, reparsed));
  11. RyanJsonDelete(reparsed);
  12. RyanJsonFree(printed);
  13. RyanJsonDelete(root);
  14. }
  15. // usage 层只保留真实文档结构的 roundtrip 契约;
  16. // String 转义、Unicode、空容器基础值矩阵等已由 standard/equality 承接。
  17. #define USAGE_ROUNDTRIP_STRUCTURAL_CASES \
  18. X(testUsageRoundtripRootArrayRecords, "[{\"id\":1,\"v\":true},{\"id\":2,\"v\":false}]") \
  19. X(testUsageRoundtripMixedLiteralArrayInObject, "{\"mix\":[1,\"2\",null,true,false,{\"x\":1}],\"end\":0}") \
  20. X(testUsageRoundtripDeepMixedContainers, "{\"arr\":[0,{\"m\":{\"n\":[1,{\"o\":\"p\"}]}}],\"flag\":false}") \
  21. X(testUsageRoundtripNestedNullAndEmptyFields, "{\"doc\":{\"title\":\"x\",\"subtitle\":null,\"tags\":[],\"meta\":{}},\"ok\":true}") \
  22. X(testUsageRoundtripSiblingListsBoard, \
  23. "{\"board\":{\"todo\":[{\"id\":1,\"tags\":[\"a\"]}],\"doing\":[{\"id\":2,\"tags\":[]}],\"done\":[]}}") \
  24. X(testUsageRoundtripListItemsWithNestedTags, \
  25. "{\"list\":[{\"id\":1,\"tags\":[\"a\",\"b\"]},{\"id\":2,\"tags\":[]}],\"meta\":{\"count\":2}}")
  26. #define USAGE_ROUNDTRIP_BUSINESS_CASES \
  27. X(testUsageRoundtripOrderSnapshot, \
  28. "{\"order\":{\"id\":\"O1001\",\"items\":[{\"sku\":\"A1\",\"qty\":2,\"price\":9.99},{\"sku\":\"B2\",\"qty\":1,\"price\":19.5}]," \
  29. "\"discount\":1.5},\"total\":37.98,\"paid\":true}") \
  30. X(testUsageRoundtripFeatureFlagRules, "{\"flags\":{\"new_ui\":true,\"beta\":false},\"conditions\":[{\"key\":\"region\"," \
  31. "\"in\":[\"us\",\"eu\"]},{\"key\":\"tier\",\"in\":[\"pro\"]}]}") \
  32. X(testUsageRoundtripMetricsSeries, \
  33. "{\"metrics\":{\"series\":[{\"t\":1,\"v\":1.1},{\"t\":2,\"v\":1.2},{\"t\":3,\"v\":1.3}],\"unit\":\"ms\"}}") \
  34. X(testUsageRoundtripI18nCatalog, \
  35. "{\"i18n\":{\"en\":{\"ok\":\"OK\",\"cancel\":\"Cancel\"},\"zh\":{\"ok\":\"\\u786e\\u5b9a\",\"cancel\":\"\\u53d6\\u6d88\"}}}") \
  36. X(testUsageRoundtripProductCatalog, \
  37. "{\"catalog\":{\"id\":\"C1\",\"products\":[{\"id\":\"P1\",\"name\":\"Pen\",\"attrs\":" \
  38. "{\"color\":\"blue\",\"size\":\"M\"}},{\"id\":\"P2\",\"name\":\"Box\",\"attrs\":{}}]},\"ver\":3}")
  39. #define X(name, jsonText) \
  40. static void name(void) \
  41. { \
  42. assertUsageRoundtripDocument((jsonText)); \
  43. }
  44. USAGE_ROUNDTRIP_STRUCTURAL_CASES
  45. USAGE_ROUNDTRIP_BUSINESS_CASES
  46. #undef X
  47. void testUsageRoundtripRunner(void)
  48. {
  49. UnitySetTestFile(__FILE__);
  50. #define X(name, jsonText) RUN_TEST(name);
  51. USAGE_ROUNDTRIP_STRUCTURAL_CASES
  52. USAGE_ROUNDTRIP_BUSINESS_CASES
  53. #undef X
  54. }