status_test_support.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "status_test_support.h"
  5. namespace crdtp {
  6. void PrintTo(const Status& status, std::ostream* os) {
  7. *os << status.ToASCIIString() << " (error: 0x" << std::hex
  8. << static_cast<int>(status.error) << ", "
  9. << "pos: " << std::dec << status.pos << ")";
  10. }
  11. namespace {
  12. class StatusIsMatcher : public testing::MatcherInterface<Status> {
  13. public:
  14. explicit StatusIsMatcher(Status status) : expected_(status) {}
  15. bool MatchAndExplain(Status status,
  16. testing::MatchResultListener* listener) const override {
  17. return status.error == expected_.error && status.pos == expected_.pos;
  18. }
  19. void DescribeTo(std::ostream* os) const override {
  20. *os << "equals to ";
  21. PrintTo(expected_, os);
  22. }
  23. private:
  24. Status expected_;
  25. };
  26. class StatusIsOkMatcher : public testing::MatcherInterface<Status> {
  27. bool MatchAndExplain(Status status,
  28. testing::MatchResultListener* listener) const override {
  29. return status.ok();
  30. }
  31. void DescribeTo(std::ostream* os) const override { *os << "is ok"; }
  32. };
  33. } // namespace
  34. testing::Matcher<Status> StatusIsOk() {
  35. return MakeMatcher(new StatusIsOkMatcher());
  36. }
  37. testing::Matcher<Status> StatusIs(Error error, size_t pos) {
  38. return MakeMatcher(new StatusIsMatcher(Status(error, pos)));
  39. }
  40. } // namespace crdtp