| 22 | using namespace jsoncons; |
| 23 | |
| 24 | void jsonpath_tests(const std::string& fpath) |
| 25 | { |
| 26 | std::cout << "Test " << fpath << '\n'; |
| 27 | |
| 28 | std::fstream is(fpath); |
| 29 | if (!is) |
| 30 | { |
| 31 | std::cerr << "Cannot open " << fpath << "\n"; |
| 32 | exit(1); |
| 33 | } |
| 34 | |
| 35 | json tests = json::parse(is); |
| 36 | for (const auto& test_group : tests.array_range()) |
| 37 | { |
| 38 | const json& instance = test_group["given"]; |
| 39 | |
| 40 | for (const auto& test_case : test_group["cases"].array_range()) |
| 41 | { |
| 42 | std::string expr = test_case["expression"].as<std::string>(); |
| 43 | try |
| 44 | { |
| 45 | jsonpath::result_options options = jsonpath::result_options(); |
| 46 | if (test_case.contains("nodups") && test_case.at("nodups").as<bool>()) |
| 47 | { |
| 48 | options |= jsonpath::result_options::nodups; |
| 49 | } |
| 50 | if (test_case.contains("sort") && test_case.at("sort").as<bool>()) |
| 51 | { |
| 52 | options |= jsonpath::result_options::sort; |
| 53 | } |
| 54 | auto expression = jsoncons::jsonpath::make_expression<json>(expr); |
| 55 | if (test_case.contains("result")) |
| 56 | { |
| 57 | jsonpath::result_options rflags = options; |
| 58 | json actual = expression.evaluate(instance, rflags); |
| 59 | const json& expected = test_case["result"]; |
| 60 | //std::cout << "actual\n:" << actual << "\n"; |
| 61 | if (actual != expected) |
| 62 | { |
| 63 | if (test_case.contains("comment")) |
| 64 | { |
| 65 | std::cout << "\n" << test_case["comment"] << "\n"; |
| 66 | } |
| 67 | std::cout << "Input:\n" << pretty_print(instance) << "\n\n"; |
| 68 | std::cout << "Expression: " << expr << "\n\n"; |
| 69 | std::cout << "Actual: " << pretty_print(actual) << "\n\n"; |
| 70 | std::cout << "Expected: " << pretty_print(expected) << "\n\n"; |
| 71 | } |
| 72 | CHECK(expected == actual); |
| 73 | } |
| 74 | if (test_case.contains("path")) |
| 75 | { |
| 76 | jsonpath::result_options pflags = options | jsonpath::result_options::path; |
| 77 | json actual = expression.evaluate(instance, pflags); |
| 78 | const json& expected = test_case["path"]; |
| 79 | //std::cout << "actual\n:" << actual << "\n"; |
| 80 | if (actual != expected) |
| 81 | { |
no test coverage detected