matchers.h 1.7 KB

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