File: status_test_util.h

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (94 lines) | stat: -rw-r--r-- 2,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef CERT_TRANS_UTIL_STATUS_TEST_UTIL_H_
#define CERT_TRANS_UTIL_STATUS_TEST_UTIL_H_

#include <gmock/gmock-matchers.h>

#include "util/status.h"
#include "util/statusor.h"
#include "util/sync_task.h"
#include "util/task.h"

namespace util {
namespace testing {


class StatusIsMatcher : public ::testing::MatcherInterface<Status> {
 public:
  StatusIsMatcher(const ::testing::Matcher<error::Code>& code_matcher,
                  const ::testing::Matcher<std::string>& message_matcher)
      : code_matcher_(code_matcher), message_matcher_(message_matcher) {
  }

  bool MatchAndExplain(
      Status status, ::testing::MatchResultListener* listener) const override {
    if (!code_matcher_.MatchAndExplain(status.CanonicalCode(), listener)) {
      return false;
    }

    if (!message_matcher_.MatchAndExplain(status.error_message(), listener)) {
      return false;
    }

    return true;
  }

  void DescribeTo(std::ostream* os) const override {
    *os << "util::Status(code ";
    code_matcher_.DescribeTo(os);
    *os << " and message ";
    message_matcher_.DescribeTo(os);
    *os << ")";
  }

 private:
  const ::testing::Matcher<error::Code> code_matcher_;
  const ::testing::Matcher<std::string> message_matcher_;
};


::testing::Matcher<Status> StatusIs(
    const ::testing::Matcher<error::Code>& code_matcher,
    const ::testing::Matcher<std::string>& message_matcher) {
  return ::testing::MakeMatcher(
      new StatusIsMatcher(code_matcher, message_matcher));
}


::testing::Matcher<Status> StatusIs(
    const ::testing::Matcher<error::Code>& code_matcher) {
  return ::testing::MakeMatcher(
      new StatusIsMatcher(code_matcher, ::testing::_));
}


inline ::util::Status ToStatus(const ::util::Status& status) {
  return status;
}


inline ::util::Status ToStatus(::util::Task* task) {
  return task->status();
}


inline ::util::Status ToStatus(const ::util::SyncTask& task) {
  return task.status();
}


template <class T>
::util::Status ToStatus(const ::util::StatusOr<T>& statusor) {
  return statusor.status();
}


#define EXPECT_OK(expr) \
  EXPECT_EQ(util::Status::OK, util::testing::ToStatus(expr))
#define ASSERT_OK(expr) \
  ASSERT_EQ(util::Status::OK, util::testing::ToStatus(expr))


}  // namespace testing
}  // namespace util

#endif  // CERT_TRANS_UTIL_STATUS_TEST_UTIL_H_