| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 import string | |
| 7 import sys | |
| 8 | |
| 9 HEADER = """\ | |
| 10 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 11 // Use of this source code is governed by a BSD-style license that can be | |
| 12 // found in the LICENSE file. | |
| 13 | |
| 14 // This file automatically generated by testing/generate_gmock_mutant.py. | |
| 15 // DO NOT EDIT. | |
| 16 | |
| 17 #ifndef TESTING_GMOCK_MUTANT_H_ | |
| 18 #define TESTING_GMOCK_MUTANT_H_ | |
| 19 | |
| 20 // The intention of this file is to make possible using GMock actions in | |
| 21 // all of its syntactic beauty. Classes and helper functions can be used as | |
| 22 // more generic variants of Task and Callback classes (see base/task.h) | |
| 23 // Mutant supports both pre-bound arguments (like Task) and call-time | |
| 24 // arguments (like Callback) - hence the name. :-) | |
| 25 // | |
| 26 // DispatchToMethod/Function supports two sets of arguments: pre-bound (P) and | |
| 27 // call-time (C). The arguments as well as the return type are templatized. | |
| 28 // DispatchToMethod/Function will also try to call the selected method or | |
| 29 // function even if provided pre-bound arguments does not match exactly with | |
| 30 // the function signature hence the X1, X2 ... XN parameters in CreateFunctor. | |
| 31 // DispatchToMethod will try to invoke method that may not belong to the | |
| 32 // object's class itself but to the object's class base class. | |
| 33 // | |
| 34 // Additionally you can bind the object at calltime by binding a pointer to | |
| 35 // pointer to the object at creation time - before including this file you | |
| 36 // have to #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING. | |
| 37 // | |
| 38 // TODO(stoyan): It's yet not clear to me should we use T& and T&* instead | |
| 39 // of T* and T** when we invoke CreateFunctor to match the EXPECT_CALL style. | |
| 40 // | |
| 41 // | |
| 42 // Sample usage with gMock: | |
| 43 // | |
| 44 // struct Mock : public ObjectDelegate { | |
| 45 // MOCK_METHOD2(string, OnRequest(int n, const string& request)); | |
| 46 // MOCK_METHOD1(void, OnQuit(int exit_code)); | |
| 47 // MOCK_METHOD2(void, LogMessage(int level, const string& message)); | |
| 48 // | |
| 49 // string HandleFlowers(const string& reply, int n, const string& request) { | |
| 50 // string result = SStringPrintf("In request of %d %s ", n, request); | |
| 51 // for (int i = 0; i < n; ++i) result.append(reply) | |
| 52 // return result; | |
| 53 // } | |
| 54 // | |
| 55 // void DoLogMessage(int level, const string& message) { | |
| 56 // } | |
| 57 // | |
| 58 // void QuitMessageLoop(int seconds) { | |
| 59 // base::MessageLoop* loop = base::MessageLoop::current(); | |
| 60 // loop->PostDelayedTask(FROM_HERE, | |
| 61 // base::MessageLoop::QuitWhenIdleClosure(), | |
| 62 // 1000 * seconds); | |
| 63 // } | |
| 64 // }; | |
| 65 // | |
| 66 // Mock mock; | |
| 67 // // Will invoke mock.HandleFlowers("orchids", n, request) | |
| 68 // // "orchids" is a pre-bound argument, and <n> and <request> are call-time | |
| 69 // // arguments - they are not known until the OnRequest mock is invoked. | |
| 70 // EXPECT_CALL(mock, OnRequest(Ge(5), base::StartsWith("flower")) | |
| 71 // .Times(1) | |
| 72 // .WillOnce(Invoke(CreateFunctor(&mock, &Mock::HandleFlowers, | |
| 73 // string("orchids")))); | |
| 74 // | |
| 75 // | |
| 76 // // No pre-bound arguments, two call-time arguments passed | |
| 77 // // directly to DoLogMessage | |
| 78 // EXPECT_CALL(mock, OnLogMessage(_, _)) | |
| 79 // .Times(AnyNumber()) | |
| 80 // .WillAlways(Invoke(CreateFunctor, &mock, &Mock::DoLogMessage)); | |
| 81 // | |
| 82 // | |
| 83 // // In this case we have a single pre-bound argument - 3. We ignore | |
| 84 // // all of the arguments of OnQuit. | |
| 85 // EXCEPT_CALL(mock, OnQuit(_)) | |
| 86 // .Times(1) | |
| 87 // .WillOnce(InvokeWithoutArgs(CreateFunctor( | |
| 88 // &mock, &Mock::QuitMessageLoop, 3))); | |
| 89 // | |
| 90 // MessageLoop loop; | |
| 91 // loop.Run(); | |
| 92 // | |
| 93 // | |
| 94 // // Here is another example of how we can set an action that invokes | |
| 95 // // method of an object that is not yet created. | |
| 96 // struct Mock : public ObjectDelegate { | |
| 97 // MOCK_METHOD1(void, DemiurgeCreated(Demiurge*)); | |
| 98 // MOCK_METHOD2(void, OnRequest(int count, const string&)); | |
| 99 // | |
| 100 // void StoreDemiurge(Demiurge* w) { | |
| 101 // demiurge_ = w; | |
| 102 // } | |
| 103 // | |
| 104 // Demiurge* demiurge; | |
| 105 // } | |
| 106 // | |
| 107 // EXPECT_CALL(mock, DemiurgeCreated(_)).Times(1) | |
| 108 // .WillOnce(Invoke(CreateFunctor(&mock, &Mock::StoreDemiurge))); | |
| 109 // | |
| 110 // EXPECT_CALL(mock, OnRequest(_, StrEq("Moby Dick"))) | |
| 111 // .Times(AnyNumber()) | |
| 112 // .WillAlways(WithArgs<0>(Invoke( | |
| 113 // CreateFunctor(&mock->demiurge_, &Demiurge::DecreaseMonsters)))); | |
| 114 // | |
| 115 | |
| 116 #include "base/memory/linked_ptr.h" | |
| 117 #include "base/tuple.h" | |
| 118 | |
| 119 namespace testing {""" | |
| 120 | |
| 121 MUTANT = """\ | |
| 122 | |
| 123 // Interface that is exposed to the consumer, that does the actual calling | |
| 124 // of the method. | |
| 125 template <typename R, typename Params> | |
| 126 class MutantRunner { | |
| 127 public: | |
| 128 virtual R RunWithParams(const Params& params) = 0; | |
| 129 virtual ~MutantRunner() {} | |
| 130 }; | |
| 131 | |
| 132 // Mutant holds pre-bound arguments (like Task). Like Callback | |
| 133 // allows call-time arguments. You bind a pointer to the object | |
| 134 // at creation time. | |
| 135 template <typename R, typename T, typename Method, | |
| 136 typename PreBound, typename Params> | |
| 137 class Mutant : public MutantRunner<R, Params> { | |
| 138 public: | |
| 139 Mutant(T* obj, Method method, const PreBound& pb) | |
| 140 : obj_(obj), method_(method), pb_(pb) { | |
| 141 } | |
| 142 | |
| 143 // MutantRunner implementation | |
| 144 virtual R RunWithParams(const Params& params) { | |
| 145 return DispatchToMethod<R>(this->obj_, this->method_, pb_, params); | |
| 146 } | |
| 147 | |
| 148 T* obj_; | |
| 149 Method method_; | |
| 150 PreBound pb_; | |
| 151 }; | |
| 152 | |
| 153 template <typename R, typename Function, typename PreBound, typename Params> | |
| 154 class MutantFunction : public MutantRunner<R, Params> { | |
| 155 public: | |
| 156 MutantFunction(Function function, const PreBound& pb) | |
| 157 : function_(function), pb_(pb) { | |
| 158 } | |
| 159 | |
| 160 // MutantRunner implementation | |
| 161 virtual R RunWithParams(const Params& params) { | |
| 162 return DispatchToFunction<R>(function_, pb_, params); | |
| 163 } | |
| 164 | |
| 165 Function function_; | |
| 166 PreBound pb_; | |
| 167 }; | |
| 168 | |
| 169 #ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING | |
| 170 // MutantLateBind is like Mutant, but you bind a pointer to a pointer | |
| 171 // to the object. This way you can create actions for an object | |
| 172 // that is not yet created (has only storage for a pointer to it). | |
| 173 template <typename R, typename T, typename Method, | |
| 174 typename PreBound, typename Params> | |
| 175 class MutantLateObjectBind : public MutantRunner<R, Params> { | |
| 176 public: | |
| 177 MutantLateObjectBind(T** obj, Method method, const PreBound& pb) | |
| 178 : obj_(obj), method_(method), pb_(pb) { | |
| 179 } | |
| 180 | |
| 181 // MutantRunner implementation. | |
| 182 virtual R RunWithParams(const Params& params) { | |
| 183 EXPECT_THAT(*this->obj_, testing::NotNull()); | |
| 184 if (NULL == *this->obj_) | |
| 185 return R(); | |
| 186 return DispatchToMethod<R>( *this->obj_, this->method_, pb_, params); | |
| 187 } | |
| 188 | |
| 189 T** obj_; | |
| 190 Method method_; | |
| 191 PreBound pb_; | |
| 192 }; | |
| 193 #endif | |
| 194 | |
| 195 // Simple MutantRunner<> wrapper acting as a functor. | |
| 196 // Redirects operator() to MutantRunner<Params>::Run() | |
| 197 template <typename R, typename Params> | |
| 198 struct MutantFunctor { | |
| 199 explicit MutantFunctor(MutantRunner<R, Params>* cb) : impl_(cb) { | |
| 200 } | |
| 201 | |
| 202 ~MutantFunctor() { | |
| 203 } | |
| 204 | |
| 205 inline R operator()() { | |
| 206 return impl_->RunWithParams(base::Tuple<>()); | |
| 207 } | |
| 208 | |
| 209 template <typename Arg1> | |
| 210 inline R operator()(const Arg1& a) { | |
| 211 return impl_->RunWithParams(Params(a)); | |
| 212 } | |
| 213 | |
| 214 template <typename Arg1, typename Arg2> | |
| 215 inline R operator()(const Arg1& a, const Arg2& b) { | |
| 216 return impl_->RunWithParams(Params(a, b)); | |
| 217 } | |
| 218 | |
| 219 template <typename Arg1, typename Arg2, typename Arg3> | |
| 220 inline R operator()(const Arg1& a, const Arg2& b, const Arg3& c) { | |
| 221 return impl_->RunWithParams(Params(a, b, c)); | |
| 222 } | |
| 223 | |
| 224 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
| 225 inline R operator()(const Arg1& a, const Arg2& b, const Arg3& c, | |
| 226 const Arg4& d) { | |
| 227 return impl_->RunWithParams(Params(a, b, c, d)); | |
| 228 } | |
| 229 | |
| 230 private: | |
| 231 // We need copy constructor since MutantFunctor is copied few times | |
| 232 // inside GMock machinery, hence no DISALLOW_EVIL_CONTRUCTORS | |
| 233 MutantFunctor(); | |
| 234 linked_ptr<MutantRunner<R, Params> > impl_; | |
| 235 }; | |
| 236 """ | |
| 237 | |
| 238 FOOTER = """\ | |
| 239 } // namespace testing | |
| 240 | |
| 241 #endif // TESTING_GMOCK_MUTANT_H_""" | |
| 242 | |
| 243 # Templates for DispatchToMethod/DispatchToFunction functions. | |
| 244 # template_params - typename P1, typename P2.. typename C1.. | |
| 245 # prebound - Tuple<P1, .. PN> | |
| 246 # calltime - Tuple<C1, .. CN> | |
| 247 # args - p.a, p.b.., c.a, c.b.. | |
| 248 DISPATCH_TO_METHOD_TEMPLATE = """\ | |
| 249 template <typename R, typename T, typename Method, %(template_params)s> | |
| 250 inline R DispatchToMethod(T* obj, Method method, | |
| 251 const %(prebound)s& p, | |
| 252 const %(calltime)s& c) { | |
| 253 return (obj->*method)(%(args)s); | |
| 254 } | |
| 255 """ | |
| 256 | |
| 257 DISPATCH_TO_FUNCTION_TEMPLATE = """\ | |
| 258 template <typename R, typename Function, %(template_params)s> | |
| 259 inline R DispatchToFunction(Function function, | |
| 260 const %(prebound)s& p, | |
| 261 const %(calltime)s& c) { | |
| 262 return (*function)(%(args)s); | |
| 263 } | |
| 264 """ | |
| 265 | |
| 266 # Templates for CreateFunctor functions. | |
| 267 # template_params - typename P1, typename P2.. typename C1.. typename X1.. | |
| 268 # prebound - Tuple<P1, .. PN> | |
| 269 # calltime - Tuple<A1, .. AN> | |
| 270 # params - X1,.. , A1, .. | |
| 271 # args - const P1& p1 .. | |
| 272 # call_args - p1, p2, p3.. | |
| 273 CREATE_METHOD_FUNCTOR_TEMPLATE = """\ | |
| 274 template <typename R, typename T, typename U, %(template_params)s> | |
| 275 inline MutantFunctor<R, %(calltime)s> | |
| 276 CreateFunctor(T* obj, R (U::*method)(%(params)s), %(args)s) { | |
| 277 MutantRunner<R, %(calltime)s>* t = | |
| 278 new Mutant<R, T, R (U::*)(%(params)s), | |
| 279 %(prebound)s, %(calltime)s> | |
| 280 (obj, method, base::MakeTuple(%(call_args)s)); | |
| 281 return MutantFunctor<R, %(calltime)s>(t); | |
| 282 } | |
| 283 """ | |
| 284 | |
| 285 CREATE_FUNCTION_FUNCTOR_TEMPLATE = """\ | |
| 286 template <typename R, %(template_params)s> | |
| 287 inline MutantFunctor<R, %(calltime)s> | |
| 288 CreateFunctor(R (*function)(%(params)s), %(args)s) { | |
| 289 MutantRunner<R, %(calltime)s>* t = | |
| 290 new MutantFunction<R, R (*)(%(params)s), | |
| 291 %(prebound)s, %(calltime)s> | |
| 292 (function, base::MakeTuple(%(call_args)s)); | |
| 293 return MutantFunctor<R, %(calltime)s>(t); | |
| 294 } | |
| 295 """ | |
| 296 | |
| 297 def SplitLine(line, width): | |
| 298 """Splits a single line at comma, at most |width| characters long.""" | |
| 299 if len(line) <= width: | |
| 300 return (line, None) | |
| 301 n = 1 + line[:width].rfind(",") | |
| 302 if n == 0: # If comma cannot be found give up and return the entire line. | |
| 303 return (line, None) | |
| 304 # Assume there is a space after the comma | |
| 305 assert line[n] == " " | |
| 306 return (line[:n], line[n + 1:]) | |
| 307 | |
| 308 | |
| 309 def Wrap(s, width, subsequent_offset): | |
| 310 """Wraps a single line |s| at commas so every line is at most |width| | |
| 311 characters long. | |
| 312 """ | |
| 313 w = [] | |
| 314 spaces = " " * subsequent_offset | |
| 315 while s: | |
| 316 (f, s) = SplitLine(s, width) | |
| 317 w.append(f) | |
| 318 if s: | |
| 319 s = spaces + s | |
| 320 return "\n".join(w) | |
| 321 | |
| 322 | |
| 323 def Clean(s): | |
| 324 """Cleans artifacts from generated C++ code. | |
| 325 | |
| 326 Our simple string formatting/concatenation may introduce extra commas. | |
| 327 """ | |
| 328 s = s.replace(", >", ">") | |
| 329 s = s.replace(", )", ")") | |
| 330 return s | |
| 331 | |
| 332 | |
| 333 def ExpandPattern(pattern, it): | |
| 334 """Return list of expanded pattern strings. | |
| 335 | |
| 336 Each string is created by replacing all '%' in |pattern| with element of |it|. | |
| 337 """ | |
| 338 return [pattern.replace("%", x) for x in it] | |
| 339 | |
| 340 | |
| 341 def Gen(pattern, n, start): | |
| 342 """Expands pattern replacing '%' with sequential integers starting with start. | |
| 343 | |
| 344 Expanded patterns will be joined with comma separator. | |
| 345 Gen("X%", 3, 1) will return "X1, X2, X3". | |
| 346 """ | |
| 347 it = string.hexdigits[start:n + start] | |
| 348 return ", ".join(ExpandPattern(pattern, it)) | |
| 349 | |
| 350 | |
| 351 def Merge(a): | |
| 352 return ", ".join(filter(len, a)) | |
| 353 | |
| 354 | |
| 355 def GenTuple(pattern, n): | |
| 356 return Clean("base::Tuple<%s>" % (Gen(pattern, n, 1))) | |
| 357 | |
| 358 | |
| 359 def FixCode(s): | |
| 360 lines = Clean(s).splitlines() | |
| 361 # Wrap sometimes very long 1st line to be inside the "template <" | |
| 362 lines[0] = Wrap(lines[0], 80, 10) | |
| 363 | |
| 364 # Wrap all subsequent lines to 6 spaces arbitrarily. This is a 2-space line | |
| 365 # indent, plus a 4 space continuation indent. | |
| 366 for line in xrange(1, len(lines)): | |
| 367 lines[line] = Wrap(lines[line], 80, 6) | |
| 368 return "\n".join(lines) | |
| 369 | |
| 370 | |
| 371 def GenerateDispatch(prebound, calltime): | |
| 372 print "\n// %d - %d" % (prebound, calltime) | |
| 373 args = { | |
| 374 "template_params": Merge([Gen("typename P%", prebound, 1), | |
| 375 Gen("typename C%", calltime, 1)]), | |
| 376 "prebound": GenTuple("P%", prebound), | |
| 377 "calltime": GenTuple("C%", calltime), | |
| 378 "args": Merge([Gen("base::get<%>(p)", prebound, 0), | |
| 379 Gen("base::get<%>(c)", calltime, 0)]), | |
| 380 } | |
| 381 | |
| 382 print FixCode(DISPATCH_TO_METHOD_TEMPLATE % args) | |
| 383 print FixCode(DISPATCH_TO_FUNCTION_TEMPLATE % args) | |
| 384 | |
| 385 | |
| 386 def GenerateCreateFunctor(prebound, calltime): | |
| 387 print "// %d - %d" % (prebound, calltime) | |
| 388 args = { | |
| 389 "calltime": GenTuple("A%", calltime), | |
| 390 "prebound": GenTuple("P%", prebound), | |
| 391 "params": Merge([Gen("X%", prebound, 1), Gen("A%", calltime, 1)]), | |
| 392 "args": Gen("const P%& p%", prebound, 1), | |
| 393 "call_args": Gen("p%", prebound, 1), | |
| 394 "template_params": Merge([Gen("typename P%", prebound, 1), | |
| 395 Gen("typename A%", calltime, 1), | |
| 396 Gen("typename X%", prebound, 1)]) | |
| 397 } | |
| 398 | |
| 399 mutant = FixCode(CREATE_METHOD_FUNCTOR_TEMPLATE % args) | |
| 400 print mutant | |
| 401 | |
| 402 # Slightly different version for free function call. | |
| 403 print "\n", FixCode(CREATE_FUNCTION_FUNCTOR_TEMPLATE % args) | |
| 404 | |
| 405 # Functor with pointer to a pointer of the object. | |
| 406 print "\n#ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" | |
| 407 mutant2 = mutant.replace("CreateFunctor(T* obj,", "CreateFunctor(T** obj,") | |
| 408 mutant2 = mutant2.replace("new Mutant", "new MutantLateObjectBind") | |
| 409 mutant2 = mutant2.replace(" " * 17 + "Tuple", " " * 31 + "Tuple") | |
| 410 print mutant2 | |
| 411 print "#endif // GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING\n" | |
| 412 | |
| 413 # OS_WIN specific. Same functors but with stdcall calling conventions. | |
| 414 # These are not for WIN64 (x86_64) because there is only one calling | |
| 415 # convention in WIN64. | |
| 416 # Functor for method with __stdcall calling conventions. | |
| 417 print "#if defined (OS_WIN) && !defined (ARCH_CPU_X86_64)" | |
| 418 stdcall_method = CREATE_METHOD_FUNCTOR_TEMPLATE | |
| 419 stdcall_method = stdcall_method.replace("U::", "__stdcall U::") | |
| 420 stdcall_method = FixCode(stdcall_method % args) | |
| 421 print stdcall_method | |
| 422 # Functor for free function with __stdcall calling conventions. | |
| 423 stdcall_function = CREATE_FUNCTION_FUNCTOR_TEMPLATE | |
| 424 stdcall_function = stdcall_function.replace("R (*", "R (__stdcall *") | |
| 425 print "\n", FixCode(stdcall_function % args) | |
| 426 | |
| 427 print "#ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" | |
| 428 stdcall2 = stdcall_method | |
| 429 stdcall2 = stdcall2.replace("CreateFunctor(T* obj,", "CreateFunctor(T** obj,") | |
| 430 stdcall2 = stdcall2.replace("new Mutant", "new MutantLateObjectBind") | |
| 431 stdcall2 = stdcall2.replace(" " * 17 + "Tuple", " " * 31 + "Tuple") | |
| 432 print stdcall2 | |
| 433 print "#endif // GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" | |
| 434 print "#endif // defined (OS_WIN) && !defined (ARCH_CPU_X86_64)\n" | |
| 435 | |
| 436 | |
| 437 def main(): | |
| 438 print HEADER | |
| 439 for prebound in xrange(0, 6 + 1): | |
| 440 for args in xrange(0, 6 + 1): | |
| 441 GenerateDispatch(prebound, args) | |
| 442 print MUTANT | |
| 443 for prebound in xrange(0, 6 + 1): | |
| 444 for args in xrange(0, 6 + 1): | |
| 445 GenerateCreateFunctor(prebound, args) | |
| 446 print FOOTER | |
| 447 return 0 | |
| 448 | |
| 449 | |
| 450 if __name__ == "__main__": | |
| 451 sys.exit(main()) | |
| OLD | NEW |