matchers.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/detail/relative_pointer.h>
  6. #include <jvalidate/validation_result.h>
  7. #include <json/reader.h>
  8. #include <json/value.h>
  9. inline auto operator""_jptr(char const * data, size_t len) {
  10. return jvalidate::detail::Pointer::parse(std::string_view{data, len}).value();
  11. }
  12. inline auto operator""_relptr(char const * data, size_t len) {
  13. return jvalidate::detail::RelativePointer::parse(std::string_view{data, len}).value();
  14. }
  15. inline Json::Value const operator""_json(char const * data, size_t len) {
  16. Json::Value value;
  17. Json::CharReaderBuilder builder;
  18. std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  19. std::string error;
  20. if (not reader->parse(data, data + len, &value, &error)) {
  21. throw std::runtime_error(error);
  22. }
  23. return value;
  24. }
  25. MATCHER_P(Expected, m, "") {
  26. if (arg.has_value()) {
  27. return testing::ExplainMatchResult(m, arg.value(), result_listener);
  28. }
  29. return false;
  30. }
  31. MATCHER_P(Unexpected, m, "") {
  32. if (not arg.has_value()) {
  33. return testing::ExplainMatchResult(m, arg.error(), result_listener);
  34. }
  35. return false;
  36. }
  37. MATCHER(Valid, "") { return arg.valid(); }
  38. MATCHER_P(HasAnnotationsFor, doc_path, "") { return arg.has(doc_path); }
  39. MATCHER_P2(HasAnnotationAt, doc_path, schema_path, "") { return arg.has(doc_path, schema_path); }
  40. MATCHER_P4(AnnotationAt, doc_path, schema_path, key, matcher, "") {
  41. auto const * anno = arg.annotation(doc_path, schema_path, key);
  42. *result_listener << "\ninstanceLocation: " << doc_path << " is "
  43. << (arg.has(doc_path) ? "present" : "missing");
  44. *result_listener << "\nevaluationLocation: " << schema_path << " is "
  45. << (arg.has(doc_path, schema_path) ? "present" : "missing");
  46. if (not anno) {
  47. *result_listener << "\nannotation: (nil)";
  48. return false;
  49. }
  50. *result_listener << "\nannotation: " << testing::PrintToString(*anno);
  51. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  52. }
  53. template <typename M> auto AnnotationAt(std::string const & key, M && matcher) {
  54. return AnnotationAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
  55. std::forward<M>(matcher));
  56. }
  57. MATCHER_P4(ErrorAt, doc_path, schema_path, key, matcher, "") {
  58. auto const * anno = arg.error(doc_path, schema_path, key);
  59. *result_listener << "\ninstanceLocation: " << doc_path << " is "
  60. << (arg.has(doc_path) ? "present" : "missing");
  61. *result_listener << "\nevaluationLocation: " << schema_path << " is "
  62. << (arg.has(doc_path, schema_path) ? "present" : "missing");
  63. if (not anno) {
  64. *result_listener << "\nannotation: (nil)";
  65. return false;
  66. }
  67. *result_listener << "\nannotation: " << testing::PrintToString(*anno);
  68. return testing::ExplainMatchResult(matcher, *anno, result_listener);
  69. }
  70. template <typename M> auto ErrorAt(std::string const & key, M && matcher) {
  71. return ErrorAt(jvalidate::detail::Pointer(), jvalidate::detail::Pointer(), key,
  72. std::forward<M>(matcher));
  73. }