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 <google/protobuf/stubs/common.h> 54 #include <google/protobuf/util/internal/object_writer.h> 55 #include <gmock/gmock.h> 56 #include <google/protobuf/stubs/strutil.h> 57 58 namespace google { 59 namespace protobuf { 60 namespace util { 61 namespace converter { 62 63 using testing::Eq; 64 using testing::IsEmpty; 65 using testing::NanSensitiveDoubleEq; 66 using testing::NanSensitiveFloatEq; 67 using testing::Return; 68 using testing::StrEq; 69 using testing::TypedEq; 70 71 class MockObjectWriter : public ObjectWriter { 72 public: MockObjectWriter()73 MockObjectWriter() {} 74 75 MOCK_METHOD(ObjectWriter*, StartObject, (StringPiece), (override)); 76 MOCK_METHOD(ObjectWriter*, EndObject, (), (override)); 77 MOCK_METHOD(ObjectWriter*, StartList, (StringPiece), (override)); 78 MOCK_METHOD(ObjectWriter*, EndList, (), (override)); 79 MOCK_METHOD(ObjectWriter*, RenderBool, (StringPiece, bool), (override)); 80 MOCK_METHOD(ObjectWriter*, RenderInt32, (StringPiece, int32), 81 (override)); 82 MOCK_METHOD(ObjectWriter*, RenderUint32, (StringPiece, uint32), 83 (override)); 84 MOCK_METHOD(ObjectWriter*, RenderInt64, (StringPiece, int64), 85 (override)); 86 MOCK_METHOD(ObjectWriter*, RenderUint64, (StringPiece, uint64), 87 (override)); 88 MOCK_METHOD(ObjectWriter*, RenderDouble, (StringPiece, double), 89 (override)); 90 MOCK_METHOD(ObjectWriter*, RenderFloat, (StringPiece, float), 91 (override)); 92 MOCK_METHOD(ObjectWriter*, RenderString, 93 (StringPiece, StringPiece), (override)); 94 MOCK_METHOD(ObjectWriter*, RenderBytes, (StringPiece, StringPiece)); 95 MOCK_METHOD(ObjectWriter*, RenderNull, (StringPiece), (override)); 96 }; 97 98 class ExpectingObjectWriter : public ObjectWriter { 99 public: ExpectingObjectWriter(MockObjectWriter * mock)100 explicit ExpectingObjectWriter(MockObjectWriter* mock) : mock_(mock) {} 101 StartObject(StringPiece name)102 virtual ObjectWriter* StartObject(StringPiece name) { 103 (name.empty() ? EXPECT_CALL(*mock_, StartObject(IsEmpty())) 104 : EXPECT_CALL(*mock_, StartObject(Eq(std::string(name))))) 105 .WillOnce(Return(mock_)) 106 .RetiresOnSaturation(); 107 return this; 108 } 109 EndObject()110 virtual ObjectWriter* EndObject() { 111 EXPECT_CALL(*mock_, EndObject()) 112 .WillOnce(Return(mock_)) 113 .RetiresOnSaturation(); 114 return this; 115 } 116 StartList(StringPiece name)117 virtual ObjectWriter* StartList(StringPiece name) { 118 (name.empty() ? EXPECT_CALL(*mock_, StartList(IsEmpty())) 119 : EXPECT_CALL(*mock_, StartList(Eq(std::string(name))))) 120 .WillOnce(Return(mock_)) 121 .RetiresOnSaturation(); 122 return this; 123 } 124 EndList()125 virtual ObjectWriter* EndList() { 126 EXPECT_CALL(*mock_, EndList()) 127 .WillOnce(Return(mock_)) 128 .RetiresOnSaturation(); 129 return this; 130 } 131 RenderBool(StringPiece name,bool value)132 virtual ObjectWriter* RenderBool(StringPiece name, bool value) { 133 (name.empty() 134 ? EXPECT_CALL(*mock_, RenderBool(IsEmpty(), TypedEq<bool>(value))) 135 : EXPECT_CALL(*mock_, 136 RenderBool(Eq(std::string(name)), TypedEq<bool>(value)))) 137 .WillOnce(Return(mock_)) 138 .RetiresOnSaturation(); 139 return this; 140 } 141 RenderInt32(StringPiece name,int32 value)142 virtual ObjectWriter* RenderInt32(StringPiece name, int32 value) { 143 (name.empty() 144 ? EXPECT_CALL(*mock_, RenderInt32(IsEmpty(), TypedEq<int32>(value))) 145 : EXPECT_CALL(*mock_, RenderInt32(Eq(std::string(name)), 146 TypedEq<int32>(value)))) 147 .WillOnce(Return(mock_)) 148 .RetiresOnSaturation(); 149 return this; 150 } 151 RenderUint32(StringPiece name,uint32 value)152 virtual ObjectWriter* RenderUint32(StringPiece name, uint32 value) { 153 (name.empty() 154 ? EXPECT_CALL(*mock_, RenderUint32(IsEmpty(), TypedEq<uint32>(value))) 155 : EXPECT_CALL(*mock_, RenderUint32(Eq(std::string(name)), 156 TypedEq<uint32>(value)))) 157 .WillOnce(Return(mock_)) 158 .RetiresOnSaturation(); 159 return this; 160 } 161 RenderInt64(StringPiece name,int64 value)162 virtual ObjectWriter* RenderInt64(StringPiece name, int64 value) { 163 (name.empty() 164 ? EXPECT_CALL(*mock_, RenderInt64(IsEmpty(), TypedEq<int64>(value))) 165 : EXPECT_CALL(*mock_, RenderInt64(Eq(std::string(name)), 166 TypedEq<int64>(value)))) 167 .WillOnce(Return(mock_)) 168 .RetiresOnSaturation(); 169 return this; 170 } 171 RenderUint64(StringPiece name,uint64 value)172 virtual ObjectWriter* RenderUint64(StringPiece name, uint64 value) { 173 (name.empty() 174 ? EXPECT_CALL(*mock_, RenderUint64(IsEmpty(), TypedEq<uint64>(value))) 175 : EXPECT_CALL(*mock_, RenderUint64(Eq(std::string(name)), 176 TypedEq<uint64>(value)))) 177 .WillOnce(Return(mock_)) 178 .RetiresOnSaturation(); 179 return this; 180 } 181 RenderDouble(StringPiece name,double value)182 virtual ObjectWriter* RenderDouble(StringPiece name, double value) { 183 (name.empty() 184 ? EXPECT_CALL(*mock_, 185 RenderDouble(IsEmpty(), NanSensitiveDoubleEq(value))) 186 : EXPECT_CALL(*mock_, RenderDouble(Eq(std::string(name)), 187 NanSensitiveDoubleEq(value)))) 188 .WillOnce(Return(mock_)) 189 .RetiresOnSaturation(); 190 return this; 191 } 192 RenderFloat(StringPiece name,float value)193 virtual ObjectWriter* RenderFloat(StringPiece name, float value) { 194 (name.empty() 195 ? EXPECT_CALL(*mock_, 196 RenderFloat(IsEmpty(), NanSensitiveFloatEq(value))) 197 : EXPECT_CALL(*mock_, RenderFloat(Eq(std::string(name)), 198 NanSensitiveFloatEq(value)))) 199 .WillOnce(Return(mock_)) 200 .RetiresOnSaturation(); 201 return this; 202 } 203 RenderString(StringPiece name,StringPiece value)204 virtual ObjectWriter* RenderString(StringPiece name, 205 StringPiece value) { 206 (name.empty() ? EXPECT_CALL(*mock_, RenderString(IsEmpty(), 207 TypedEq<StringPiece>( 208 std::string(value)))) 209 : EXPECT_CALL(*mock_, RenderString(Eq(std::string(name)), 210 TypedEq<StringPiece>( 211 std::string(value))))) 212 .WillOnce(Return(mock_)) 213 .RetiresOnSaturation(); 214 return this; 215 } RenderBytes(StringPiece name,StringPiece value)216 virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) { 217 (name.empty() 218 ? EXPECT_CALL(*mock_, RenderBytes(IsEmpty(), TypedEq<StringPiece>( 219 value.ToString()))) 220 : EXPECT_CALL(*mock_, 221 RenderBytes(Eq(std::string(name)), 222 TypedEq<StringPiece>(value.ToString())))) 223 .WillOnce(Return(mock_)) 224 .RetiresOnSaturation(); 225 return this; 226 } 227 RenderNull(StringPiece name)228 virtual ObjectWriter* RenderNull(StringPiece name) { 229 (name.empty() ? EXPECT_CALL(*mock_, RenderNull(IsEmpty())) 230 : EXPECT_CALL(*mock_, RenderNull(Eq(std::string(name)))) 231 .WillOnce(Return(mock_)) 232 .RetiresOnSaturation()); 233 return this; 234 } 235 236 private: 237 MockObjectWriter* mock_; 238 239 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ExpectingObjectWriter); 240 }; 241 242 } // namespace converter 243 } // namespace util 244 } // namespace protobuf 245 } // namespace google 246 247 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_EXPECTING_OBJECTWRITER_H__ 248