Skip to content

Commit 56246cd

Browse files
jacobsacopybara-github
authored andcommitted
gmock-spec-builders: move a method to the header.
In order to make the diff more readable in an upcoming commit that requires the method to be templated on the action's result type. PiperOrigin-RevId: 451157029 Change-Id: I57beb7544efccd0459efb3a1f039ea45cd7c7602
1 parent cf942a5 commit 56246cd

File tree

2 files changed

+134
-133
lines changed

2 files changed

+134
-133
lines changed

googlemock/include/gmock/gmock-spec-builders.h

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,10 @@ class ActionResultHolder<void> : public UntypedActionResultHolderBase {
14631463
ActionResultHolder& operator=(const ActionResultHolder&) = delete;
14641464
};
14651465

1466+
// Reports an uninteresting call (whose description is in msg) in the
1467+
// manner specified by 'reaction'.
1468+
void ReportUninterestingCall(CallReaction reaction, const std::string& msg);
1469+
14661470
template <typename F>
14671471
class FunctionMocker;
14681472

@@ -1788,9 +1792,136 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
17881792
}
17891793
}; // class FunctionMocker
17901794

1791-
// Reports an uninteresting call (whose description is in msg) in the
1792-
// manner specified by 'reaction'.
1793-
void ReportUninterestingCall(CallReaction reaction, const std::string& msg);
1795+
// Calculates the result of invoking this mock function with the given
1796+
// arguments, prints it, and returns it. The caller is responsible
1797+
// for deleting the result.
1798+
inline UntypedActionResultHolderBase*
1799+
UntypedFunctionMockerBase::UntypedInvokeWith(void* const untyped_args)
1800+
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1801+
// See the definition of untyped_expectations_ for why access to it
1802+
// is unprotected here.
1803+
if (untyped_expectations_.size() == 0) {
1804+
// No expectation is set on this mock method - we have an
1805+
// uninteresting call.
1806+
1807+
// We must get Google Mock's reaction on uninteresting calls
1808+
// made on this mock object BEFORE performing the action,
1809+
// because the action may DELETE the mock object and make the
1810+
// following expression meaningless.
1811+
const CallReaction reaction =
1812+
Mock::GetReactionOnUninterestingCalls(MockObject());
1813+
1814+
// True if and only if we need to print this call's arguments and return
1815+
// value. This definition must be kept in sync with
1816+
// the behavior of ReportUninterestingCall().
1817+
const bool need_to_report_uninteresting_call =
1818+
// If the user allows this uninteresting call, we print it
1819+
// only when they want informational messages.
1820+
reaction == kAllow ? LogIsVisible(kInfo) :
1821+
// If the user wants this to be a warning, we print
1822+
// it only when they want to see warnings.
1823+
reaction == kWarn
1824+
? LogIsVisible(kWarning)
1825+
:
1826+
// Otherwise, the user wants this to be an error, and we
1827+
// should always print detailed information in the error.
1828+
true;
1829+
1830+
if (!need_to_report_uninteresting_call) {
1831+
// Perform the action without printing the call information.
1832+
return this->UntypedPerformDefaultAction(
1833+
untyped_args, "Function call: " + std::string(Name()));
1834+
}
1835+
1836+
// Warns about the uninteresting call.
1837+
::std::stringstream ss;
1838+
this->UntypedDescribeUninterestingCall(untyped_args, &ss);
1839+
1840+
// Calculates the function result.
1841+
UntypedActionResultHolderBase* const result =
1842+
this->UntypedPerformDefaultAction(untyped_args, ss.str());
1843+
1844+
// Prints the function result.
1845+
if (result != nullptr) result->PrintAsActionResult(&ss);
1846+
1847+
ReportUninterestingCall(reaction, ss.str());
1848+
return result;
1849+
}
1850+
1851+
bool is_excessive = false;
1852+
::std::stringstream ss;
1853+
::std::stringstream why;
1854+
::std::stringstream loc;
1855+
const void* untyped_action = nullptr;
1856+
1857+
// The UntypedFindMatchingExpectation() function acquires and
1858+
// releases g_gmock_mutex.
1859+
1860+
const ExpectationBase* const untyped_expectation =
1861+
this->UntypedFindMatchingExpectation(untyped_args, &untyped_action,
1862+
&is_excessive, &ss, &why);
1863+
const bool found = untyped_expectation != nullptr;
1864+
1865+
// True if and only if we need to print the call's arguments
1866+
// and return value.
1867+
// This definition must be kept in sync with the uses of Expect()
1868+
// and Log() in this function.
1869+
const bool need_to_report_call =
1870+
!found || is_excessive || LogIsVisible(kInfo);
1871+
if (!need_to_report_call) {
1872+
// Perform the action without printing the call information.
1873+
return untyped_action == nullptr
1874+
? this->UntypedPerformDefaultAction(untyped_args, "")
1875+
: this->UntypedPerformAction(untyped_action, untyped_args);
1876+
}
1877+
1878+
ss << " Function call: " << Name();
1879+
this->UntypedPrintArgs(untyped_args, &ss);
1880+
1881+
// In case the action deletes a piece of the expectation, we
1882+
// generate the message beforehand.
1883+
if (found && !is_excessive) {
1884+
untyped_expectation->DescribeLocationTo(&loc);
1885+
}
1886+
1887+
UntypedActionResultHolderBase* result = nullptr;
1888+
1889+
auto perform_action = [&] {
1890+
return untyped_action == nullptr
1891+
? this->UntypedPerformDefaultAction(untyped_args, ss.str())
1892+
: this->UntypedPerformAction(untyped_action, untyped_args);
1893+
};
1894+
auto handle_failures = [&] {
1895+
ss << "\n" << why.str();
1896+
1897+
if (!found) {
1898+
// No expectation matches this call - reports a failure.
1899+
Expect(false, nullptr, -1, ss.str());
1900+
} else if (is_excessive) {
1901+
// We had an upper-bound violation and the failure message is in ss.
1902+
Expect(false, untyped_expectation->file(), untyped_expectation->line(),
1903+
ss.str());
1904+
} else {
1905+
// We had an expected call and the matching expectation is
1906+
// described in ss.
1907+
Log(kInfo, loc.str() + ss.str(), 2);
1908+
}
1909+
};
1910+
#if GTEST_HAS_EXCEPTIONS
1911+
try {
1912+
result = perform_action();
1913+
} catch (...) {
1914+
handle_failures();
1915+
throw;
1916+
}
1917+
#else
1918+
result = perform_action();
1919+
#endif
1920+
1921+
if (result != nullptr) result->PrintAsActionResult(&ss);
1922+
handle_failures();
1923+
return result;
1924+
}
17941925

