michael@0: #!/usr/bin/env python michael@0: # Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: import string michael@0: import sys michael@0: michael@0: HEADER = """\ michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // This file automatically generated by testing/generate_gmock_mutant.py. michael@0: // DO NOT EDIT. michael@0: michael@0: #ifndef TESTING_GMOCK_MUTANT_H_ michael@0: #define TESTING_GMOCK_MUTANT_H_ michael@0: michael@0: // The intention of this file is to make possible using GMock actions in michael@0: // all of its syntactic beauty. Classes and helper functions can be used as michael@0: // more generic variants of Task and Callback classes (see base/task.h) michael@0: // Mutant supports both pre-bound arguments (like Task) and call-time michael@0: // arguments (like Callback) - hence the name. :-) michael@0: // michael@0: // DispatchToMethod/Function supports two sets of arguments: pre-bound (P) and michael@0: // call-time (C). The arguments as well as the return type are templatized. michael@0: // DispatchToMethod/Function will also try to call the selected method or michael@0: // function even if provided pre-bound arguments does not match exactly with michael@0: // the function signature hence the X1, X2 ... XN parameters in CreateFunctor. michael@0: // DispatchToMethod will try to invoke method that may not belong to the michael@0: // object's class itself but to the object's class base class. michael@0: // michael@0: // Additionally you can bind the object at calltime by binding a pointer to michael@0: // pointer to the object at creation time - before including this file you michael@0: // have to #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING. michael@0: // michael@0: // TODO(stoyan): It's yet not clear to me should we use T& and T&* instead michael@0: // of T* and T** when we invoke CreateFunctor to match the EXPECT_CALL style. michael@0: // michael@0: // michael@0: // Sample usage with gMock: michael@0: // michael@0: // struct Mock : public ObjectDelegate { michael@0: // MOCK_METHOD2(string, OnRequest(int n, const string& request)); michael@0: // MOCK_METHOD1(void, OnQuit(int exit_code)); michael@0: // MOCK_METHOD2(void, LogMessage(int level, const string& message)); michael@0: // michael@0: // string HandleFlowers(const string& reply, int n, const string& request) { michael@0: // string result = SStringPrintf("In request of %d %s ", n, request); michael@0: // for (int i = 0; i < n; ++i) result.append(reply) michael@0: // return result; michael@0: // } michael@0: // michael@0: // void DoLogMessage(int level, const string& message) { michael@0: // } michael@0: // michael@0: // void QuitMessageLoop(int seconds) { michael@0: // MessageLoop* loop = MessageLoop::current(); michael@0: // loop->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), michael@0: // 1000 * seconds); michael@0: // } michael@0: // }; michael@0: // michael@0: // Mock mock; michael@0: // // Will invoke mock.HandleFlowers("orchids", n, request) michael@0: // // "orchids" is a pre-bound argument, and and are call-time michael@0: // // arguments - they are not known until the OnRequest mock is invoked. michael@0: // EXPECT_CALL(mock, OnRequest(Ge(5), StartsWith("flower")) michael@0: // .Times(1) michael@0: // .WillOnce(Invoke(CreateFunctor(&mock, &Mock::HandleFlowers, michael@0: // string("orchids")))); michael@0: // michael@0: // michael@0: // // No pre-bound arguments, two call-time arguments passed michael@0: // // directly to DoLogMessage michael@0: // EXPECT_CALL(mock, OnLogMessage(_, _)) michael@0: // .Times(AnyNumber()) michael@0: // .WillAlways(Invoke(CreateFunctor, &mock, &Mock::DoLogMessage)); michael@0: // michael@0: // michael@0: // // In this case we have a single pre-bound argument - 3. We ignore michael@0: // // all of the arguments of OnQuit. michael@0: // EXCEPT_CALL(mock, OnQuit(_)) michael@0: // .Times(1) michael@0: // .WillOnce(InvokeWithoutArgs(CreateFunctor( michael@0: // &mock, &Mock::QuitMessageLoop, 3))); michael@0: // michael@0: // MessageLoop loop; michael@0: // loop.Run(); michael@0: // michael@0: // michael@0: // // Here is another example of how we can set an action that invokes michael@0: // // method of an object that is not yet created. michael@0: // struct Mock : public ObjectDelegate { michael@0: // MOCK_METHOD1(void, DemiurgeCreated(Demiurge*)); michael@0: // MOCK_METHOD2(void, OnRequest(int count, const string&)); michael@0: // michael@0: // void StoreDemiurge(Demiurge* w) { michael@0: // demiurge_ = w; michael@0: // } michael@0: // michael@0: // Demiurge* demiurge; michael@0: // } michael@0: // michael@0: // EXPECT_CALL(mock, DemiurgeCreated(_)).Times(1) michael@0: // .WillOnce(Invoke(CreateFunctor(&mock, &Mock::StoreDemiurge))); michael@0: // michael@0: // EXPECT_CALL(mock, OnRequest(_, StrEq("Moby Dick"))) michael@0: // .Times(AnyNumber()) michael@0: // .WillAlways(WithArgs<0>(Invoke( michael@0: // CreateFunctor(&mock->demiurge_, &Demiurge::DecreaseMonsters)))); michael@0: // michael@0: michael@0: #include "base/memory/linked_ptr.h" michael@0: #include "base/tuple.h" // for Tuple michael@0: michael@0: namespace testing {""" michael@0: michael@0: MUTANT = """\ michael@0: michael@0: // Interface that is exposed to the consumer, that does the actual calling michael@0: // of the method. michael@0: template michael@0: class MutantRunner { michael@0: public: michael@0: virtual R RunWithParams(const Params& params) = 0; michael@0: virtual ~MutantRunner() {} michael@0: }; michael@0: michael@0: // Mutant holds pre-bound arguments (like Task). Like Callback michael@0: // allows call-time arguments. You bind a pointer to the object michael@0: // at creation time. michael@0: template michael@0: class Mutant : public MutantRunner { michael@0: public: michael@0: Mutant(T* obj, Method method, const PreBound& pb) michael@0: : obj_(obj), method_(method), pb_(pb) { michael@0: } michael@0: michael@0: // MutantRunner implementation michael@0: virtual R RunWithParams(const Params& params) { michael@0: return DispatchToMethod(this->obj_, this->method_, pb_, params); michael@0: } michael@0: michael@0: T* obj_; michael@0: Method method_; michael@0: PreBound pb_; michael@0: }; michael@0: michael@0: template michael@0: class MutantFunction : public MutantRunner { michael@0: public: michael@0: MutantFunction(Function function, const PreBound& pb) michael@0: : function_(function), pb_(pb) { michael@0: } michael@0: michael@0: // MutantRunner implementation michael@0: virtual R RunWithParams(const Params& params) { michael@0: return DispatchToFunction(function_, pb_, params); michael@0: } michael@0: michael@0: Function function_; michael@0: PreBound pb_; michael@0: }; michael@0: michael@0: #ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING michael@0: // MutantLateBind is like Mutant, but you bind a pointer to a pointer michael@0: // to the object. This way you can create actions for an object michael@0: // that is not yet created (has only storage for a pointer to it). michael@0: template michael@0: class MutantLateObjectBind : public MutantRunner { michael@0: public: michael@0: MutantLateObjectBind(T** obj, Method method, const PreBound& pb) michael@0: : obj_(obj), method_(method), pb_(pb) { michael@0: } michael@0: michael@0: // MutantRunner implementation. michael@0: virtual R RunWithParams(const Params& params) { michael@0: EXPECT_THAT(*this->obj_, testing::NotNull()); michael@0: if (NULL == *this->obj_) michael@0: return R(); michael@0: return DispatchToMethod( *this->obj_, this->method_, pb_, params); michael@0: } michael@0: michael@0: T** obj_; michael@0: Method method_; michael@0: PreBound pb_; michael@0: }; michael@0: #endif michael@0: michael@0: // Simple MutantRunner<> wrapper acting as a functor. michael@0: // Redirects operator() to MutantRunner::Run() michael@0: template michael@0: struct MutantFunctor { michael@0: explicit MutantFunctor(MutantRunner* cb) : impl_(cb) { michael@0: } michael@0: michael@0: ~MutantFunctor() { michael@0: } michael@0: michael@0: inline R operator()() { michael@0: return impl_->RunWithParams(Tuple0()); michael@0: } michael@0: michael@0: template michael@0: inline R operator()(const Arg1& a) { michael@0: return impl_->RunWithParams(Params(a)); michael@0: } michael@0: michael@0: template michael@0: inline R operator()(const Arg1& a, const Arg2& b) { michael@0: return impl_->RunWithParams(Params(a, b)); michael@0: } michael@0: michael@0: template michael@0: inline R operator()(const Arg1& a, const Arg2& b, const Arg3& c) { michael@0: return impl_->RunWithParams(Params(a, b, c)); michael@0: } michael@0: michael@0: template michael@0: inline R operator()(const Arg1& a, const Arg2& b, const Arg3& c, michael@0: const Arg4& d) { michael@0: return impl_->RunWithParams(Params(a, b, c, d)); michael@0: } michael@0: michael@0: private: michael@0: // We need copy constructor since MutantFunctor is copied few times michael@0: // inside GMock machinery, hence no DISALLOW_EVIL_CONTRUCTORS michael@0: MutantFunctor(); michael@0: linked_ptr > impl_; michael@0: }; michael@0: """ michael@0: michael@0: FOOTER = """\ michael@0: } // namespace testing michael@0: michael@0: #endif // TESTING_GMOCK_MUTANT_H_""" michael@0: michael@0: # Templates for DispatchToMethod/DispatchToFunction functions. michael@0: # template_params - typename P1, typename P2.. typename C1.. michael@0: # prebound - TupleN michael@0: # calltime - TupleN michael@0: # args - p.a, p.b.., c.a, c.b.. michael@0: DISPATCH_TO_METHOD_TEMPLATE = """\ michael@0: template michael@0: inline R DispatchToMethod(T* obj, Method method, michael@0: const %(prebound)s& p, michael@0: const %(calltime)s& c) { michael@0: return (obj->*method)(%(args)s); michael@0: } michael@0: """ michael@0: michael@0: DISPATCH_TO_FUNCTION_TEMPLATE = """\ michael@0: template michael@0: inline R DispatchToFunction(Function function, michael@0: const %(prebound)s& p, michael@0: const %(calltime)s& c) { michael@0: return (*function)(%(args)s); michael@0: } michael@0: """ michael@0: michael@0: # Templates for CreateFunctor functions. michael@0: # template_params - typename P1, typename P2.. typename C1.. typename X1.. michael@0: # prebound - TupleN michael@0: # calltime - TupleN michael@0: # params - X1,.. , A1, .. michael@0: # args - const P1& p1 .. michael@0: # call_args - p1, p2, p3.. michael@0: CREATE_METHOD_FUNCTOR_TEMPLATE = """\ michael@0: template michael@0: inline MutantFunctor michael@0: CreateFunctor(T* obj, R (U::*method)(%(params)s), %(args)s) { michael@0: MutantRunner* t = michael@0: new Mutant michael@0: (obj, method, MakeTuple(%(call_args)s)); michael@0: return MutantFunctor(t); michael@0: } michael@0: """ michael@0: michael@0: CREATE_FUNCTION_FUNCTOR_TEMPLATE = """\ michael@0: template michael@0: inline MutantFunctor michael@0: CreateFunctor(R (*function)(%(params)s), %(args)s) { michael@0: MutantRunner* t = michael@0: new MutantFunction michael@0: (function, MakeTuple(%(call_args)s)); michael@0: return MutantFunctor(t); michael@0: } michael@0: """ michael@0: michael@0: def SplitLine(line, width): michael@0: """Splits a single line at comma, at most |width| characters long.""" michael@0: if len(line) < width: michael@0: return (line, None) michael@0: n = 1 + line[:width].rfind(",") michael@0: if n == 0: # If comma cannot be found give up and return the entire line. michael@0: return (line, None) michael@0: # Assume there is a space after the comma michael@0: assert line[n] == " " michael@0: return (line[:n], line[n + 1:]) michael@0: michael@0: michael@0: def Wrap(s, width, subsequent_offset=4): michael@0: """Wraps a single line |s| at commas so every line is at most |width| michael@0: characters long. michael@0: """ michael@0: w = [] michael@0: spaces = " " * subsequent_offset michael@0: while s: michael@0: (f, s) = SplitLine(s, width) michael@0: w.append(f) michael@0: if s: michael@0: s = spaces + s michael@0: return "\n".join(w) michael@0: michael@0: michael@0: def Clean(s): michael@0: """Cleans artifacts from generated C++ code. michael@0: michael@0: Our simple string formatting/concatenation may introduce extra commas. michael@0: """ michael@0: s = s.replace("<>", "") michael@0: s = s.replace(", >", ">") michael@0: s = s.replace(", )", ")") michael@0: s = s.replace(">>", "> >") michael@0: return s michael@0: michael@0: michael@0: def ExpandPattern(pattern, it): michael@0: """Return list of expanded pattern strings. michael@0: michael@0: Each string is created by replacing all '%' in |pattern| with element of |it|. michael@0: """ michael@0: return [pattern.replace("%", x) for x in it] michael@0: michael@0: michael@0: def Gen(pattern, n): michael@0: """Expands pattern replacing '%' with sequential integers. michael@0: michael@0: Expanded patterns will be joined with comma separator. michael@0: GenAlphs("X%", 3) will return "X1, X2, X3". michael@0: """ michael@0: it = string.hexdigits[1:n + 1] michael@0: return ", ".join(ExpandPattern(pattern, it)) michael@0: michael@0: michael@0: def GenAlpha(pattern, n): michael@0: """Expands pattern replacing '%' with sequential small ASCII letters. michael@0: michael@0: Expanded patterns will be joined with comma separator. michael@0: GenAlphs("X%", 3) will return "Xa, Xb, Xc". michael@0: """ michael@0: it = string.ascii_lowercase[0:n] michael@0: return ", ".join(ExpandPattern(pattern, it)) michael@0: michael@0: michael@0: def Merge(a): michael@0: return ", ".join(filter(len, a)) michael@0: michael@0: michael@0: def GenTuple(pattern, n): michael@0: return Clean("Tuple%d<%s>" % (n, Gen(pattern, n))) michael@0: michael@0: michael@0: def FixCode(s): michael@0: lines = Clean(s).splitlines() michael@0: # Wrap sometimes very long 1st and 3rd line at 80th column. michael@0: lines[0] = Wrap(lines[0], 80, 10) michael@0: lines[2] = Wrap(lines[2], 80, 4) michael@0: return "\n".join(lines) michael@0: michael@0: michael@0: def GenerateDispatch(prebound, calltime): michael@0: print "\n// %d - %d" % (prebound, calltime) michael@0: args = { michael@0: "template_params": Merge([Gen("typename P%", prebound), michael@0: Gen("typename C%", calltime)]), michael@0: "prebound": GenTuple("P%", prebound), michael@0: "calltime": GenTuple("C%", calltime), michael@0: "args": Merge([GenAlpha("p.%", prebound), GenAlpha("c.%", calltime)]), michael@0: } michael@0: michael@0: print FixCode(DISPATCH_TO_METHOD_TEMPLATE % args) michael@0: print FixCode(DISPATCH_TO_FUNCTION_TEMPLATE % args) michael@0: michael@0: michael@0: def GenerateCreateFunctor(prebound, calltime): michael@0: print "// %d - %d" % (prebound, calltime) michael@0: args = { michael@0: "calltime": GenTuple("A%", calltime), michael@0: "prebound": GenTuple("P%", prebound), michael@0: "params": Merge([Gen("X%", prebound), Gen("A%", calltime)]), michael@0: "args": Gen("const P%& p%", prebound), michael@0: "call_args": Gen("p%", prebound), michael@0: "template_params": Merge([Gen("typename P%", prebound), michael@0: Gen("typename A%", calltime), michael@0: Gen("typename X%", prebound)]) michael@0: } michael@0: michael@0: mutant = FixCode(CREATE_METHOD_FUNCTOR_TEMPLATE % args) michael@0: print mutant michael@0: michael@0: # Slightly different version for free function call. michael@0: print "\n", FixCode(CREATE_FUNCTION_FUNCTOR_TEMPLATE % args) michael@0: michael@0: # Functor with pointer to a pointer of the object. michael@0: print "\n#ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" michael@0: mutant2 = mutant.replace("CreateFunctor(T* obj,", "CreateFunctor(T** obj,") michael@0: mutant2 = mutant2.replace("new Mutant", "new MutantLateObjectBind") michael@0: mutant2 = mutant2.replace(" " * 17 + "Tuple", " " * 31 + "Tuple") michael@0: print mutant2 michael@0: print "#endif // GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING\n" michael@0: michael@0: # OS_WIN specific. Same functors but with stdcall calling conventions. michael@0: # Functor for method with __stdcall calling conventions. michael@0: print "#if defined (OS_WIN)" michael@0: stdcall_method = CREATE_METHOD_FUNCTOR_TEMPLATE michael@0: stdcall_method = stdcall_method.replace("U::", "__stdcall U::") michael@0: stdcall_method = FixCode(stdcall_method % args) michael@0: print stdcall_method michael@0: # Functor for free function with __stdcall calling conventions. michael@0: stdcall_function = CREATE_FUNCTION_FUNCTOR_TEMPLATE michael@0: stdcall_function = stdcall_function.replace("R (*", "R (__stdcall *"); michael@0: print "\n", FixCode(stdcall_function % args) michael@0: michael@0: print "#ifdef GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" michael@0: stdcall2 = stdcall_method; michael@0: stdcall2 = stdcall2.replace("CreateFunctor(T* obj,", "CreateFunctor(T** obj,") michael@0: stdcall2 = stdcall2.replace("new Mutant", "new MutantLateObjectBind") michael@0: stdcall2 = stdcall2.replace(" " * 17 + "Tuple", " " * 31 + "Tuple") michael@0: print stdcall2 michael@0: print "#endif // GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING" michael@0: print "#endif // OS_WIN\n" michael@0: michael@0: michael@0: def main(): michael@0: print HEADER michael@0: for prebound in xrange(0, 6 + 1): michael@0: for args in xrange(0, 6 + 1): michael@0: GenerateDispatch(prebound, args) michael@0: print MUTANT michael@0: for prebound in xrange(0, 6 + 1): michael@0: for args in xrange(0, 6 + 1): michael@0: GenerateCreateFunctor(prebound, args) michael@0: print FOOTER michael@0: return 0 michael@0: michael@0: michael@0: if __name__ == "__main__": michael@0: sys.exit(main())