matchers.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "gtest/internal/gtest-internal.h"
  2. #include <gmock/gmock.h>
  3. #include <gtest/gtest.h>
  4. #include <jvalidate/detail/pointer.h>
  5. #include <jvalidate/validation_result.h>
  6. #include <json/reader.h>
  7. #include <json/value.h>
  8. inline auto operator""_jptr(char const * data, size_t len) {
  9. return jvalidate::detail::Pointer::parse(std::string_view{data, len}).value();
  10. }
  11. inline Json::Value const operator""_json(char const * data, size_t len) {
  12. Json::Value value;
  13. Json::CharReaderBuilder builder;
  14. std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  15. std::string error;
  16. if (not reader->parse(data, data + len, &value, &error)) {
  17. throw std::runtime_error(error);
  18. }
  19. return value;
  20. }
  21. MATCHER(Valid, "") { return arg.valid(); }
  22. MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has(doc_path); }
  23. MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") { return arg.has(doc_path, schema_path); }
  24. MATCHER_P4(AnnotationAt, doc_path, schema_path, key, matcher, "") {
  25. auto const * anno = arg.annotation(doc_path, schema_path, key);
  26. *result_listener << "\ninstanceLocation: " << doc_path << " is "
  27. << (arg.has(doc_path) ? "present" : "missing");
  28. *result_listener << "\nevaluationLocation: " << schema_path << " is "
  29. << (arg.has(doc_path, schema_path) ? "present" : "missing");
  30. if (not anno) {
  31. *result_listener << "\nannotation: (nil)";
  32. return false;
  33. }
  34. *result_listener << "\nannotation: " << testing::PrintToString(*anno);
  35. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  36. }
  37. template <typename M> auto AnnotationAt(std::string const & key, M && matcher) {
  38. return AnnotationAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
  39. std::forward<M>(matcher));
  40. }
  41. MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
  42. auto const * anno = arg.error(doc_path, schema_path, key);
  43. *result_listener << "\ninstanceLocation: " << doc_path << " is "
  44. << (arg.has(doc_path) ? "present" : "missing");
  45. *result_listener << "\nevaluationLocation: " << schema_path << " is "
  46. << (arg.has(doc_path, schema_path) ? "present" : "missing");
  47. if (not anno) {
  48. *result_listener << "\nannotation: (nil)";
  49. return false;
  50. }
  51. *result_listener << "\nannotation: " << testing::PrintToString(*anno);
  52. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  53. }
  54. template <typename M> auto ErrorAt(std::string const & key, M && matcher) {
  55. return ErrorAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
  56. std::forward<M>(matcher));
  57. }