17951926
} // namespace internal
17961927

googlemock/src/gmock-spec-builders.cc

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -369,136 +369,6 @@ const char* UntypedFunctionMockerBase::Name() const
369369
return name;
370370
}
371371

372-
// Calculates the result of invoking this mock function with the given
373-
// arguments, prints it, and returns it. The caller is responsible
374-
// for deleting the result.
375-
UntypedActionResultHolderBase* UntypedFunctionMockerBase::UntypedInvokeWith(
376-
void* const untyped_args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
377-
// See the definition of untyped_expectations_ for why access to it
378-
// is unprotected here.
379-
if (untyped_expectations_.size() == 0) {
380-
// No expectation is set on this mock method - we have an
381-
// uninteresting call.
382-
383-
// We must get Google Mock's reaction on uninteresting calls
384-
// made on this mock object BEFORE performing the action,
385-
// because the action may DELETE the mock object and make the
386-
// following expression meaningless.
387-
const CallReaction reaction =
388-
Mock::GetReactionOnUninterestingCalls(MockObject());
389-
390-
// True if and only if we need to print this call's arguments and return
391-
// value. This definition must be kept in sync with
392-
// the behavior of ReportUninterestingCall().
393-
const bool need_to_report_uninteresting_call =
394-
// If the user allows this uninteresting call, we print it
395-
// only when they want informational messages.
396-
reaction == kAllow ? LogIsVisible(kInfo) :
397-
// If the user wants this to be a warning, we print
398-
// it only when they want to see warnings.
399-
reaction == kWarn
400-
? LogIsVisible(kWarning)
401-
:
402-
// Otherwise, the user wants this to be an error, and we
403-
// should always print detailed information in the error.
404-
true;
405-
406-
if (!need_to_report_uninteresting_call) {
407-
// Perform the action without printing the call information.
408-
return this->UntypedPerformDefaultAction(
409-
untyped_args, "Function call: " + std::string(Name()));
410-
}
411-
412-
// Warns about the uninteresting call.
413-
::std::stringstream ss;
414-
this->UntypedDescribeUninterestingCall(untyped_args, &ss);
415-
416-
// Calculates the function result.
417-
UntypedActionResultHolderBase* const result =
418-
this->UntypedPerformDefaultAction(untyped_args, ss.str());
419-
420-
// Prints the function result.
421-
if (result != nullptr) result->PrintAsActionResult(&ss);
422-
423-
ReportUninterestingCall(reaction, ss.str());
424-
return result;
425-
}
426-
427-
bool is_excessive = false;
428-
::std::stringstream ss;
429-
::std::stringstream why;
430-
::std::stringstream loc;
431-
const void* untyped_action = nullptr;
432-
433-
// The UntypedFindMatchingExpectation() function acquires and
434-
// releases g_gmock_mutex.
435-
436-
const ExpectationBase* const untyped_expectation =
437-
this->UntypedFindMatchingExpectation(untyped_args, &untyped_action,
438-
&is_excessive, &ss, &why);
439-
const bool found = untyped_expectation != nullptr;
440-
441-
// True if and only if we need to print the call's arguments
442-
// and return value.
443-
// This definition must be kept in sync with the uses of Expect()
444-
// and Log() in this function.
445-
const bool need_to_report_call =
446-
!found || is_excessive || LogIsVisible(kInfo);
447-
if (!need_to_report_call) {
448-
// Perform the action without printing the call information.
449-
return untyped_action == nullptr
450-
? this->UntypedPerformDefaultAction(untyped_args, "")
451-
: this->UntypedPerformAction(untyped_action, untyped_args);
452-
}
453-
454-
ss << " Function call: " << Name();
455-
this->UntypedPrintArgs(untyped_args, &ss);
456-
457-
// In case the action deletes a piece of the expectation, we
458-
// generate the message beforehand.
459-
if (found && !is_excessive) {
460-
untyped_expectation->DescribeLocationTo(&loc);
461-
}
462-
463-
UntypedActionResultHolderBase* result = nullptr;
464-
465-
auto perform_action = [&] {
466-
return untyped_action == nullptr
467-
? this->UntypedPerformDefaultAction(untyped_args, ss.str())
468-
: this->UntypedPerformAction(untyped_action, untyped_args);
469-
};
470-
auto handle_failures = [&] {
471-
ss << "\n" << why.str();
472-
473-
if (!found) {
474-
// No expectation matches this call - reports a failure.
475-
Expect(false, nullptr, -1, ss.str());
476-
} else if (is_excessive) {
477-
// We had an upper-bound violation and the failure message is in ss.
478-
Expect(false, untyped_expectation->file(), untyped_expectation->line(),
479-
ss.str());
480-
} else {
481-
// We had an expected call and the matching expectation is
482-
// described in ss.
483-
Log(kInfo, loc.str() + ss.str(), 2);
484-
}
485-
};
486-
#if GTEST_HAS_EXCEPTIONS
487-
try {
488-
result = perform_action();
489-
} catch (...) {
490-
handle_failures();
491-
throw;
492-
}
493-
#else
494-
result = perform_action();
495-
#endif
496-
497-
if (result != nullptr) result->PrintAsActionResult(&ss);
498-
handle_failures();
499-
return result;
500-
}
501-
502372
// Returns an Expectation object that references and co-owns exp,
503373
// which must be an expectation on this mock function.
504374
Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) {

0 commit comments

Comments
 (0)