Kinetic C/C++ Client
 All Classes Functions Variables Pages
matchers.h
1 /*
2  * kinetic-cpp-client
3  * Copyright (C) 2014 Seagate Technology.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #ifndef KINETIC_CPP_CLIENT_MATCHERS_H_
22 #define KINETIC_CPP_CLIENT_MATCHERS_H_
23 
24 #include <memory>
25 
26 #include "gmock/gmock.h"
27 
28 namespace kinetic {
29 
30 using std::shared_ptr;
31 using std::string;
32 using std::vector;
33 using ::testing::MatcherInterface;
34 using ::testing::Matcher;
35 
36 class StringSharedPtrMatcher : public MatcherInterface<shared_ptr<const string>> {
37  public:
38  StringSharedPtrMatcher(const std::string s) : s_(s) {}
39 
40  virtual bool MatchAndExplain(const shared_ptr<const string> other,
41  testing::MatchResultListener *listener) const {
42  if (s_ == *other) {
43  return true;
44  }
45  *listener << "expected = <" << s_ << ">, actual = <" << *other << ">";
46  return false;
47  }
48 
49  virtual void DescribeTo(::std::ostream *os) const {
50  *os << "s=" << s_;
51  }
52 
53  private:
54  const std::string s_;
55 };
56 
57 inline Matcher<shared_ptr<const string>> StringSharedPtrEq(const string s) {
58  return MakeMatcher(new StringSharedPtrMatcher(s));
59 };
60 
61 class VectorStringPtrMatcher : public MatcherInterface<vector<string>*> {
62  public:
63  VectorStringPtrMatcher(vector<string> v) : v_(v) {}
64 
65  virtual bool MatchAndExplain(vector<string>* other,
66  testing::MatchResultListener *listener) const {
67  if (v_ == *other) {
68  return true;
69  }
70  *listener << "expected = <" << v_.size() << ">, actual = <" << other->size() << ">";
71  return false;
72  }
73 
74  virtual void DescribeTo(::std::ostream *os) const {
75  *os << "v=" << v_.size();
76  }
77 
78  private:
79  vector<string> v_;
80 };
81 
82 inline Matcher<vector<string>*> VectorStringPtrEq(vector<string> v) {
83  return MakeMatcher(new VectorStringPtrMatcher(v));
84 }
85 
86 class KineticStatusMatcher : public MatcherInterface<KineticStatus> {
87  public:
88  KineticStatusMatcher(StatusCode code, string message) : code_(code), message_(message) {}
89 
90  virtual bool MatchAndExplain(KineticStatus other,
91  testing::MatchResultListener *listener) const {
92  if (code_ == other.statusCode() && message_ == other.message()) {
93  return true;
94  }
95 
96  *listener << "expected = <" << static_cast<int>(code_) << ":" << message_ << ">, actual = <"
97  << static_cast<int>(other.statusCode()) << ":" << other.message() << ">";
98  return false;
99  }
100 
101  virtual void DescribeTo(::std::ostream *os) const {
102  *os << "expected = <" << static_cast<int>(code_) << ":" << message_ << ">";
103  }
104 
105  private:
106  StatusCode code_;
107  string message_;
108 };
109 
110 inline Matcher<KineticStatus> KineticStatusEq(StatusCode code, string message) {
111  return MakeMatcher(new KineticStatusMatcher(code, message));
112 }
113 
114 } // namespace kinetic
115 #endif // KINETIC_CPP_CLIENT_MATCHERS_H_
Indicates whether a Kinetic operation (get, put, security, etc) put succeeded or failed. Unlike Status it provides details like whether the failure resulted from a version or an HMAC error.