@@ -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+
14661470template <typename F>
14671471class 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
0 commit comments