examples_ut.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "test_base.h"
  2. /*
  3. These examples are taken from [ISO/IEC TR 19075-6:2017] standard (https://www.iso.org/standard/67367.html)
  4. */
  5. class TJsonPathExamplesTest : public TJsonPathTestBase {
  6. public:
  7. TJsonPathExamplesTest()
  8. : TJsonPathTestBase()
  9. {
  10. }
  11. UNIT_TEST_SUITE(TJsonPathExamplesTest);
  12. UNIT_TEST(TestMemberAccessExamples);
  13. UNIT_TEST(TestElementAccessExamples);
  14. UNIT_TEST(TestFilterExamples);
  15. UNIT_TEST_SUITE_END();
  16. void TestMemberAccessExamples() {
  17. TString input = R"({
  18. "phones": [
  19. {"type": "cell", "number": "abc-defg"},
  20. {"number": "pqr-wxyz"},
  21. {"type": "home", "number": "hij-klmn"}
  22. ]
  23. })";
  24. RunTestCase(input, "lax $.phones.type", {"\"cell\"", "\"home\""});
  25. RunRuntimeErrorTestCase(input, "strict $.phones[*].type", C(TIssuesIds::JSONPATH_MEMBER_NOT_FOUND));
  26. // NOTE: Example in standard has different order of elements. This is okay because order of elements after
  27. // wildcard member access is implementation-defined
  28. RunTestCase(input, "lax $.phones.*", {"\"abc-defg\"", "\"cell\"", "\"pqr-wxyz\"", "\"hij-klmn\"", "\"home\""});
  29. }
  30. void TestElementAccessExamples() {
  31. // NOTE: Example in standard has different order of elements. This is okay because order of elements after
  32. // wildcard member access is implementation-defined
  33. RunTestCase(R"({
  34. "sensors": {
  35. "SF": [10, 11, 12, 13, 15, 16, 17],
  36. "FC": [20, 22, 24],
  37. "SJ": [30, 33]
  38. }
  39. })", "lax $.sensors.*[0, last, 2]", {"20", "24", "24", "10", "17", "12", "30", "33"});
  40. RunTestCase(R"({
  41. "x": [12, 30],
  42. "y": [8],
  43. "z": ["a", "b", "c"]
  44. })", "lax $.*[1 to last]", {"30", "\"b\"", "\"c\""});
  45. }
  46. void TestFilterExamples() {
  47. RunParseErrorTestCase("$ ? (@.skilled)");
  48. TString json = R"({"name":"Portia","skilled":true})";
  49. RunTestCase(json, "$ ? (@.skilled == true)", {json});
  50. // Standard also mentions this example in lax mode. It is invalid because
  51. // in this case automatic unwrapping on arrays before filters will be performed
  52. // and query will finish with error
  53. RunTestCase(R"({
  54. "x": [1, "one"]
  55. })", "strict $.x ? (2 > @[*])", {});
  56. RunTestCase(R"({
  57. "name": {
  58. "first": "Manny",
  59. "last": "Moe"
  60. },
  61. "points": 123
  62. })", "strict $ ? (exists (@.name)).name", {R"({"first":"Manny","last":"Moe"})"});
  63. RunTestCase(R"({
  64. "points": 41
  65. })", "strict $ ? (exists (@.name)).name", {});
  66. }
  67. };
  68. UNIT_TEST_SUITE_REGISTRATION(TJsonPathExamplesTest);