/proc/self/cwd/external/com_google_googletest/googlemock/src/gmock-spec-builders.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2007, Google Inc. |
2 | | // All rights reserved. |
3 | | // |
4 | | // Redistribution and use in source and binary forms, with or without |
5 | | // modification, are permitted provided that the following conditions are |
6 | | // met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright |
9 | | // notice, this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above |
11 | | // copyright notice, this list of conditions and the following disclaimer |
12 | | // in the documentation and/or other materials provided with the |
13 | | // distribution. |
14 | | // * Neither the name of Google Inc. nor the names of its |
15 | | // contributors may be used to endorse or promote products derived from |
16 | | // this software without specific prior written permission. |
17 | | // |
18 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | |
30 | | |
31 | | // Google Mock - a framework for writing C++ mock classes. |
32 | | // |
33 | | // This file implements the spec builder syntax (ON_CALL and |
34 | | // EXPECT_CALL). |
35 | | |
36 | | #include "gmock/gmock-spec-builders.h" |
37 | | |
38 | | #include <stdlib.h> |
39 | | |
40 | | #include <iostream> // NOLINT |
41 | | #include <map> |
42 | | #include <memory> |
43 | | #include <set> |
44 | | #include <string> |
45 | | #include <vector> |
46 | | |
47 | | #include "gmock/gmock.h" |
48 | | #include "gtest/gtest.h" |
49 | | #include "gtest/internal/gtest-port.h" |
50 | | |
51 | | #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC |
52 | | # include <unistd.h> // NOLINT |
53 | | #endif |
54 | | |
55 | | // Silence C4800 (C4800: 'int *const ': forcing value |
56 | | // to bool 'true' or 'false') for MSVC 15 |
57 | | #ifdef _MSC_VER |
58 | | #if _MSC_VER == 1900 |
59 | | # pragma warning(push) |
60 | | # pragma warning(disable:4800) |
61 | | #endif |
62 | | #endif |
63 | | |
64 | | namespace testing { |
65 | | namespace internal { |
66 | | |
67 | | // Protects the mock object registry (in class Mock), all function |
68 | | // mockers, and all expectations. |
69 | | GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); |
70 | | |
71 | | // Logs a message including file and line number information. |
72 | | GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, |
73 | | const char* file, int line, |
74 | 59.6M | const std::string& message) { |
75 | 59.6M | ::std::ostringstream s; |
76 | 59.6M | s << internal::FormatFileLocation(file, line) << " " << message |
77 | 59.6M | << ::std::endl; |
78 | 59.6M | Log(severity, s.str(), 0); |
79 | 59.6M | } |
80 | | |
81 | | // Constructs an ExpectationBase object. |
82 | | ExpectationBase::ExpectationBase(const char* a_file, int a_line, |
83 | | const std::string& a_source_text) |
84 | | : file_(a_file), |
85 | | line_(a_line), |
86 | | source_text_(a_source_text), |
87 | | cardinality_specified_(false), |
88 | | cardinality_(Exactly(1)), |
89 | | call_count_(0), |
90 | | retired_(false), |
91 | | extra_matcher_specified_(false), |
92 | | repeated_action_specified_(false), |
93 | | retires_on_saturation_(false), |
94 | | last_clause_(kNone), |
95 | 926k | action_count_checked_(false) {} |
96 | | |
97 | | // Destructs an ExpectationBase object. |
98 | 925k | ExpectationBase::~ExpectationBase() {} |
99 | | |
100 | | // Explicitly specifies the cardinality of this expectation. Used by |
101 | | // the subclasses to implement the .Times() clause. |
102 | 12.0k | void ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) { |
103 | 12.0k | cardinality_specified_ = true; |
104 | 12.0k | cardinality_ = a_cardinality; |
105 | 12.0k | } |
106 | | |
107 | | // Retires all pre-requisites of this expectation. |
108 | | void ExpectationBase::RetireAllPreRequisites() |
109 | 1.79M | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
110 | 1.79M | if (is_retired()) { |
111 | | // We can take this short-cut as we never retire an expectation |
112 | | // until we have retired all its pre-requisites. |
113 | 0 | return; |
114 | 0 | } |
115 | | |
116 | 1.79M | ::std::vector<ExpectationBase*> expectations(1, this); |
117 | 3.58M | while (!expectations.empty()) { |
118 | 1.79M | ExpectationBase* exp = expectations.back(); |
119 | 1.79M | expectations.pop_back(); |
120 | | |
121 | 1.79M | for (ExpectationSet::const_iterator it = |
122 | 1.79M | exp->immediate_prerequisites_.begin(); |
123 | 1.79M | it != exp->immediate_prerequisites_.end(); ++it) { |
124 | 0 | ExpectationBase* next = it->expectation_base().get(); |
125 | 0 | if (!next->is_retired()) { |
126 | 0 | next->Retire(); |
127 | 0 | expectations.push_back(next); |
128 | 0 | } |
129 | 0 | } |
130 | 1.79M | } |
131 | 1.79M | } |
132 | | |
133 | | // Returns true if and only if all pre-requisites of this expectation |
134 | | // have been satisfied. |
135 | | bool ExpectationBase::AllPrerequisitesAreSatisfied() const |
136 | 3.15M | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
137 | 3.15M | g_gmock_mutex.AssertHeld(); |
138 | 3.15M | ::std::vector<const ExpectationBase*> expectations(1, this); |
139 | 6.30M | while (!expectations.empty()) { |
140 | 3.15M | const ExpectationBase* exp = expectations.back(); |
141 | 3.15M | expectations.pop_back(); |
142 | | |
143 | 3.15M | for (ExpectationSet::const_iterator it = |
144 | 3.15M | exp->immediate_prerequisites_.begin(); |
145 | 3.15M | it != exp->immediate_prerequisites_.end(); ++it) { |
146 | 0 | const ExpectationBase* next = it->expectation_base().get(); |
147 | 0 | if (!next->IsSatisfied()) return false; |
148 | 0 | expectations.push_back(next); |
149 | 0 | } |
150 | 3.15M | } |
151 | 3.15M | return true; |
152 | 3.15M | } |
153 | | |
154 | | // Adds unsatisfied pre-requisites of this expectation to 'result'. |
155 | | void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const |
156 | 0 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
157 | 0 | g_gmock_mutex.AssertHeld(); |
158 | 0 | ::std::vector<const ExpectationBase*> expectations(1, this); |
159 | 0 | while (!expectations.empty()) { |
160 | 0 | const ExpectationBase* exp = expectations.back(); |
161 | 0 | expectations.pop_back(); |
162 | |
|
163 | 0 | for (ExpectationSet::const_iterator it = |
164 | 0 | exp->immediate_prerequisites_.begin(); |
165 | 0 | it != exp->immediate_prerequisites_.end(); ++it) { |
166 | 0 | const ExpectationBase* next = it->expectation_base().get(); |
167 | |
|
168 | 0 | if (next->IsSatisfied()) { |
169 | | // If *it is satisfied and has a call count of 0, some of its |
170 | | // pre-requisites may not be satisfied yet. |
171 | 0 | if (next->call_count_ == 0) { |
172 | 0 | expectations.push_back(next); |
173 | 0 | } |
174 | 0 | } else { |
175 | | // Now that we know next is unsatisfied, we are not so interested |
176 | | // in whether its pre-requisites are satisfied. Therefore we |
177 | | // don't iterate into it here. |
178 | 0 | *result += *it; |
179 | 0 | } |
180 | 0 | } |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | // Describes how many times a function call matching this |
185 | | // expectation has occurred. |
186 | | void ExpectationBase::DescribeCallCountTo(::std::ostream* os) const |
187 | 1.35M | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
188 | 1.35M | g_gmock_mutex.AssertHeld(); |
189 | | |
190 | | // Describes how many times the function is expected to be called. |
191 | 1.35M | *os << " Expected: to be "; |
192 | 1.35M | cardinality().DescribeTo(os); |
193 | 1.35M | *os << "\n Actual: "; |
194 | 1.35M | Cardinality::DescribeActualCallCountTo(call_count(), os); |
195 | | |
196 | | // Describes the state of the expectation (e.g. is it satisfied? |
197 | | // is it active?). |
198 | 1.35M | *os << " - " << (IsOverSaturated() ? "over-saturated" : |
199 | 1.35M | IsSaturated() ? "saturated" : |
200 | 1.35M | IsSatisfied() ? "satisfied" : "unsatisfied") |
201 | 1.35M | << " and " |
202 | 1.35M | << (is_retired() ? "retired" : "active"); |
203 | 1.35M | } |
204 | | |
205 | | // Checks the action count (i.e. the number of WillOnce() and |
206 | | // WillRepeatedly() clauses) against the cardinality if this hasn't |
207 | | // been done before. Prints a warning if there are too many or too |
208 | | // few actions. |
209 | | void ExpectationBase::CheckActionCountIfNotDone() const |
210 | 4.30M | GTEST_LOCK_EXCLUDED_(mutex_) { |
211 | 4.30M | bool should_check = false; |
212 | 4.30M | { |
213 | 4.30M | MutexLock l(&mutex_); |
214 | 4.30M | if (!action_count_checked_) { |
215 | 926k | action_count_checked_ = true; |
216 | 926k | should_check = true; |
217 | 926k | } |
218 | 4.30M | } |
219 | | |
220 | 4.30M | if (should_check) { |
221 | 926k | if (!cardinality_specified_) { |
222 | | // The cardinality was inferred - no need to check the action |
223 | | // count against it. |
224 | 914k | return; |
225 | 914k | } |
226 | | |
227 | | // The cardinality was explicitly specified. |
228 | 12.0k | const int action_count = static_cast<int>(untyped_actions_.size()); |
229 | 12.0k | const int upper_bound = cardinality().ConservativeUpperBound(); |
230 | 12.0k | const int lower_bound = cardinality().ConservativeLowerBound(); |
231 | 12.0k | bool too_many; // True if there are too many actions, or false |
232 | | // if there are too few. |
233 | 12.0k | if (action_count > upper_bound || |
234 | 12.0k | (action_count == upper_bound && repeated_action_specified_)) { |
235 | 0 | too_many = true; |
236 | 12.0k | } else if (0 < action_count && action_count < lower_bound && |
237 | 12.0k | !repeated_action_specified_) { |
238 | 0 | too_many = false; |
239 | 12.0k | } else { |
240 | 12.0k | return; |
241 | 12.0k | } |
242 | | |
243 | 0 | ::std::stringstream ss; |
244 | 0 | DescribeLocationTo(&ss); |
245 | 0 | ss << "Too " << (too_many ? "many" : "few") |
246 | 0 | << " actions specified in " << source_text() << "...\n" |
247 | 0 | << "Expected to be "; |
248 | 0 | cardinality().DescribeTo(&ss); |
249 | 0 | ss << ", but has " << (too_many ? "" : "only ") |
250 | 0 | << action_count << " WillOnce()" |
251 | 0 | << (action_count == 1 ? "" : "s"); |
252 | 0 | if (repeated_action_specified_) { |
253 | 0 | ss << " and a WillRepeatedly()"; |
254 | 0 | } |
255 | 0 | ss << "."; |
256 | 0 | Log(kWarning, ss.str(), -1); // -1 means "don't print stack trace". |
257 | 0 | } |
258 | 4.30M | } |
259 | | |
260 | | // Implements the .Times() clause. |
261 | 12.0k | void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) { |
262 | 12.0k | if (last_clause_ == kTimes) { |
263 | 0 | ExpectSpecProperty(false, |
264 | 0 | ".Times() cannot appear " |
265 | 0 | "more than once in an EXPECT_CALL()."); |
266 | 12.0k | } else { |
267 | 12.0k | ExpectSpecProperty(last_clause_ < kTimes, |
268 | 12.0k | ".Times() cannot appear after " |
269 | 12.0k | ".InSequence(), .WillOnce(), .WillRepeatedly(), " |
270 | 12.0k | "or .RetiresOnSaturation()."); |
271 | 12.0k | } |
272 | 12.0k | last_clause_ = kTimes; |
273 | | |
274 | 12.0k | SpecifyCardinality(a_cardinality); |
275 | 12.0k | } |
276 | | |
277 | | // Points to the implicit sequence introduced by a living InSequence |
278 | | // object (if any) in the current thread or NULL. |
279 | | GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence; |
280 | | |
281 | | // Reports an uninteresting call (whose description is in msg) in the |
282 | | // manner specified by 'reaction'. |
283 | 0 | void ReportUninterestingCall(CallReaction reaction, const std::string& msg) { |
284 | | // Include a stack trace only if --gmock_verbose=info is specified. |
285 | 0 | const int stack_frames_to_skip = |
286 | 0 | GMOCK_FLAG(verbose) == kInfoVerbosity ? 3 : -1; |
287 | 0 | switch (reaction) { |
288 | 0 | case kAllow: |
289 | 0 | Log(kInfo, msg, stack_frames_to_skip); |
290 | 0 | break; |
291 | 0 | case kWarn: |
292 | 0 | Log(kWarning, |
293 | 0 | msg + |
294 | 0 | "\nNOTE: You can safely ignore the above warning unless this " |
295 | 0 | "call should not happen. Do not suppress it by blindly adding " |
296 | 0 | "an EXPECT_CALL() if you don't mean to enforce the call. " |
297 | 0 | "See " |
298 | 0 | "https://github.com/google/googletest/blob/master/googlemock/" |
299 | 0 | "docs/cook_book.md#" |
300 | 0 | "knowing-when-to-expect for details.\n", |
301 | 0 | stack_frames_to_skip); |
302 | 0 | break; |
303 | 0 | default: // FAIL |
304 | 0 | Expect(false, nullptr, -1, msg); |
305 | 0 | } |
306 | 0 | } |
307 | | |
308 | | UntypedFunctionMockerBase::UntypedFunctionMockerBase() |
309 | 133M | : mock_obj_(nullptr), name_("") {} |
310 | | |
311 | 132M | UntypedFunctionMockerBase::~UntypedFunctionMockerBase() {} |
312 | | |
313 | | // Sets the mock object this mock method belongs to, and registers |
314 | | // this information in the global mock registry. Will be called |
315 | | // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock |
316 | | // method. |
317 | | void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) |
318 | 59.6M | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
319 | 59.6M | { |
320 | 59.6M | MutexLock l(&g_gmock_mutex); |
321 | 59.6M | mock_obj_ = mock_obj; |
322 | 59.6M | } |
323 | 59.6M | Mock::Register(mock_obj, this); |
324 | 59.6M | } |
325 | | |
326 | | // Sets the mock object this mock method belongs to, and sets the name |
327 | | // of the mock function. Will be called upon each invocation of this |
328 | | // mock function. |
329 | | void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj, |
330 | | const char* name) |
331 | 39.3M | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
332 | | // We protect name_ under g_gmock_mutex in case this mock function |
333 | | // is called from two threads concurrently. |
334 | 39.3M | MutexLock l(&g_gmock_mutex); |
335 | 39.3M | mock_obj_ = mock_obj; |
336 | 39.3M | name_ = name; |
337 | 39.3M | } |
338 | | |
339 | | // Returns the name of the function being mocked. Must be called |
340 | | // after RegisterOwner() or SetOwnerAndName() has been called. |
341 | | const void* UntypedFunctionMockerBase::MockObject() const |
342 | 97.2M | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
343 | 97.2M | const void* mock_obj; |
344 | 97.2M | { |
345 | | // We protect mock_obj_ under g_gmock_mutex in case this mock |
346 | | // function is called from two threads concurrently. |
347 | 97.2M | MutexLock l(&g_gmock_mutex); |
348 | 97.2M | Assert(mock_obj_ != nullptr, __FILE__, __LINE__, |
349 | 97.2M | "MockObject() must not be called before RegisterOwner() or " |
350 | 97.2M | "SetOwnerAndName() has been called."); |
351 | 97.2M | mock_obj = mock_obj_; |
352 | 97.2M | } |
353 | 97.2M | return mock_obj; |
354 | 97.2M | } |
355 | | |
356 | | // Returns the name of this mock method. Must be called after |
357 | | // SetOwnerAndName() has been called. |
358 | | const char* UntypedFunctionMockerBase::Name() const |
359 | 37.6M | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
360 | 37.6M | const char* name; |
361 | 37.6M | { |
362 | | // We protect name_ under g_gmock_mutex in case this mock |
363 | | // function is called from two threads concurrently. |
364 | 37.6M | MutexLock l(&g_gmock_mutex); |
365 | 37.6M | Assert(name_ != nullptr, __FILE__, __LINE__, |
366 | 37.6M | "Name() must not be called before SetOwnerAndName() has " |
367 | 37.6M | "been called."); |
368 | 37.6M | name = name_; |
369 | 37.6M | } |
370 | 37.6M | return name; |
371 | 37.6M | } |
372 | | |
373 | | // Calculates the result of invoking this mock function with the given |
374 | | // arguments, prints it, and returns it. The caller is responsible |
375 | | // for deleting the result. |
376 | | UntypedActionResultHolderBase* UntypedFunctionMockerBase::UntypedInvokeWith( |
377 | 39.3M | void* const untyped_args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
378 | | // See the definition of untyped_expectations_ for why access to it |
379 | | // is unprotected here. |
380 | 39.3M | if (untyped_expectations_.size() == 0) { |
381 | | // No expectation is set on this mock method - we have an |
382 | | // uninteresting call. |
383 | | |
384 | | // We must get Google Mock's reaction on uninteresting calls |
385 | | // made on this mock object BEFORE performing the action, |
386 | | // because the action may DELETE the mock object and make the |
387 | | // following expression meaningless. |
388 | 37.5M | const CallReaction reaction = |
389 | 37.5M | Mock::GetReactionOnUninterestingCalls(MockObject()); |
390 | | |
391 | | // True if and only if we need to print this call's arguments and return |
392 | | // value. This definition must be kept in sync with |
393 | | // the behavior of ReportUninterestingCall(). |
394 | 37.5M | const bool need_to_report_uninteresting_call = |
395 | | // If the user allows this uninteresting call, we print it |
396 | | // only when they want informational messages. |
397 | 37.5M | reaction == kAllow ? LogIsVisible(kInfo) : |
398 | | // If the user wants this to be a warning, we print |
399 | | // it only when they want to see warnings. |
400 | 37.5M | reaction == kWarn |
401 | 348k | ? LogIsVisible(kWarning) |
402 | 348k | : |
403 | | // Otherwise, the user wants this to be an error, and we |
404 | | // should always print detailed information in the error. |
405 | 348k | true; |
406 | | |
407 | 37.5M | if (!need_to_report_uninteresting_call) { |
408 | | // Perform the action without printing the call information. |
409 | 37.5M | return this->UntypedPerformDefaultAction( |
410 | 37.5M | untyped_args, "Function call: " + std::string(Name())); |
411 | 37.5M | } |
412 | | |
413 | | // Warns about the uninteresting call. |
414 | 0 | ::std::stringstream ss; |
415 | 0 | this->UntypedDescribeUninterestingCall(untyped_args, &ss); |
416 | | |
417 | | // Calculates the function result. |
418 | 0 | UntypedActionResultHolderBase* const result = |
419 | 0 | this->UntypedPerformDefaultAction(untyped_args, ss.str()); |
420 | | |
421 | | // Prints the function result. |
422 | 0 | if (result != nullptr) result->PrintAsActionResult(&ss); |
423 | |
|
424 | 0 | ReportUninterestingCall(reaction, ss.str()); |
425 | 0 | return result; |
426 | 37.5M | } |
427 | | |
428 | 1.80M | bool is_excessive = false; |
429 | 1.80M | ::std::stringstream ss; |
430 | 1.80M | ::std::stringstream why; |
431 | 1.80M | ::std::stringstream loc; |
432 | 1.80M | const void* untyped_action = nullptr; |
433 | | |
434 | | // The UntypedFindMatchingExpectation() function acquires and |
435 | | // releases g_gmock_mutex. |
436 | | |
437 | 1.80M | const ExpectationBase* const untyped_expectation = |
438 | 1.80M | this->UntypedFindMatchingExpectation(untyped_args, &untyped_action, |
439 | 1.80M | &is_excessive, &ss, &why); |
440 | 1.80M | const bool found = untyped_expectation != nullptr; |
441 | | |
442 | | // True if and only if we need to print the call's arguments |
443 | | // and return value. |
444 | | // This definition must be kept in sync with the uses of Expect() |
445 | | // and Log() in this function. |
446 | 1.80M | const bool need_to_report_call = |
447 | 1.80M | !found || is_excessive || LogIsVisible(kInfo); |
448 | 1.80M | if (!need_to_report_call) { |
449 | | // Perform the action without printing the call information. |
450 | 1.79M | return untyped_action == nullptr |
451 | 1.79M | ? this->UntypedPerformDefaultAction(untyped_args, "") |
452 | 1.79M | : this->UntypedPerformAction(untyped_action, untyped_args); |
453 | 1.79M | } |
454 | | |
455 | 9.52k | ss << " Function call: " << Name(); |
456 | 9.52k | this->UntypedPrintArgs(untyped_args, &ss); |
457 | | |
458 | | // In case the action deletes a piece of the expectation, we |
459 | | // generate the message beforehand. |
460 | 9.52k | if (found && !is_excessive) { |
461 | 0 | untyped_expectation->DescribeLocationTo(&loc); |
462 | 0 | } |
463 | | |
464 | 9.52k | UntypedActionResultHolderBase* result = nullptr; |
465 | | |
466 | 9.52k | auto perform_action = [&] { |
467 | 9.52k | return untyped_action == nullptr |
468 | 9.52k | ? this->UntypedPerformDefaultAction(untyped_args, ss.str()) |
469 | 9.52k | : this->UntypedPerformAction(untyped_action, untyped_args); |
470 | 9.52k | }; |
471 | 9.52k | auto handle_failures = [&] { |
472 | 9.52k | ss << "\n" << why.str(); |
473 | | |
474 | 9.52k | if (!found) { |
475 | | // No expectation matches this call - reports a failure. |
476 | 9.52k | Expect(false, nullptr, -1, ss.str()); |
477 | 9.52k | } else if (is_excessive) { |
478 | | // We had an upper-bound violation and the failure message is in ss. |
479 | 0 | Expect(false, untyped_expectation->file(), untyped_expectation->line(), |
480 | 0 | ss.str()); |
481 | 0 | } else { |
482 | | // We had an expected call and the matching expectation is |
483 | | // described in ss. |
484 | 0 | Log(kInfo, loc.str() + ss.str(), 2); |
485 | 0 | } |
486 | 9.52k | }; |
487 | 9.52k | #if GTEST_HAS_EXCEPTIONS |
488 | 9.52k | try { |
489 | 9.52k | result = perform_action(); |
490 | 9.52k | } catch (...) { |
491 | 0 | handle_failures(); |
492 | 0 | throw; |
493 | 0 | } |
494 | | #else |
495 | | result = perform_action(); |
496 | | #endif |
497 | | |
498 | 9.52k | if (result != nullptr) result->PrintAsActionResult(&ss); |
499 | 9.52k | handle_failures(); |
500 | 9.52k | return result; |
501 | 9.52k | } |
502 | | |
503 | | // Returns an Expectation object that references and co-owns exp, |
504 | | // which must be an expectation on this mock function. |
505 | 0 | Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { |
506 | | // See the definition of untyped_expectations_ for why access to it |
507 | | // is unprotected here. |
508 | 0 | for (UntypedExpectations::const_iterator it = |
509 | 0 | untyped_expectations_.begin(); |
510 | 0 | it != untyped_expectations_.end(); ++it) { |
511 | 0 | if (it->get() == exp) { |
512 | 0 | return Expectation(*it); |
513 | 0 | } |
514 | 0 | } |
515 | | |
516 | 0 | Assert(false, __FILE__, __LINE__, "Cannot find expectation."); |
517 | 0 | return Expectation(); |
518 | | // The above statement is just to make the code compile, and will |
519 | | // never be executed. |
520 | 0 | } |
521 | | |
522 | | // Verifies that all expectations on this mock function have been |
523 | | // satisfied. Reports one or more Google Test non-fatal failures |
524 | | // and returns false if not. |
525 | | bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() |
526 | 133M | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
527 | 133M | g_gmock_mutex.AssertHeld(); |
528 | 133M | bool expectations_met = true; |
529 | 133M | for (UntypedExpectations::const_iterator it = |
530 | 133M | untyped_expectations_.begin(); |
531 | 134M | it != untyped_expectations_.end(); ++it) { |
532 | 925k | ExpectationBase* const untyped_expectation = it->get(); |
533 | 925k | if (untyped_expectation->IsOverSaturated()) { |
534 | | // There was an upper-bound violation. Since the error was |
535 | | // already reported when it occurred, there is no need to do |
536 | | // anything here. |
537 | 0 | expectations_met = false; |
538 | 925k | } else if (!untyped_expectation->IsSatisfied()) { |
539 | 286 | expectations_met = false; |
540 | 286 | ::std::stringstream ss; |
541 | 286 | ss << "Actual function call count doesn't match " |
542 | 286 | << untyped_expectation->source_text() << "...\n"; |
543 | | // No need to show the source file location of the expectation |
544 | | // in the description, as the Expect() call that follows already |
545 | | // takes care of it. |
546 | 286 | untyped_expectation->MaybeDescribeExtraMatcherTo(&ss); |
547 | 286 | untyped_expectation->DescribeCallCountTo(&ss); |
548 | 286 | Expect(false, untyped_expectation->file(), |
549 | 286 | untyped_expectation->line(), ss.str()); |
550 | 286 | } |
551 | 925k | } |
552 | | |
553 | | // Deleting our expectations may trigger other mock objects to be deleted, for |
554 | | // example if an action contains a reference counted smart pointer to that |
555 | | // mock object, and that is the last reference. So if we delete our |
556 | | // expectations within the context of the global mutex we may deadlock when |
557 | | // this method is called again. Instead, make a copy of the set of |
558 | | // expectations to delete, clear our set within the mutex, and then clear the |
559 | | // copied set outside of it. |
560 | 133M | UntypedExpectations expectations_to_delete; |
561 | 133M | untyped_expectations_.swap(expectations_to_delete); |
562 | | |
563 | 133M | g_gmock_mutex.Unlock(); |
564 | 133M | expectations_to_delete.clear(); |
565 | 133M | g_gmock_mutex.Lock(); |
566 | | |
567 | 133M | return expectations_met; |
568 | 133M | } |
569 | | |
570 | 348k | CallReaction intToCallReaction(int mock_behavior) { |
571 | 348k | if (mock_behavior >= kAllow && mock_behavior <= kFail) { |
572 | 348k | return static_cast<internal::CallReaction>(mock_behavior); |
573 | 348k | } |
574 | 0 | return kWarn; |
575 | 348k | } |
576 | | |
577 | | } // namespace internal |
578 | | |
579 | | // Class Mock. |
580 | | |
581 | | namespace { |
582 | | |
583 | | typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers; |
584 | | |
585 | | // The current state of a mock object. Such information is needed for |
586 | | // detecting leaked mock objects and explicitly verifying a mock's |
587 | | // expectations. |
588 | | struct MockObjectState { |
589 | | MockObjectState() |
590 | 8.52M | : first_used_file(nullptr), first_used_line(-1), leakable(false) {} |
591 | | |
592 | | // Where in the source file an ON_CALL or EXPECT_CALL is first |
593 | | // invoked on this mock object. |
594 | | const char* first_used_file; |
595 | | int first_used_line; |
596 | | ::std::string first_used_test_suite; |
597 | | ::std::string first_used_test; |
598 | | bool leakable; // true if and only if it's OK to leak the object. |
599 | | FunctionMockers function_mockers; // All registered methods of the object. |
600 | | }; |
601 | | |
602 | | // A global registry holding the state of all mock objects that are |
603 | | // alive. A mock object is added to this registry the first time |
604 | | // Mock::AllowLeak(), ON_CALL(), or EXPECT_CALL() is called on it. It |
605 | | // is removed from the registry in the mock object's destructor. |
606 | | class MockObjectRegistry { |
607 | | public: |
608 | | // Maps a mock object (identified by its address) to its state. |
609 | | typedef std::map<const void*, MockObjectState> StateMap; |
610 | | |
611 | | // This destructor will be called when a program exits, after all |
612 | | // tests in it have been run. By then, there should be no mock |
613 | | // object alive. Therefore we report any living object as test |
614 | | // failure, unless the user explicitly asked us to ignore it. |
615 | 0 | ~MockObjectRegistry() { |
616 | 0 | if (!GMOCK_FLAG(catch_leaked_mocks)) |
617 | 0 | return; |
618 | | |
619 | 0 | int leaked_count = 0; |
620 | 0 | for (StateMap::const_iterator it = states_.begin(); it != states_.end(); |
621 | 0 | ++it) { |
622 | 0 | if (it->second.leakable) // The user said it's fine to leak this object. |
623 | 0 | continue; |
624 | | |
625 | | // FIXME: Print the type of the leaked object. |
626 | | // This can help the user identify the leaked object. |
627 | 0 | std::cout << "\n"; |
628 | 0 | const MockObjectState& state = it->second; |
629 | 0 | std::cout << internal::FormatFileLocation(state.first_used_file, |
630 | 0 | state.first_used_line); |
631 | 0 | std::cout << " ERROR: this mock object"; |
632 | 0 | if (state.first_used_test != "") { |
633 | 0 | std::cout << " (used in test " << state.first_used_test_suite << "." |
634 | 0 | << state.first_used_test << ")"; |
635 | 0 | } |
636 | 0 | std::cout << " should be deleted but never is. Its address is @" |
637 | 0 | << it->first << "."; |
638 | 0 | leaked_count++; |
639 | 0 | } |
640 | 0 | if (leaked_count > 0) { |
641 | 0 | std::cout << "\nERROR: " << leaked_count << " leaked mock " |
642 | 0 | << (leaked_count == 1 ? "object" : "objects") |
643 | 0 | << " found at program exit. Expectations on a mock object are " |
644 | 0 | "verified when the object is destructed. Leaking a mock " |
645 | 0 | "means that its expectations aren't verified, which is " |
646 | 0 | "usually a test bug. If you really intend to leak a mock, " |
647 | 0 | "you can suppress this error using " |
648 | 0 | "testing::Mock::AllowLeak(mock_object), or you may use a " |
649 | 0 | "fake or stub instead of a mock.\n"; |
650 | 0 | std::cout.flush(); |
651 | 0 | ::std::cerr.flush(); |
652 | | // RUN_ALL_TESTS() has already returned when this destructor is |
653 | | // called. Therefore we cannot use the normal Google Test |
654 | | // failure reporting mechanism. |
655 | 0 | _exit(1); // We cannot call exit() as it is not reentrant and |
656 | | // may already have been called. |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | 50.7G | StateMap& states() { return states_; } |
661 | | |
662 | | private: |
663 | | StateMap states_; |
664 | | }; |
665 | | |
666 | | // Protected by g_gmock_mutex. |
667 | | MockObjectRegistry g_mock_object_registry; |
668 | | |
669 | | // Maps a mock object to the reaction Google Mock should have when an |
670 | | // uninteresting method is called. Protected by g_gmock_mutex. |
671 | | std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction; |
672 | | |
673 | | // Sets the reaction Google Mock should have when an uninteresting |
674 | | // method of the given mock object is called. |
675 | | void SetReactionOnUninterestingCalls(const void* mock_obj, |
676 | | internal::CallReaction reaction) |
677 | 11.4M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
678 | 11.4M | internal::MutexLock l(&internal::g_gmock_mutex); |
679 | 11.4M | g_uninteresting_call_reaction[mock_obj] = reaction; |
680 | 11.4M | } |
681 | | |
682 | | } // namespace |
683 | | |
684 | | // Tells Google Mock to allow uninteresting calls on the given mock |
685 | | // object. |
686 | | void Mock::AllowUninterestingCalls(const void* mock_obj) |
687 | 11.4M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
688 | 11.4M | SetReactionOnUninterestingCalls(mock_obj, internal::kAllow); |
689 | 11.4M | } |
690 | | |
691 | | // Tells Google Mock to warn the user about uninteresting calls on the |
692 | | // given mock object. |
693 | | void Mock::WarnUninterestingCalls(const void* mock_obj) |
694 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
695 | 0 | SetReactionOnUninterestingCalls(mock_obj, internal::kWarn); |
696 | 0 | } |
697 | | |
698 | | // Tells Google Mock to fail uninteresting calls on the given mock |
699 | | // object. |
700 | | void Mock::FailUninterestingCalls(const void* mock_obj) |
701 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
702 | 0 | SetReactionOnUninterestingCalls(mock_obj, internal::kFail); |
703 | 0 | } |
704 | | |
705 | | // Tells Google Mock the given mock object is being destroyed and its |
706 | | // entry in the call-reaction table should be removed. |
707 | | void Mock::UnregisterCallReaction(const void* mock_obj) |
708 | 11.4M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
709 | 11.4M | internal::MutexLock l(&internal::g_gmock_mutex); |
710 | 11.4M | g_uninteresting_call_reaction.erase(mock_obj); |
711 | 11.4M | } |
712 | | |
713 | | // Returns the reaction Google Mock will have on uninteresting calls |
714 | | // made on the given mock object. |
715 | | internal::CallReaction Mock::GetReactionOnUninterestingCalls( |
716 | | const void* mock_obj) |
717 | 37.5M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
718 | 37.5M | internal::MutexLock l(&internal::g_gmock_mutex); |
719 | 37.5M | return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? |
720 | 348k | internal::intToCallReaction(GMOCK_FLAG(default_mock_behavior)) : |
721 | 37.5M | g_uninteresting_call_reaction[mock_obj]; |
722 | 37.5M | } |
723 | | |
724 | | // Tells Google Mock to ignore mock_obj when checking for leaked mock |
725 | | // objects. |
726 | | void Mock::AllowLeak(const void* mock_obj) |
727 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
728 | 0 | internal::MutexLock l(&internal::g_gmock_mutex); |
729 | 0 | g_mock_object_registry.states()[mock_obj].leakable = true; |
730 | 0 | } |
731 | | |
732 | | // Verifies and clears all expectations on the given mock object. If |
733 | | // the expectations aren't satisfied, generates one or more Google |
734 | | // Test non-fatal failures and returns false. |
735 | | bool Mock::VerifyAndClearExpectations(void* mock_obj) |
736 | 176k | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
737 | 176k | internal::MutexLock l(&internal::g_gmock_mutex); |
738 | 176k | return VerifyAndClearExpectationsLocked(mock_obj); |
739 | 176k | } |
740 | | |
741 | | // Verifies all expectations on the given mock object and clears its |
742 | | // default actions and expectations. Returns true if and only if the |
743 | | // verification was successful. |
744 | | bool Mock::VerifyAndClear(void* mock_obj) |
745 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
746 | 0 | internal::MutexLock l(&internal::g_gmock_mutex); |
747 | 0 | ClearDefaultActionsLocked(mock_obj); |
748 | 0 | return VerifyAndClearExpectationsLocked(mock_obj); |
749 | 0 | } |
750 | | |
751 | | // Verifies and clears all expectations on the given mock object. If |
752 | | // the expectations aren't satisfied, generates one or more Google |
753 | | // Test non-fatal failures and returns false. |
754 | | bool Mock::VerifyAndClearExpectationsLocked(void* mock_obj) |
755 | 176k | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
756 | 176k | internal::g_gmock_mutex.AssertHeld(); |
757 | 176k | if (g_mock_object_registry.states().count(mock_obj) == 0) { |
758 | | // No EXPECT_CALL() was set on the given mock object. |
759 | 0 | return true; |
760 | 0 | } |
761 | | |
762 | | // Verifies and clears the expectations on each mock method in the |
763 | | // given mock object. |
764 | 176k | bool expectations_met = true; |
765 | 176k | FunctionMockers& mockers = |
766 | 176k | g_mock_object_registry.states()[mock_obj].function_mockers; |
767 | 176k | for (FunctionMockers::const_iterator it = mockers.begin(); |
768 | 530k | it != mockers.end(); ++it) { |
769 | 353k | if (!(*it)->VerifyAndClearExpectationsLocked()) { |
770 | 0 | expectations_met = false; |
771 | 0 | } |
772 | 353k | } |
773 | | |
774 | | // We don't clear the content of mockers, as they may still be |
775 | | // needed by ClearDefaultActionsLocked(). |
776 | 176k | return expectations_met; |
777 | 176k | } |
778 | | |
779 | | bool Mock::IsNaggy(void* mock_obj) |
780 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
781 | 0 | return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kWarn; |
782 | 0 | } |
783 | | bool Mock::IsNice(void* mock_obj) |
784 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
785 | 0 | return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kAllow; |
786 | 0 | } |
787 | | bool Mock::IsStrict(void* mock_obj) |
788 | 0 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
789 | 0 | return Mock::GetReactionOnUninterestingCalls(mock_obj) == internal::kFail; |
790 | 0 | } |
791 | | |
792 | | // Registers a mock object and a mock method it owns. |
793 | | void Mock::Register(const void* mock_obj, |
794 | | internal::UntypedFunctionMockerBase* mocker) |
795 | 59.6M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
796 | 59.6M | internal::MutexLock l(&internal::g_gmock_mutex); |
797 | 59.6M | g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker); |
798 | 59.6M | } |
799 | | |
800 | | // Tells Google Mock where in the source code mock_obj is used in an |
801 | | // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this |
802 | | // information helps the user identify which object it is. |
803 | | void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, |
804 | | const char* file, int line) |
805 | 59.6M | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
806 | 59.6M | internal::MutexLock l(&internal::g_gmock_mutex); |
807 | 59.6M | MockObjectState& state = g_mock_object_registry.states()[mock_obj]; |
808 | 59.6M | if (state.first_used_file == nullptr) { |
809 | 8.52M | state.first_used_file = file; |
810 | 8.52M | state.first_used_line = line; |
811 | 8.52M | const TestInfo* const test_info = |
812 | 8.52M | UnitTest::GetInstance()->current_test_info(); |
813 | 8.52M | if (test_info != nullptr) { |
814 | 0 | state.first_used_test_suite = test_info->test_suite_name(); |
815 | 0 | state.first_used_test = test_info->name(); |
816 | 0 | } |
817 | 8.52M | } |
818 | 59.6M | } |
819 | | |
820 | | // Unregisters a mock method; removes the owning mock object from the |
821 | | // registry when the last mock method associated with it has been |
822 | | // unregistered. This is called only in the destructor of |
823 | | // FunctionMockerBase. |
824 | | void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) |
825 | 132M | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
826 | 132M | internal::g_gmock_mutex.AssertHeld(); |
827 | 132M | for (MockObjectRegistry::StateMap::iterator it = |
828 | 132M | g_mock_object_registry.states().begin(); |
829 | 50.4G | it != g_mock_object_registry.states().end(); ++it) { |
830 | 50.3G | FunctionMockers& mockers = it->second.function_mockers; |
831 | 50.3G | if (mockers.erase(mocker) > 0) { |
832 | | // mocker was in mockers and has been just removed. |
833 | 58.2M | if (mockers.empty()) { |
834 | 8.52M | g_mock_object_registry.states().erase(it); |
835 | 8.52M | } |
836 | 58.2M | return; |
837 | 58.2M | } |
838 | 50.3G | } |
839 | 132M | } |
840 | | |
841 | | // Clears all ON_CALL()s set on the given mock object. |
842 | | void Mock::ClearDefaultActionsLocked(void* mock_obj) |
843 | 0 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
844 | 0 | internal::g_gmock_mutex.AssertHeld(); |
845 | |
|
846 | 0 | if (g_mock_object_registry.states().count(mock_obj) == 0) { |
847 | | // No ON_CALL() was set on the given mock object. |
848 | 0 | return; |
849 | 0 | } |
850 | | |
851 | | // Clears the default actions for each mock method in the given mock |
852 | | // object. |
853 | 0 | FunctionMockers& mockers = |
854 | 0 | g_mock_object_registry.states()[mock_obj].function_mockers; |
855 | 0 | for (FunctionMockers::const_iterator it = mockers.begin(); |
856 | 0 | it != mockers.end(); ++it) { |
857 | 0 | (*it)->ClearDefaultActionsLocked(); |
858 | 0 | } |
859 | | |
860 | | // We don't clear the content of mockers, as they may still be |
861 | | // needed by VerifyAndClearExpectationsLocked(). |
862 | 0 | } |
863 | | |
864 | 0 | Expectation::Expectation() {} |
865 | | |
866 | | Expectation::Expectation( |
867 | | const std::shared_ptr<internal::ExpectationBase>& an_expectation_base) |
868 | 0 | : expectation_base_(an_expectation_base) {} |
869 | | |
870 | 0 | Expectation::~Expectation() {} |
871 | | |
872 | | // Adds an expectation to a sequence. |
873 | 0 | void Sequence::AddExpectation(const Expectation& expectation) const { |
874 | 0 | if (*last_expectation_ != expectation) { |
875 | 0 | if (last_expectation_->expectation_base() != nullptr) { |
876 | 0 | expectation.expectation_base()->immediate_prerequisites_ |
877 | 0 | += *last_expectation_; |
878 | 0 | } |
879 | 0 | *last_expectation_ = expectation; |
880 | 0 | } |
881 | 0 | } |
882 | | |
883 | | // Creates the implicit sequence if there isn't one. |
884 | 0 | InSequence::InSequence() { |
885 | 0 | if (internal::g_gmock_implicit_sequence.get() == nullptr) { |
886 | 0 | internal::g_gmock_implicit_sequence.set(new Sequence); |
887 | 0 | sequence_created_ = true; |
888 | 0 | } else { |
889 | 0 | sequence_created_ = false; |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | | // Deletes the implicit sequence if it was created by the constructor |
894 | | // of this object. |
895 | 0 | InSequence::~InSequence() { |
896 | 0 | if (sequence_created_) { |
897 | 0 | delete internal::g_gmock_implicit_sequence.get(); |
898 | 0 | internal::g_gmock_implicit_sequence.set(nullptr); |
899 | 0 | } |
900 | 0 | } |
901 | | |
902 | | } // namespace testing |
903 | | |
904 | | #ifdef _MSC_VER |
905 | | #if _MSC_VER == 1900 |
906 | | # pragma warning(pop) |
907 | | #endif |
908 | | #endif |