gmock-spec-builders.cc (googletest-release-1.10.0) | : | gmock-spec-builders.cc (googletest-release-1.11.0) | ||
---|---|---|---|---|
skipping to change at line 38 | skipping to change at line 38 | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// Google Mock - a framework for writing C++ mock classes. | // Google Mock - a framework for writing C++ mock classes. | |||
// | // | |||
// This file implements the spec builder syntax (ON_CALL and | // This file implements the spec builder syntax (ON_CALL and | |||
// EXPECT_CALL). | // EXPECT_CALL). | |||
#include "gmock/gmock-spec-builders.h" | #include "gmock/gmock-spec-builders.h" | |||
#include <stdlib.h> | #include <stdlib.h> | |||
#include <iostream> // NOLINT | #include <iostream> // NOLINT | |||
#include <map> | #include <map> | |||
#include <memory> | #include <memory> | |||
#include <set> | #include <set> | |||
#include <string> | #include <string> | |||
#include <vector> | #include <vector> | |||
#include "gmock/gmock.h" | #include "gmock/gmock.h" | |||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
#include "gtest/internal/gtest-port.h" | ||||
#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC | #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC | |||
# include <unistd.h> // NOLINT | # include <unistd.h> // NOLINT | |||
#endif | #endif | |||
// Silence C4800 (C4800: 'int *const ': forcing value | // Silence C4800 (C4800: 'int *const ': forcing value | |||
// to bool 'true' or 'false') for MSVC 15 | // to bool 'true' or 'false') for MSVC 15 | |||
#ifdef _MSC_VER | #ifdef _MSC_VER | |||
#if _MSC_VER == 1900 | #if _MSC_VER == 1900 | |||
# pragma warning(push) | # pragma warning(push) | |||
skipping to change at line 72 | skipping to change at line 75 | |||
// Protects the mock object registry (in class Mock), all function | // Protects the mock object registry (in class Mock), all function | |||
// mockers, and all expectations. | // mockers, and all expectations. | |||
GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); | GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); | |||
// Logs a message including file and line number information. | // Logs a message including file and line number information. | |||
GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, | GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, | |||
const char* file, int line, | const char* file, int line, | |||
const std::string& message) { | const std::string& message) { | |||
::std::ostringstream s; | ::std::ostringstream s; | |||
s << file << ":" << line << ": " << message << ::std::endl; | s << internal::FormatFileLocation(file, line) << " " << message | |||
<< ::std::endl; | ||||
Log(severity, s.str(), 0); | Log(severity, s.str(), 0); | |||
} | } | |||
// Constructs an ExpectationBase object. | // Constructs an ExpectationBase object. | |||
ExpectationBase::ExpectationBase(const char* a_file, int a_line, | ExpectationBase::ExpectationBase(const char* a_file, int a_line, | |||
const std::string& a_source_text) | const std::string& a_source_text) | |||
: file_(a_file), | : file_(a_file), | |||
line_(a_line), | line_(a_line), | |||
source_text_(a_source_text), | source_text_(a_source_text), | |||
cardinality_specified_(false), | cardinality_specified_(false), | |||
skipping to change at line 293 | skipping to change at line 297 | |||
case kAllow: | case kAllow: | |||
Log(kInfo, msg, stack_frames_to_skip); | Log(kInfo, msg, stack_frames_to_skip); | |||
break; | break; | |||
case kWarn: | case kWarn: | |||
Log(kWarning, | Log(kWarning, | |||
msg + | msg + | |||
"\nNOTE: You can safely ignore the above warning unless this " | "\nNOTE: You can safely ignore the above warning unless this " | |||
"call should not happen. Do not suppress it by blindly adding " | "call should not happen. Do not suppress it by blindly adding " | |||
"an EXPECT_CALL() if you don't mean to enforce the call. " | "an EXPECT_CALL() if you don't mean to enforce the call. " | |||
"See " | "See " | |||
"https://github.com/google/googletest/blob/master/googlemock/" | "https://github.com/google/googletest/blob/master/docs/" | |||
"docs/cook_book.md#" | "gmock_cook_book.md#" | |||
"knowing-when-to-expect for details.\n", | "knowing-when-to-expect for details.\n", | |||
stack_frames_to_skip); | stack_frames_to_skip); | |||
break; | break; | |||
default: // FAIL | default: // FAIL | |||
Expect(false, nullptr, -1, msg); | Expect(false, nullptr, -1, msg); | |||
} | } | |||
} | } | |||
UntypedFunctionMockerBase::UntypedFunctionMockerBase() | UntypedFunctionMockerBase::UntypedFunctionMockerBase() | |||
: mock_obj_(nullptr), name_("") {} | : mock_obj_(nullptr), name_("") {} | |||
skipping to change at line 431 | skipping to change at line 435 | |||
} | } | |||
bool is_excessive = false; | bool is_excessive = false; | |||
::std::stringstream ss; | ::std::stringstream ss; | |||
::std::stringstream why; | ::std::stringstream why; | |||
::std::stringstream loc; | ::std::stringstream loc; | |||
const void* untyped_action = nullptr; | const void* untyped_action = nullptr; | |||
// The UntypedFindMatchingExpectation() function acquires and | // The UntypedFindMatchingExpectation() function acquires and | |||
// releases g_gmock_mutex. | // releases g_gmock_mutex. | |||
const ExpectationBase* const untyped_expectation = | const ExpectationBase* const untyped_expectation = | |||
this->UntypedFindMatchingExpectation( | this->UntypedFindMatchingExpectation(untyped_args, &untyped_action, | |||
untyped_args, &untyped_action, &is_excessive, | &is_excessive, &ss, &why); | |||
&ss, &why); | ||||
const bool found = untyped_expectation != nullptr; | const bool found = untyped_expectation != nullptr; | |||
// True if and only if we need to print the call's arguments | // True if and only if we need to print the call's arguments | |||
// and return value. | // and return value. | |||
// This definition must be kept in sync with the uses of Expect() | // This definition must be kept in sync with the uses of Expect() | |||
// and Log() in this function. | // and Log() in this function. | |||
const bool need_to_report_call = | const bool need_to_report_call = | |||
!found || is_excessive || LogIsVisible(kInfo); | !found || is_excessive || LogIsVisible(kInfo); | |||
if (!need_to_report_call) { | if (!need_to_report_call) { | |||
// Perform the action without printing the call information. | // Perform the action without printing the call information. | |||
skipping to change at line 459 | skipping to change at line 463 | |||
ss << " Function call: " << Name(); | ss << " Function call: " << Name(); | |||
this->UntypedPrintArgs(untyped_args, &ss); | this->UntypedPrintArgs(untyped_args, &ss); | |||
// In case the action deletes a piece of the expectation, we | // In case the action deletes a piece of the expectation, we | |||
// generate the message beforehand. | // generate the message beforehand. | |||
if (found && !is_excessive) { | if (found && !is_excessive) { | |||
untyped_expectation->DescribeLocationTo(&loc); | untyped_expectation->DescribeLocationTo(&loc); | |||
} | } | |||
UntypedActionResultHolderBase* const result = | UntypedActionResultHolderBase* result = nullptr; | |||
untyped_action == nullptr | ||||
? this->UntypedPerformDefaultAction(untyped_args, ss.str()) | ||||
: this->UntypedPerformAction(untyped_action, untyped_args); | ||||
if (result != nullptr) result->PrintAsActionResult(&ss); | ||||
ss << "\n" << why.str(); | ||||
if (!found) { | auto perform_action = [&] { | |||
// No expectation matches this call - reports a failure. | return untyped_action == nullptr | |||
Expect(false, nullptr, -1, ss.str()); | ? this->UntypedPerformDefaultAction(untyped_args, ss.str()) | |||
} else if (is_excessive) { | : this->UntypedPerformAction(untyped_action, untyped_args); | |||
// We had an upper-bound violation and the failure message is in ss. | }; | |||
Expect(false, untyped_expectation->file(), | auto handle_failures = [&] { | |||
untyped_expectation->line(), ss.str()); | ss << "\n" << why.str(); | |||
} else { | ||||
// We had an expected call and the matching expectation is | if (!found) { | |||
// described in ss. | // No expectation matches this call - reports a failure. | |||
Log(kInfo, loc.str() + ss.str(), 2); | Expect(false, nullptr, -1, ss.str()); | |||
} else if (is_excessive) { | ||||
// We had an upper-bound violation and the failure message is in ss. | ||||
Expect(false, untyped_expectation->file(), untyped_expectation->line(), | ||||
ss.str()); | ||||
} else { | ||||
// We had an expected call and the matching expectation is | ||||
// described in ss. | ||||
Log(kInfo, loc.str() + ss.str(), 2); | ||||
} | ||||
}; | ||||
#if GTEST_HAS_EXCEPTIONS | ||||
try { | ||||
result = perform_action(); | ||||
} catch (...) { | ||||
handle_failures(); | ||||
throw; | ||||
} | } | |||
#else | ||||
result = perform_action(); | ||||
#endif | ||||
if (result != nullptr) result->PrintAsActionResult(&ss); | ||||
handle_failures(); | ||||
return result; | return result; | |||
} | } | |||
// Returns an Expectation object that references and co-owns exp, | // Returns an Expectation object that references and co-owns exp, | |||
// which must be an expectation on this mock function. | // which must be an expectation on this mock function. | |||
Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { | Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { | |||
// See the definition of untyped_expectations_ for why access to it | // See the definition of untyped_expectations_ for why access to it | |||
// is unprotected here. | // is unprotected here. | |||
for (UntypedExpectations::const_iterator it = | for (UntypedExpectations::const_iterator it = | |||
untyped_expectations_.begin(); | untyped_expectations_.begin(); | |||
skipping to change at line 622 | skipping to change at line 642 | |||
std::cout << " (used in test " << state.first_used_test_suite << "." | std::cout << " (used in test " << state.first_used_test_suite << "." | |||
<< state.first_used_test << ")"; | << state.first_used_test << ")"; | |||
} | } | |||
std::cout << " should be deleted but never is. Its address is @" | std::cout << " should be deleted but never is. Its address is @" | |||
<< it->first << "."; | << it->first << "."; | |||
leaked_count++; | leaked_count++; | |||
} | } | |||
if (leaked_count > 0) { | if (leaked_count > 0) { | |||
std::cout << "\nERROR: " << leaked_count << " leaked mock " | std::cout << "\nERROR: " << leaked_count << " leaked mock " | |||
<< (leaked_count == 1 ? "object" : "objects") | << (leaked_count == 1 ? "object" : "objects") | |||
<< " found at program exit. Expectations on a mock object is " | << " found at program exit. Expectations on a mock object are " | |||
"verified when the object is destructed. Leaking a mock " | "verified when the object is destructed. Leaking a mock " | |||
"means that its expectations aren't verified, which is " | "means that its expectations aren't verified, which is " | |||
"usually a test bug. If you really intend to leak a mock, " | "usually a test bug. If you really intend to leak a mock, " | |||
"you can suppress this error using " | "you can suppress this error using " | |||
"testing::Mock::AllowLeak(mock_object), or you may use a " | "testing::Mock::AllowLeak(mock_object), or you may use a " | |||
"fake or stub instead of a mock.\n"; | "fake or stub instead of a mock.\n"; | |||
std::cout.flush(); | std::cout.flush(); | |||
::std::cerr.flush(); | ::std::cerr.flush(); | |||
// RUN_ALL_TESTS() has already returned when this destructor is | // RUN_ALL_TESTS() has already returned when this destructor is | |||
// called. Therefore we cannot use the normal Google Test | // called. Therefore we cannot use the normal Google Test | |||
End of changes. 12 change blocks. | ||||
24 lines changed or deleted | 44 lines changed or added |