Google Mock(Gmock)简单使用和源码分析——源码分析
源碼分析
? ? ? ? 通過《Google Mock(Gmock)簡單使用和源碼分析——簡單使用》中的例子,我們發(fā)現(xiàn)被mock的相關(guān)方法在mock類中已經(jīng)被重新實(shí)現(xiàn)了,否則它們也不會按照我們的期待的行為執(zhí)行。我們通過閱讀源碼,來分析整個過程的實(shí)現(xiàn)邏輯。(轉(zhuǎn)載請指明出于breaksoftware的csdn博客)
MOCK_METHOD系列宏
? ? ? ? 首先我們以MOCK_METHOD0為例
#define MOCK_METHOD0(m, ...) GMOCK_METHOD0_(, , , m, __VA_ARGS__)
? ? ? ? 可以看到它實(shí)際上封裝了GMOCK_METHOD0_。我們在介紹GMOCK_METHOD0_之前,還可以看到其他無參數(shù)的宏
#define MOCK_CONST_METHOD0(m, ...) GMOCK_METHOD0_(, const, , m, __VA_ARGS__)
……
#define MOCK_METHOD0_T(m, ...) GMOCK_METHOD0_(typename, , , m, __VA_ARGS__)
……
#define MOCK_CONST_METHOD0_T(m, ...) \GMOCK_METHOD0_(typename, const, , m, __VA_ARGS__)
……
#define MOCK_METHOD0_WITH_CALLTYPE(ct, m, ...) \GMOCK_METHOD0_(, , ct, m, __VA_ARGS__)
……
#define MOCK_CONST_METHOD0_WITH_CALLTYPE(ct, m, ...) \GMOCK_METHOD0_(, const, ct, m, __VA_ARGS__)
……
#define MOCK_METHOD0_T_WITH_CALLTYPE(ct, m, ...) \GMOCK_METHOD0_(typename, , ct, m, __VA_ARGS__)
……
#define MOCK_CONST_METHOD0_T_WITH_CALLTYPE(ct, m, ...) \GMOCK_METHOD0_(typename, const, ct, m, __VA_ARGS__)
……
? ? ? ? 這些無參數(shù)的宏宏都是基于GMOCK_METHOD0_實(shí)現(xiàn)的,它們的差別只是不同參數(shù)的組合。這兒要列出它們是因?yàn)镚MOCK_METHOD0_的定義比較晦澀,通過這些醒目的定義,我們將會發(fā)現(xiàn)其各個參數(shù)的作用。
// INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
#define GMOCK_METHOD0_(tn, constness, ct, Method, ...) \GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \) constness { \GTEST_COMPILE_ASSERT_((::testing::tuple_size< \tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value \== 0), \this_method_does_not_take_0_arguments); \GMOCK_MOCKER_(0, constness, Method).SetOwnerAndName(this, #Method); \return GMOCK_MOCKER_(0, constness, Method).Invoke(); \} \::testing::MockSpec<__VA_ARGS__>& \gmock_##Method() constness { \GMOCK_MOCKER_(0, constness, Method).RegisterOwner(this); \return GMOCK_MOCKER_(0, constness, Method).With(); \} \mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_(0, constness, \Method)
? ? ? ? 上面宏中的tn可以通過之前的調(diào)用發(fā)現(xiàn)其應(yīng)該是typename這類用于定義模板的關(guān)鍵字。constness表示mock的方法是不是const類型的。ct是調(diào)用約定,比如我們在windows程序里經(jīng)常見到的STDMETHODCALLTYPE。Method是被mock的函數(shù)名。不定參數(shù)則是函數(shù)指針類型。這兒比較有意思的是不定參數(shù),因?yàn)樽鳛橐粋€框架,它需要支持各種類型的函數(shù),而我們不可能把所有類型一一進(jìn)行羅列。這個時候我們就可以使用不定參數(shù)來解決這個問題。
? ? ? ? 我們先總覽一下GMOCK_METHOD0_的實(shí)現(xiàn)。上述代碼第17行定義了一個具有mutable屬性的變量,之所以使用mutable是因?yàn)樗赡軙皇褂迷赾onst類型的函數(shù)中,然而該對象的方法并不一定是const的。這個參數(shù)的名稱使用GMOCK_MOCKER_宏組裝
#define GMOCK_MOCKER_(arity, constness, Method) \GTEST_CONCAT_TOKEN_(gmock##constness##arity##_##Method##_, __LINE__)#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar
? ? ? ? 可以見得該參數(shù)名包括了gmock關(guān)鍵字、是否有const屬性、參數(shù)個數(shù)、方法名已經(jīng)所在的行號組成。這樣就盡可能的保證該變量在同一個文件中的唯一性。
? ? ? ? 該變量的類型是一個以函數(shù)類型為模板參數(shù)的對象,其模板類的定義是
template <typename R>
class FunctionMocker<R()> : publicinternal::FunctionMockerBase<R()> {public:typedef R F();typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;MockSpec<F>& With() {return this->current_spec();}R Invoke() {// Even though gcc and MSVC don't enforce it, 'this->' is required// by the C++ standard [14.6.4] here, as the base class type is// dependent on the template argument (and thus shouldn't be// looked into when resolving InvokeWith).return this->InvokeWith(ArgumentTuple());}
};
? ? ? ? 該模板類定義的模板類型就是函數(shù)的返回值類型——R。比如例子中Online方法,它被mock之后,傳導(dǎo)到該類的R就是bool。上面代碼中05行使用返回類型重新定義了函數(shù)類型為F()。06行別名了用于保存函數(shù)參數(shù)的元組類型為ArgumentTuple。08行定義的With函數(shù)是用于對參數(shù)的篩選。于是我們是以無參數(shù)函數(shù)為例,所以該處沒有設(shè)定參數(shù)預(yù)期。12行是我們mock函數(shù)的真實(shí)實(shí)現(xiàn)。這些內(nèi)容我們將在之后詳細(xì)講解,我們再回到GMOCK_METHOD0_的定義上
GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \) constness { \GTEST_COMPILE_ASSERT_((::testing::tuple_size< \tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value \== 0), \this_method_does_not_take_0_arguments); \GMOCK_MOCKER_(0, constness, Method).SetOwnerAndName(this, #Method); \return GMOCK_MOCKER_(0, constness, Method).Invoke(); \} \
? ? ? ??GMOCK_RESULT_宏定義了mock函數(shù)的返回類型
#define GMOCK_RESULT_(tn, ...) \tn ::testing::internal::Function<__VA_ARGS__>::Resulttemplate <typename R>
struct Function<R()> {typedef R Result;typedef ::testing::tuple<> ArgumentTuple;typedef typename MatcherTuple<ArgumentTuple>::type ArgumentMatcherTuple;typedef void MakeResultVoid();typedef IgnoredValue MakeResultIgnoredValue();
};
? ? ? ? 這么定義的一個優(yōu)點(diǎn)就是可以通過模板將函數(shù)類型的定義中的返回類型給拆出來。這和FunctionMocker定義方式是一樣的。
? ? ? ??GTEST_COMPILE_ASSERT_宏用于檢測定義的參數(shù)個數(shù)是否符合規(guī)定。檢測完之后,使用FunctionMocker模板類對象的SetOwnerAndName方法將對象指針和方法名傳遞到底層邏輯中。最后就會調(diào)用FunctionMocker模板類對象的Invoke方法實(shí)現(xiàn)函數(shù)行為邏輯的調(diào)用。
? ? ? ??GMOCK_METHOD0_中還定義了另一個方法
::testing::MockSpec<__VA_ARGS__>& \gmock_##Method() constness { \GMOCK_MOCKER_(0, constness, Method).RegisterOwner(this); \return GMOCK_MOCKER_(0, constness, Method).With(); \} \
? ? ? ? 它使用了gmock和函數(shù)名組合成為一個新的函數(shù)。該函數(shù)內(nèi)部通過FunctionMocker模板類對象的RegisterOwner方法保存了對象指針,最后返回了MockSpec模板對象。MockSpec模板對象在之前我們見過,它是為了實(shí)現(xiàn)參數(shù)篩選而設(shè)計(jì)的。其具體實(shí)現(xiàn)我們在之后會分析。
? ? ? ? 無參數(shù)的版本忽略了很多函數(shù)參數(shù)的問題,但是其讓我們可以清晰的看見實(shí)現(xiàn)的脈絡(luò)。現(xiàn)在我們將以有兩個參數(shù)的版本來講解其實(shí)現(xiàn)。
// INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
#define GMOCK_METHOD2_(tn, constness, ct, Method, ...) \GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \GMOCK_ARG_(tn, 1, __VA_ARGS__) gmock_a1, \GMOCK_ARG_(tn, 2, __VA_ARGS__) gmock_a2) constness { \GTEST_COMPILE_ASSERT_((::testing::tuple_size< \tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value \== 2), \this_method_does_not_take_2_arguments); \GMOCK_MOCKER_(2, constness, Method).SetOwnerAndName(this, #Method); \return GMOCK_MOCKER_(2, constness, Method).Invoke(gmock_a1, gmock_a2); \} \::testing::MockSpec<__VA_ARGS__>& \gmock_##Method(GMOCK_MATCHER_(tn, 1, __VA_ARGS__) gmock_a1, \GMOCK_MATCHER_(tn, 2, __VA_ARGS__) gmock_a2) constness { \GMOCK_MOCKER_(2, constness, Method).RegisterOwner(this); \return GMOCK_MOCKER_(2, constness, Method).With(gmock_a1, gmock_a2); \} \mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_(2, constness, \Method)
? ? ? ? 上例中,我們發(fā)現(xiàn)相關(guān)函數(shù)的定義多了兩個參數(shù)聲明。我們先看和mock函數(shù)同名的函數(shù)的參數(shù)定義,它使用了GMOCK_ARG_宏指定參數(shù)類型
#define GMOCK_ARG_(tn, N, ...) \tn ::testing::internal::Function<__VA_ARGS__>::Argument##N
? ? ? ? Function模板類在之前我們反復(fù)見過,它的一個非常大的作用就是從函數(shù)類型中拆分出函數(shù)返回值類型和各個參數(shù)類型。因?yàn)橹耙詿o參數(shù)函數(shù)為例,所以我們并沒有欣賞到它的妙處。
template <typename R, typename A1>
struct Function<R(A1)>: Function<R()> {typedef A1 Argument1;typedef ::testing::tuple<A1> ArgumentTuple;typedef typename MatcherTuple<ArgumentTuple>::type ArgumentMatcherTuple;typedef void MakeResultVoid(A1);typedef IgnoredValue MakeResultIgnoredValue(A1);
};template <typename R, typename A1, typename A2>
struct Function<R(A1, A2)>: Function<R(A1)> {typedef A2 Argument2;typedef ::testing::tuple<A1, A2> ArgumentTuple;typedef typename MatcherTuple<ArgumentTuple>::type ArgumentMatcherTuple;typedef void MakeResultVoid(A1, A2);typedef IgnoredValue MakeResultIgnoredValue(A1, A2);
};
? ? ? ? Function模板類的兩個參數(shù)的版本繼承于一個參數(shù)的版本,一個參數(shù)版本繼承于無參數(shù)版本。這樣兩個參數(shù)版本中,它從無參數(shù)版本中繼承到了
typedef R Result;
? ? ? ? 從一個參數(shù)版本中繼承到了
typedef A1 Argument1;
? ? ? ? 而自身定義了
typedef A2 Argument2;
? ? ? ? 它還覆蓋了基類中ArgumentTuple、ArgumentMatcherTuple等的定義。
? ? ? ? 我們看到兩個參數(shù)版本的Function類的模板類型是R(A1, A2),這種方式就是函數(shù)類型的定義。而R、A1和A2是Function模板類的模板。以Login方法為例
MOCK_METHOD2(Login, bool(const std::string&, const std::string&));
? ? ? ? 編譯器將推導(dǎo)出R是bool,A1和A2都是const ?std::string&。這樣它便將函數(shù)返回類型和參數(shù)進(jìn)行了拆分。并別名了各個類型,從而方便在之后模板中忽略具體類型。
? ? ? ?相應(yīng)的FunctionMocker也是使用相同的方式實(shí)現(xiàn)了拆分,我們看下兩個參數(shù)版本的實(shí)現(xiàn)
template <typename R, typename A1, typename A2>
class FunctionMocker<R(A1, A2)> : publicinternal::FunctionMockerBase<R(A1, A2)> {public:typedef R F(A1, A2);typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;MockSpec<F>& With(const Matcher<A1>& m1, const Matcher<A2>& m2) {this->current_spec().SetMatchers(::testing::make_tuple(m1, m2));return this->current_spec();}R Invoke(A1 a1, A2 a2) {// Even though gcc and MSVC don't enforce it, 'this->' is required// by the C++ standard [14.6.4] here, as the base class type is// dependent on the template argument (and thus shouldn't be// looked into when resolving InvokeWith).return this->InvokeWith(ArgumentTuple(a1, a2));}
};
? ? ? ? 相比于無參數(shù)版本,它在With函數(shù)中使用了SetMatchers方法實(shí)現(xiàn)了參數(shù)限制,并在Invoke中,使用兩個參數(shù)定義了一個臨時的參數(shù)元組類型ArgumentTuple對象。這樣將參數(shù)放到一個元組對象中,是對InvokeWith方法對不同個數(shù)、不同類型、不同順序的參數(shù)調(diào)用實(shí)現(xiàn)統(tǒng)一化處理。
EXPECT_CALL、ON_CALL宏? ? ? ??
? ? ? ? 在介紹MOCK_METHOD系列宏是,我們發(fā)現(xiàn)其在我們mock的類中定義兩個方法和一個變量:
- GMOCK_RESULT_(tn, __VA_ARGS__) ct Method(……)
- ::testing::MockSpec<__VA_ARGS__>& gmock_##Method(……)
- mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_(0, constness, Method)
? ? ? ? 1中的方法和我們希望mock的方法同名,這將方便使用者調(diào)用它。2中的函數(shù)是使用gmock和函數(shù)名聯(lián)合組裝成的新函數(shù)名,它返回了一個被參數(shù)篩選的函數(shù)對象。EXPECT_CALL和ON_CALL宏中就是調(diào)用了它。
#define GMOCK_ON_CALL_IMPL_(obj, call) \((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \#obj, #call)
#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
? ? ? ? 宏中((obj).gmock_##call)就是調(diào)用了2中的方法,并對返回的對象調(diào)用InternalDefaultActionSetAt或InternalExpectedAt。以下面的調(diào)用為例
EXPECT_CALL(test_user, Pay(_)).WillRepeatedly(testing::Return(true));
? ? ? ? 其最終是這樣的調(diào)用
test_user.gmock_Pay(_).InternalExpectedAt(__FILE__, __LINE__, 'test_user', 'Pay').WillRepeatedly(testing::Return(true));
? ? ? ? 下劃線_是通配符,它的定義如下
const internal::AnythingMatcher _ = {};class AnythingMatcher {public:template <typename T>operator Matcher<T>() const { return A<T>(); }
};template <typename T>
inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }template <typename T>
inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {return Matcher<T>(impl);
}
? ? ? ? 為什么任何函數(shù)的參數(shù)都可以接受AnythingMatcher。我們可以見2中參數(shù)的定義
::testing::MockSpec<__VA_ARGS__>& \gmock_##Method(GMOCK_MATCHER_(tn, 1, __VA_ARGS__) gmock_a1) constness { \GMOCK_MOCKER_(1, constness, Method).RegisterOwner(this); \return GMOCK_MOCKER_(1, constness, Method).With(gmock_a1); \} \// The matcher type for argument N of the given function type.// INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!#define GMOCK_MATCHER_(tn, N, ...) \const ::testing::Matcher<GMOCK_ARG_(tn, N, __VA_ARGS__)>&
? ? ? ? 函數(shù)的參數(shù)類型都是Matcher模板類,而AnythingMatcher定義了Matcher<T>()方法用于返回一個Matcher<T>對象。
參數(shù)過濾 ? ? ? ?
? ? ? ?參數(shù)過濾是Gmock非常有用的一個功能,它讓我們可以通過參數(shù)定義不同的調(diào)用場景。
? ? ? ?Gmock中提供了兩處設(shè)置參數(shù)過濾的地方,舉個例子
EXPECT_CALL(test_user, Pay(Eq(1))).With(_).WillRepeatedly(testing::Return(true));
? ? ? ?Pay中指定參數(shù)不能等于1,With則表示對參數(shù)沒有限制。這就是兩處參數(shù)約束。一般來說gmock##Method中的參數(shù)約束是針對各自參數(shù)的,而With則是關(guān)注于參數(shù)之間的關(guān)系。我們看下這兩處約束是怎么工作的。
? ? ? ? 以一個參數(shù)的版本為例,MOCK_METHOD1宏中
::testing::MockSpec<__VA_ARGS__>& \gmock_##Method(GMOCK_MATCHER_(tn, 1, __VA_ARGS__) gmock_a1) constness { \GMOCK_MOCKER_(1, constness, Method).RegisterOwner(this); \return GMOCK_MOCKER_(1, constness, Method).With(gmock_a1); \} \mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_(1, constness, \Method)
? ? ? ? gmock##Method方法調(diào)用了FunctionMocker模板類的With方法,該方法返回了一個MockSpec模板對象。gmock##Method方法是在EXPECT_CALL宏中被調(diào)用的。
? ? ? ??FunctionMocker中的With是這么實(shí)現(xiàn)的
MockSpec<F>& With(const Matcher<A1>& m1) {this->current_spec().SetMatchers(::testing::make_tuple(m1));return this->current_spec();}
? ? ? ??current_spec()是其基類FunctionMockerBase的方法,它返回了以FunctionMocker<R(A1)> 為模板的MockSpec對象。SetMatchers方法將參數(shù)的匹配規(guī)則設(shè)置到其底層的matchers_中
void SetMatchers(const ArgumentMatcherTuple& matchers) {matchers_ = matchers;}
? ? ? ??matchers_這個匹配規(guī)則會在調(diào)用EXPECT_CALL時通過下列兩個方法保存起來
// Adds a new default action spec to the function mocker and returns// the newly created spec.internal::OnCallSpec<F>& InternalDefaultActionSetAt(const char* file, int line, const char* obj, const char* call) {LogWithLocation(internal::kInfo, file, line,string("ON_CALL(") + obj + ", " + call + ") invoked");return function_mocker_->AddNewOnCallSpec(file, line, matchers_);}// Adds a new expectation spec to the function mocker and returns// the newly created spec.internal::TypedExpectation<F>& InternalExpectedAt(const char* file, int line, const char* obj, const char* call) {const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")");LogWithLocation(internal::kInfo, file, line, source_text + " invoked");return function_mocker_->AddNewExpectation(file, line, source_text, matchers_);}
? ? ? ? 具體的保存邏輯是
// Adds and returns a default action spec for this mock function.OnCallSpec<F>& AddNewOnCallSpec(const char* file, int line,const ArgumentMatcherTuple& m)GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m);untyped_on_call_specs_.push_back(on_call_spec);return *on_call_spec;}// Adds and returns an expectation spec for this mock function.TypedExpectation<F>& AddNewExpectation(const char* file,int line,const string& source_text,const ArgumentMatcherTuple& m)GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line);TypedExpectation<F>* const expectation =new TypedExpectation<F>(this, file, line, source_text, m);const linked_ptr<ExpectationBase> untyped_expectation(expectation);untyped_expectations_.push_back(untyped_expectation);// Adds this expectation into the implicit sequence if there is one.Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();if (implicit_sequence != NULL) {implicit_sequence->AddExpectation(Expectation(untyped_expectation));}return *expectation;}
? ? ? ? 即以該規(guī)則為參數(shù),新建了OnCallSpec<F>或TypedExpectation<F>對象,這兩個對象將會被保存到各自的vector中。當(dāng)mock的函數(shù)被調(diào)用時,Gmock將通過下面兩個函數(shù)之一去檢測參數(shù)是否匹配
// Returns the ON_CALL spec that matches this mock function with the// given arguments; returns NULL if no matching ON_CALL is found.// L = *const OnCallSpec<F>* FindOnCallSpec(const ArgumentTuple& args) const {for (UntypedOnCallSpecs::const_reverse_iterator it= untyped_on_call_specs_.rbegin();it != untyped_on_call_specs_.rend(); ++it) {const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it);if (spec->Matches(args))return spec;}return NULL;}
// Returns the expectation that matches the arguments, or NULL if no// expectation matches them.TypedExpectation<F>* FindMatchingExpectationLocked(const ArgumentTuple& args) constGTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();for (typename UntypedExpectations::const_reverse_iterator it =untyped_expectations_.rbegin();it != untyped_expectations_.rend(); ++it) {TypedExpectation<F>* const exp =static_cast<TypedExpectation<F>*>(it->get());if (exp->ShouldHandleArguments(args)) {return exp;}}return NULL;}
? ? ? ? 這兩個函數(shù)最終將在Matches函數(shù)中進(jìn)行參數(shù)匹配
// Returns true iff this expectation matches the given arguments.bool Matches(const ArgumentTuple& args) constGTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();return TupleMatches(matchers_, args) && extra_matcher_.Matches(args);}
? ? ? ? 這個函數(shù)中,還有extra_matcher_這種參數(shù)匹配規(guī)則。它是通過TypedExpectation模板類的With方法(不是FunctionMocker模板類的With方法)傳遞進(jìn)來的
// Implements the .With() clause.TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) {if (last_clause_ == kWith) {ExpectSpecProperty(false,".With() cannot appear ""more than once in an EXPECT_CALL().");} else {ExpectSpecProperty(last_clause_ < kWith,".With() must be the first ""clause in an EXPECT_CALL().");}last_clause_ = kWith;extra_matcher_ = m;extra_matcher_specified_ = true;return *this;}
? ? ? ? 總結(jié)一下,Gmock的參數(shù)匹配通過FunctionMocker的With方法設(shè)置了一個通用匹配規(guī)則,還可以通過TypedExpectation的With方法設(shè)置額外的匹配規(guī)則,只有這兩個匹配規(guī)則都滿足時,才會被選中。
設(shè)定約束
? ? ? ? 我們主要分析下Times、WillOnce和WillRepeatedly這幾個常見的約束。先回顧一個例子
EXPECT_CALL(test_user, Pay(_)).Times(5).WillOnce(testing::Return(true)).WillOnce(testing::Return(true)).WillRepeatedly(testing::Return(false));
? ? ? ? 這例子說,Pay行為有5次可控的執(zhí)行次數(shù),第6次執(zhí)行就按默認(rèn)值返回了。第1個WillOnce規(guī)定第一次執(zhí)行Pay的行為,第2個WillOnce規(guī)定第二次執(zhí)行Pay的行為,之后的3~5次都按WillRepeatedly規(guī)定的方式去執(zhí)行。
? ? ? ? 我們先看Times的實(shí)現(xiàn)
// Implements the .Times() clause.TypedExpectation& Times(const Cardinality& a_cardinality) {ExpectationBase::UntypedTimes(a_cardinality);return *this;}// Implements the .Times() clause.TypedExpectation& Times(int n) {return Times(Exactly(n));}
// Implements the .Times() clause.
void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) {if (last_clause_ == kTimes) {ExpectSpecProperty(false,".Times() cannot appear ""more than once in an EXPECT_CALL().");} else {ExpectSpecProperty(last_clause_ < kTimes,".Times() cannot appear after "".InSequence(), .WillOnce(), .WillRepeatedly(), ""or .RetiresOnSaturation().");}last_clause_ = kTimes;SpecifyCardinality(a_cardinality);
}// Explicitly specifies the cardinality of this expectation. Used by
// the subclasses to implement the .Times() clause.
void ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) {cardinality_specified_ = true;cardinality_ = a_cardinality;
}
? ? ? ? 執(zhí)行次數(shù)最終被轉(zhuǎn)換為Cardinality類的一個對象保存在FunctionMocker模板對象中。它將在IsSatisfied、IsSaturated和IsOverSaturated方法中被使用,用以判定執(zhí)行的次數(shù)是否符合約定
// Returns true iff this expectation is satisfied.bool IsSatisfied() constGTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();return cardinality().IsSatisfiedByCallCount(call_count_);}// Returns true iff this expectation is saturated.bool IsSaturated() constGTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();return cardinality().IsSaturatedByCallCount(call_count_);}// Returns true iff this expectation is over-saturated.bool IsOverSaturated() constGTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();return cardinality().IsOverSaturatedByCallCount(call_count_);}
? ? ? ? 參數(shù)中的call_count_就是函數(shù)執(zhí)行的次數(shù),它是在IncrementCallCount函數(shù)中實(shí)現(xiàn)自增。IncrementCallCount函數(shù)則是在獲取行為時被調(diào)用到
const Action<F>* GetActionForArguments(const FunctionMockerBase<F>* mocker,const ArgumentTuple& args,::std::ostream* what,::std::ostream* why)GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {g_gmock_mutex.AssertHeld();if (IsSaturated()) {
......IncrementCallCount();
......}IncrementCallCount();
......}
? ? ? ? 我們再看下WillOnce的實(shí)現(xiàn)
// Implements the .WillOnce() clause.TypedExpectation& WillOnce(const Action<F>& action) {ExpectSpecProperty(last_clause_ <= kWillOnce,".WillOnce() cannot appear after "".WillRepeatedly() or .RetiresOnSaturation().");last_clause_ = kWillOnce;untyped_actions_.push_back(new Action<F>(action));if (!cardinality_specified()) {set_cardinality(Exactly(static_cast<int>(untyped_actions_.size())));}return *this;}
? ? ? ? 可見WillOnce將入?yún)⒅匦沦x值給一個新建的Action<F>對象。然后將它保存到untyped_actions_列表中。最終它會在GetCurrentAction方法中,通過參數(shù)匹配后被取出
return count <= action_count ?*static_cast<const Action<F>*>(untyped_actions_[count - 1]) :repeated_action();
? ? ? ? 上面代碼中的repeated_action方法是在WillRepeatedly方法這被賦值的
// Implements the .WillRepeatedly() clause.TypedExpectation& WillRepeatedly(const Action<F>& action) {if (last_clause_ == kWillRepeatedly) {ExpectSpecProperty(false,".WillRepeatedly() cannot appear ""more than once in an EXPECT_CALL().");} else {ExpectSpecProperty(last_clause_ < kWillRepeatedly,".WillRepeatedly() cannot appear ""after .RetiresOnSaturation().");}last_clause_ = kWillRepeatedly;repeated_action_specified_ = true;repeated_action_ = action;if (!cardinality_specified()) {set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size())));}// Now that no more action clauses can be specified, we check// whether their count makes sense.CheckActionCountIfNotDone();return *this;}
? ? ? ? 再看下testing::Return(true)是怎么轉(zhuǎn)換為Action<F>的。它的定義是
template <typename R>
internal::ReturnAction<R> Return(R value) {return internal::ReturnAction<R>(internal::move(value));
}
? ? ? ? 其中ReturnAction是個模板類,它重載了Action<F>()方法,將返回值轉(zhuǎn)換為一個Action<F>對象
template <typename F>operator Action<F>() const {typedef typename Function<F>::Result Result;GTEST_COMPILE_ASSERT_(!is_reference<Result>::value,use_ReturnRef_instead_of_Return_to_return_a_reference);return Action<F>(new Impl<R, F>(value_));}
? ? ? ? 在new一個Action<F>是,傳入了一個Impl模板類對象,這個模板類有一個Perform方法,其實(shí)現(xiàn)就是返回期待的值
virtual Result Perform(const ArgumentTuple&) { return value_; }
? ? ? ? 那么Action<F>對象和這個Impl模板類是怎么聯(lián)系的呢?我們看下Impl的定義
template <typename R_, typename F>class Impl : public ActionInterface<F> {public:
……
? ? ? ? 而在Action模板類的內(nèi)部有
template <typename F>
class Action {
……
Action(const Action& action) : impl_(action.impl_) {}
……Result Perform(const ArgumentTuple& args) const {
…… return impl_->Perform(args);}private:
……internal::linked_ptr<ActionInterface<F> > impl_;
};
? ? ? ? 很醒目,最終執(zhí)行的行為將由Action類中的Impl_成員變量來執(zhí)行,而該Impl_變量就是在Action被創(chuàng)建時傳入的。
執(zhí)行
? ? ? ? 當(dāng)我們調(diào)用mock的類的mock的函數(shù)時,將會調(diào)用到MOCK_METHOD系列宏中定義的函數(shù)。以一個參數(shù)版本為例
GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \GMOCK_ARG_(tn, 1, __VA_ARGS__) gmock_a1) constness { \GTEST_COMPILE_ASSERT_((::testing::tuple_size< \tn ::testing::internal::Function<__VA_ARGS__>::ArgumentTuple>::value \== 1), \this_method_does_not_take_1_argument); \GMOCK_MOCKER_(1, constness, Method).SetOwnerAndName(this, #Method); \return GMOCK_MOCKER_(1, constness, Method).Invoke(gmock_a1); \} \
? ? ? ? 其最終調(diào)用到FunctionMocker類的Invoke函數(shù)中
R Invoke(A1 a1) {// Even though gcc and MSVC don't enforce it, 'this->' is required// by the C++ standard [14.6.4] here, as the base class type is// dependent on the template argument (and thus shouldn't be// looked into when resolving InvokeWith).return this->InvokeWith(ArgumentTuple(a1));}
? ? ? ? 調(diào)用InvokeWith之前,已將參數(shù)轉(zhuǎn)換成一個ArgumentTuple對象,這樣將方便之后統(tǒng)一處理。? ? ? ??InvokeWith函數(shù)內(nèi)部使用了一個結(jié)果承載器——ResultHolder用于保存結(jié)果。InvokeWith最終會調(diào)用到FunctionMockerBase的PerformDefaultAction中
Result PerformDefaultAction(const ArgumentTuple& args,const string& call_description) const {const OnCallSpec<F>* const spec =this->FindOnCallSpec(args);if (spec != NULL) {return spec->GetAction().Perform(args);}const string message = call_description +"\n The mock function has no default action ""set, and its return type has no default value set.";
#if GTEST_HAS_EXCEPTIONSif (!DefaultValue<Result>::Exists()) {throw std::runtime_error(message);}
#elseAssert(DefaultValue<Result>::Exists(), "", -1, message);
#endifreturn DefaultValue<Result>::Get();}
? ? ? ? 第3行通過參數(shù)匹配相應(yīng)的處理行為。找到行為后,在06行執(zhí)行該行為;沒有找到,則返回默認(rèn)值。
? ? ? ? 至此,Gmock的主要流程相關(guān)的源碼已經(jīng)分析結(jié)束了。我們稍微總結(jié)下:
- Mock的類通過MOCK_METHOD系列方法,聲明了一個Mock函數(shù)的對象,并定義了一個通過該對象獲取符合相應(yīng)約束的函數(shù)對象。還定義了一個和需要mock的函數(shù)同名的函數(shù),該函數(shù)內(nèi)部完成最終的結(jié)果計(jì)算。
- EXPECT_CALL宏和WillOnce、WillRepeatedly等方法,設(shè)定了函數(shù)對象的一些特性。
- 最終用戶調(diào)用函數(shù)時,將通過參數(shù)匹配得到適合的函數(shù)對象,并執(zhí)行該函數(shù)對象中的預(yù)期行為。
總結(jié)
以上是生活随笔為你收集整理的Google Mock(Gmock)简单使用和源码分析——源码分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Google Mock(Gmock)简单
- 下一篇: 朴素、Select、Poll和Epoll