strict_ut.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "test_base.h"
  2. class TJsonPathStrictTest : public TJsonPathTestBase {
  3. public:
  4. TJsonPathStrictTest()
  5. : TJsonPathTestBase()
  6. {
  7. }
  8. UNIT_TEST_SUITE(TJsonPathStrictTest);
  9. UNIT_TEST(TestRuntimeErrors);
  10. UNIT_TEST(TestIncomparableTypes);
  11. UNIT_TEST(TestLikeRegexPredicate);
  12. UNIT_TEST(TestStartsWithPredicate);
  13. UNIT_TEST_SUITE_END();
  14. void TestRuntimeErrors() {
  15. const TVector<TRuntimeErrorTestCase> testCases = {
  16. {R"([
  17. {"key": 1},
  18. {"key": 2}
  19. ])", "$.key", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
  20. {R"([
  21. {"key": 1},
  22. {"key": 2}
  23. ])", "$.*", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
  24. {R"({
  25. "first": {"key": 1},
  26. "second": []
  27. })", "$.*.key", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
  28. {R"({
  29. "first": {"key": 1},
  30. "second": []
  31. })", "$.*.*", C(TIssuesIds::JSONPATH_EXPECTED_OBJECT)},
  32. {R"({"another_key": 123})", "$.key", C(TIssuesIds::JSONPATH_MEMBER_NOT_FOUND)},
  33. {R"([1, 2])", "$[*][0]", C(TIssuesIds::JSONPATH_EXPECTED_ARRAY)},
  34. {R"([[1], 2, [3]])", "$[*][0]", C(TIssuesIds::JSONPATH_EXPECTED_ARRAY)},
  35. {R"({
  36. "idx": -1,
  37. "array": [1, 2, 3]
  38. })", "$.array[$.idx]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  39. {R"({
  40. "from": -1,
  41. "to": 3,
  42. "array": [1, 2, 3]
  43. })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  44. {R"({
  45. "from": 0,
  46. "to": -1,
  47. "array": [1, 2, 3]
  48. })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  49. {R"({
  50. "from": -20,
  51. "to": -10,
  52. "array": [1, 2, 3]
  53. })", "$.array[$.from to $.to]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  54. {R"([1, 2, 3, 4, 5])", "$[3 to 0]", C(TIssuesIds::JSONPATH_INVALID_ARRAY_INDEX_RANGE)},
  55. {R"([[1, 2], [3, 4, 5], []])", "$[*][2]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  56. {"[]", "$[last]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  57. {"[]", "$[last to 0]", C(TIssuesIds::JSONPATH_ARRAY_INDEX_OUT_OF_BOUNDS)},
  58. };
  59. for (const auto& testCase : testCases) {
  60. for (const auto mode : STRICT_MODES) {
  61. RunRuntimeErrorTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Error);
  62. }
  63. }
  64. }
  65. void TestIncomparableTypes() {
  66. const TVector<TMultiOutputTestCase> testCases = {
  67. {R"({
  68. "left": [1, 2, "string"],
  69. "right": [4, 5, 6]
  70. })", "$.left < $.right", {"null"}},
  71. {R"({
  72. "left": ["string", 2, 3],
  73. "right": [4, 5, 6]
  74. })", "$.left < $.right", {"null"}},
  75. };
  76. for (const auto& testCase : testCases) {
  77. for (const auto mode : STRICT_MODES) {
  78. RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
  79. }
  80. }
  81. }
  82. void TestLikeRegexPredicate() {
  83. const TVector<TMultiOutputTestCase> testCases = {
  84. {R"(["123", 123])", R"($[*] like_regex "[0-9]+")", {"null"}},
  85. {R"([123, "123"])", R"($[*] like_regex "[0-9]+")", {"null"}},
  86. };
  87. for (const auto& testCase : testCases) {
  88. for (const auto mode : STRICT_MODES) {
  89. RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
  90. }
  91. }
  92. }
  93. void TestStartsWithPredicate() {
  94. const TVector<TMultiOutputTestCase> testCases = {
  95. {R"(["a", "b", "c"])", R"("abcd" starts with $[*])", {"true"}},
  96. {R"(["a", 1.45, 50])", R"("abcd" starts with $[*])", {"null"}},
  97. {R"([1.45, 50, "a"])", R"("abcd" starts with $[*])", {"null"}},
  98. {R"(["b", "c"])", R"("abcd" starts with $[*])", {"false"}},
  99. };
  100. for (const auto& testCase : testCases) {
  101. for (const auto mode : STRICT_MODES) {
  102. RunTestCase(testCase.Json, mode + testCase.JsonPath, testCase.Result);
  103. }
  104. }
  105. }
  106. };
  107. UNIT_TEST_SUITE_REGISTRATION(TJsonPathStrictTest);