matchers.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(Valid, "") { return arg.valid(); }
  21. MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has(doc_path); }
  22. MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") { return arg.has(doc_path, schema_path); }
  23. MATCHER_P2(AnnotationAt, key, matcher, "") {
  24. auto const * anno = arg.annotation({}, {}, key);
  25. if (not anno) {
  26. return false;
  27. }
  28. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  29. }
  30. MATCHER_P4(AnnotationAt, doc_path, schema_path, key, matcher, "") {
  31. auto const * anno = arg.annotation(doc_path, schema_path, key);
  32. if (not anno) {
  33. return false;
  34. }
  35. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  36. }
  37. MATCHER_P2(ErrorAt, key, matcher, "") {
  38. auto const * anno = arg.error({}, {}, key);
  39. if (not anno) {
  40. return false;
  41. }
  42. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  43. }
  44. MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
  45. auto const * anno = arg.error(doc_path, schema_path, key);
  46. if (not anno) {
  47. return false;
  48. }
  49. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  50. }