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