protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_EXPECTING_OBJECTWRITER_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_EXPECTING_OBJECTWRITER_H__
33 
34 // An implementation of ObjectWriter that automatically sets the
35 // gmock expectations for the response to a method. Every method
36 // returns the object itself for chaining.
37 //
38 // Usage:
39 // // Setup
40 // MockObjectWriter mock;
41 // ExpectingObjectWriter ow(&mock);
42 //
43 // // Set expectation
44 // ow.StartObject("")
45 // ->RenderString("key", "value")
46 // ->EndObject();
47 //
48 // // Actual testing
49 // mock.StartObject(StringPiece())
50 // ->RenderString("key", "value")
51 // ->EndObject();
52 
53 #include <cstdint>
54 
55 #include <google/protobuf/stubs/common.h>
56 #include <google/protobuf/util/internal/object_writer.h>
57 #include <gmock/gmock.h>
58 #include <google/protobuf/stubs/strutil.h>
59 
60 namespace google {
61 namespace protobuf {
62 namespace util {
63 namespace converter {
64 
65 using testing::Eq;
66 using testing::IsEmpty;
69 using testing::Return;
70 using testing::StrEq;
71 using testing::TypedEq;
72 
73 class MockObjectWriter : public ObjectWriter {
74  public:
76 
78  MOCK_METHOD(ObjectWriter*, EndObject, (), (override));
80  MOCK_METHOD(ObjectWriter*, EndList, (), (override));
81  MOCK_METHOD(ObjectWriter*, RenderBool, (StringPiece, bool), (override));
83  (override));
85  (override));
87  (override));
89  (override));
91  (override));
93  (override));
95  (StringPiece, StringPiece), (override));
97  (override));
99 };
100 
101 class ExpectingObjectWriter : public ObjectWriter {
102  public:
103  explicit ExpectingObjectWriter(MockObjectWriter* mock) : mock_(mock) {}
104 
106  (name.empty() ? EXPECT_CALL(*mock_, StartObject(IsEmpty()))
108  .WillOnce(Return(mock_))
109  .RetiresOnSaturation();
110  return this;
111  }
112 
113  virtual ObjectWriter* EndObject() {
115  .WillOnce(Return(mock_))
116  .RetiresOnSaturation();
117  return this;
118  }
119 
121  (name.empty() ? EXPECT_CALL(*mock_, StartList(IsEmpty()))
123  .WillOnce(Return(mock_))
124  .RetiresOnSaturation();
125  return this;
126  }
127 
128  virtual ObjectWriter* EndList() {
130  .WillOnce(Return(mock_))
131  .RetiresOnSaturation();
132  return this;
133  }
134 
136  (name.empty()
137  ? EXPECT_CALL(*mock_, RenderBool(IsEmpty(), TypedEq<bool>(value)))
138  : EXPECT_CALL(*mock_,
139  RenderBool(Eq(std::string(name)), TypedEq<bool>(value))))
140  .WillOnce(Return(mock_))
141  .RetiresOnSaturation();
142  return this;
143  }
144 
146  (name.empty()
147  ? EXPECT_CALL(*mock_, RenderInt32(IsEmpty(), TypedEq<int32_t>(value)))
149  TypedEq<int32_t>(value))))
150  .WillOnce(Return(mock_))
151  .RetiresOnSaturation();
152  return this;
153  }
154 
156  (name.empty() ? EXPECT_CALL(*mock_, RenderUint32(IsEmpty(),
157  TypedEq<uint32_t>(value)))
159  TypedEq<uint32_t>(value))))
160  .WillOnce(Return(mock_))
161  .RetiresOnSaturation();
162  return this;
163  }
164 
166  (name.empty()
167  ? EXPECT_CALL(*mock_, RenderInt64(IsEmpty(), TypedEq<int64_t>(value)))
169  TypedEq<int64_t>(value))))
170  .WillOnce(Return(mock_))
171  .RetiresOnSaturation();
172  return this;
173  }
174 
176  (name.empty() ? EXPECT_CALL(*mock_, RenderUint64(IsEmpty(),
177  TypedEq<uint64_t>(value)))
179  TypedEq<uint64_t>(value))))
180  .WillOnce(Return(mock_))
181  .RetiresOnSaturation();
182  return this;
183  }
184 
186  (name.empty()
187  ? EXPECT_CALL(*mock_,
191  .WillOnce(Return(mock_))
192  .RetiresOnSaturation();
193  return this;
194  }
195 
197  (name.empty()
198  ? EXPECT_CALL(*mock_,
202  .WillOnce(Return(mock_))
203  .RetiresOnSaturation();
204  return this;
205  }
206 
208  StringPiece value) {
209  (name.empty() ? EXPECT_CALL(*mock_, RenderString(IsEmpty(),
210  TypedEq<StringPiece>(
211  std::string(value))))
213  TypedEq<StringPiece>(
214  std::string(value)))))
215  .WillOnce(Return(mock_))
216  .RetiresOnSaturation();
217  return this;
218  }
220  (name.empty()
221  ? EXPECT_CALL(*mock_, RenderBytes(IsEmpty(), TypedEq<StringPiece>(
222  value.ToString())))
223  : EXPECT_CALL(*mock_,
225  TypedEq<StringPiece>(value.ToString()))))
226  .WillOnce(Return(mock_))
227  .RetiresOnSaturation();
228  return this;
229  }
230 
232  (name.empty() ? EXPECT_CALL(*mock_, RenderNull(IsEmpty()))
234  .WillOnce(Return(mock_))
235  .RetiresOnSaturation());
236  return this;
237  }
238 
239  private:
241 
243 };
244 
245 } // namespace converter
246 } // namespace util
247 } // namespace protobuf
248 } // namespace google
249 
250 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_EXPECTING_OBJECTWRITER_H__
google::protobuf::util::converter::ExpectingObjectWriter::RenderInt32
virtual ObjectWriter * RenderInt32(StringPiece name, int32_t value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:145
google::protobuf::util::converter::ExpectingObjectWriter::ExpectingObjectWriter
ExpectingObjectWriter(MockObjectWriter *mock)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:103
google::protobuf::util::converter::ExpectingObjectWriter::mock_
MockObjectWriter * mock_
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:230
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::util::converter::ObjectWriter::StartList
virtual ObjectWriter * StartList(StringPiece name)=0
testing::Return
internal::ReturnAction< R > Return(R value)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-actions.h:1004
google::protobuf::util::converter::ExpectingObjectWriter::RenderNull
virtual ObjectWriter * RenderNull(StringPiece name)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:231
google::protobuf::util::converter::ObjectWriter::RenderUint64
virtual ObjectWriter * RenderUint64(StringPiece name, uint64 value)=0
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
make_cmakelists.converter
converter
Definition: make_cmakelists.py:317
google::protobuf::util::converter::ObjectWriter
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/object_writer.h:61
setup.name
name
Definition: setup.py:542
google::protobuf::util::converter::ExpectingObjectWriter
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:91
google::protobuf::util::converter::MockObjectWriter::MOCK_METHOD
MOCK_METHOD(ObjectWriter *, StartObject,(StringPiece),(override))
testing::NanSensitiveFloatEq
internal::FloatingEqMatcher< float > NanSensitiveFloatEq(float rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8668
google::protobuf::util::converter::MockObjectWriter
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:70
google::protobuf::util::converter::ObjectWriter::RenderDouble
virtual ObjectWriter * RenderDouble(StringPiece name, double value)=0
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf::util::converter::ExpectingObjectWriter::RenderBytes
virtual ObjectWriter * RenderBytes(StringPiece name, StringPiece value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:219
google::protobuf::util::converter::ExpectingObjectWriter::RenderInt64
virtual ObjectWriter * RenderInt64(StringPiece name, int64 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:155
google::protobuf::util::converter::ObjectWriter::RenderBool
virtual ObjectWriter * RenderBool(StringPiece name, bool value)=0
google::protobuf::util::converter::ExpectingObjectWriter::RenderFloat
virtual ObjectWriter * RenderFloat(StringPiece name, float value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:196
google::protobuf::util::converter::ObjectWriter::RenderInt32
virtual ObjectWriter * RenderInt32(StringPiece name, int32 value)=0
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf::util::converter::ObjectWriter::StartObject
virtual ObjectWriter * StartObject(StringPiece name)=0
google::protobuf::util::converter::ExpectingObjectWriter::GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ExpectingObjectWriter)
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::util::converter::ExpectingObjectWriter::RenderDouble
virtual ObjectWriter * RenderDouble(StringPiece name, double value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:185
google::protobuf::util::converter::MockObjectWriter::MockObjectWriter
MockObjectWriter()
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:75
google::protobuf::util::converter::ExpectingObjectWriter::RenderInt64
virtual ObjectWriter * RenderInt64(StringPiece name, int64_t value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:165
google::protobuf::util::converter::ObjectWriter::EndList
virtual ObjectWriter * EndList()=0
EXPECT_CALL
#define EXPECT_CALL(obj, call)
google::protobuf::util::converter::ExpectingObjectWriter::StartList
virtual ObjectWriter * StartList(StringPiece name)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:120
google::protobuf::util::converter::ObjectWriter::RenderBytes
virtual ObjectWriter * RenderBytes(StringPiece name, StringPiece value)=0
google::protobuf::util::converter::ExpectingObjectWriter::RenderBool
virtual ObjectWriter * RenderBool(StringPiece name, bool value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:135
google::protobuf::util::converter::ExpectingObjectWriter::StartObject
virtual ObjectWriter * StartObject(StringPiece name)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:105
google::protobuf::util::converter::ObjectWriter::RenderNull
virtual ObjectWriter * RenderNull(StringPiece name)=0
google::protobuf::util::converter::ObjectWriter::RenderString
virtual ObjectWriter * RenderString(StringPiece name, StringPiece value)=0
testing::TypedEq
Matcher< Lhs > TypedEq(const Rhs &rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8581
google::protobuf::util::converter::ExpectingObjectWriter::EndObject
virtual ObjectWriter * EndObject()
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:113
absl::container_internal::IsEmpty
bool IsEmpty(ctrl_t c)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:489
google::protobuf::util::converter::ExpectingObjectWriter::RenderUint64
virtual ObjectWriter * RenderUint64(StringPiece name, uint64 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:165
google::protobuf::util::converter::ExpectingObjectWriter::RenderUint64
virtual ObjectWriter * RenderUint64(StringPiece name, uint64_t value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:175
google::protobuf::util::converter::ExpectingObjectWriter::EndList
virtual ObjectWriter * EndList()
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:128
google::protobuf::util::converter::ExpectingObjectWriter::RenderString
virtual ObjectWriter * RenderString(StringPiece name, StringPiece value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:207
google::protobuf::util::converter::ExpectingObjectWriter::RenderInt32
virtual ObjectWriter * RenderInt32(StringPiece name, int32 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:135
google::protobuf::util::converter::ExpectingObjectWriter::RenderUint32
virtual ObjectWriter * RenderUint32(StringPiece name, uint32_t value)
Definition: protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:155
google::protobuf::util::converter::ObjectWriter::RenderUint32
virtual ObjectWriter * RenderUint32(StringPiece name, uint32 value)=0
testing::NanSensitiveDoubleEq
internal::FloatingEqMatcher< double > NanSensitiveDoubleEq(double rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8640
testing::StrEq
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrEq(const internal::string &str)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8774
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::util::converter::ExpectingObjectWriter::RenderUint32
virtual ObjectWriter * RenderUint32(StringPiece name, uint32 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/expecting_objectwriter.h:145
google::protobuf::util::converter::ObjectWriter::RenderFloat
virtual ObjectWriter * RenderFloat(StringPiece name, float value)=0
google::protobuf::util::converter::ObjectWriter::EndObject
virtual ObjectWriter * EndObject()=0
google::protobuf::util::converter::ObjectWriter::RenderInt64
virtual ObjectWriter * RenderInt64(StringPiece name, int64 value)=0


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:20