OLD | NEW |
1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 1765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1776 MockC() {} | 1776 MockC() {} |
1777 | 1777 |
1778 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, | 1778 MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, |
1779 const Printable& x, Unprintable y)); | 1779 const Printable& x, Unprintable y)); |
1780 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT | 1780 MOCK_METHOD0(NonVoidMethod, int()); // NOLINT |
1781 | 1781 |
1782 private: | 1782 private: |
1783 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); | 1783 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockC); |
1784 }; | 1784 }; |
1785 | 1785 |
| 1786 class VerboseFlagPreservingFixture : public testing::Test { |
| 1787 protected: |
| 1788 // The code needs to work when both ::string and ::std::string are defined |
| 1789 // and the flag is implemented as a testing::internal::String. In this |
| 1790 // case, without the call to c_str(), the compiler will complain that it |
| 1791 // cannot figure out what overload of string constructor to use. |
| 1792 // TODO(vladl@google.com): Use internal::string instead of String for |
| 1793 // string flags in Google Test. |
| 1794 VerboseFlagPreservingFixture() |
| 1795 : saved_verbose_flag_(GMOCK_FLAG(verbose).c_str()) {} |
| 1796 |
| 1797 ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; } |
| 1798 |
| 1799 private: |
| 1800 const string saved_verbose_flag_; |
| 1801 |
| 1802 GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture); |
| 1803 }; |
| 1804 |
1786 #if GTEST_HAS_STREAM_REDIRECTION_ | 1805 #if GTEST_HAS_STREAM_REDIRECTION_ |
1787 | 1806 |
1788 // Tests that an uninteresting mock function call generates a warning | 1807 // Tests that an uninteresting mock function call generates a warning |
1789 // containing the stack trace. | 1808 // containing the stack trace. |
1790 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { | 1809 TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) { |
1791 MockC c; | 1810 MockC c; |
1792 CaptureStdout(); | 1811 CaptureStdout(); |
1793 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); | 1812 c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable()); |
1794 const String output = GetCapturedStdout(); | 1813 const String output = GetCapturedStdout(); |
1795 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); | 1814 EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", output); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1835 ContainsRegex( | 1854 ContainsRegex( |
1836 "Uninteresting mock function call - returning directly\\.\n" | 1855 "Uninteresting mock function call - returning directly\\.\n" |
1837 " Function call: VoidMethod" | 1856 " Function call: VoidMethod" |
1838 "\\(false, 5, \"Hi\", NULL, @.+ " | 1857 "\\(false, 5, \"Hi\", NULL, @.+ " |
1839 "Printable, 4-byte object <0000 0000>\\)")); | 1858 "Printable, 4-byte object <0000 0000>\\)")); |
1840 // A void function has no return value to print. | 1859 // A void function has no return value to print. |
1841 } | 1860 } |
1842 | 1861 |
1843 // Tests how the --gmock_verbose flag affects Google Mock's output. | 1862 // Tests how the --gmock_verbose flag affects Google Mock's output. |
1844 | 1863 |
1845 class GMockVerboseFlagTest : public testing::Test { | 1864 class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { |
1846 public: | 1865 public: |
1847 // Verifies that the given Google Mock output is correct. (When | 1866 // Verifies that the given Google Mock output is correct. (When |
1848 // should_print is true, the output should match the given regex and | 1867 // should_print is true, the output should match the given regex and |
1849 // contain the given function name in the stack trace. When it's | 1868 // contain the given function name in the stack trace. When it's |
1850 // false, the output should be empty.) | 1869 // false, the output should be empty.) |
1851 void VerifyOutput(const String& output, bool should_print, | 1870 void VerifyOutput(const String& output, bool should_print, |
1852 const string& expected_substring, | 1871 const string& expected_substring, |
1853 const string& function_name) { | 1872 const string& function_name) { |
1854 if (should_print) { | 1873 if (should_print) { |
1855 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); | 1874 EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1975 class LogTestHelper { | 1994 class LogTestHelper { |
1976 public: | 1995 public: |
1977 LogTestHelper() {} | 1996 LogTestHelper() {} |
1978 | 1997 |
1979 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); | 1998 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); |
1980 | 1999 |
1981 private: | 2000 private: |
1982 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper); | 2001 GTEST_DISALLOW_COPY_AND_ASSIGN_(LogTestHelper); |
1983 }; | 2002 }; |
1984 | 2003 |
1985 class GMockLogTest : public ::testing::Test { | 2004 class GMockLogTest : public VerboseFlagPreservingFixture { |
1986 protected: | 2005 protected: |
1987 virtual void SetUp() { | |
1988 // The code needs to work when both ::string and ::std::string are | |
1989 // defined and the flag is implemented as a | |
1990 // testing::internal::String. In this case, without the call to | |
1991 // c_str(), the compiler will complain that it cannot figure out | |
1992 // whether the String flag should be converted to a ::string or an | |
1993 // ::std::string before being assigned to original_verbose_. | |
1994 original_verbose_ = GMOCK_FLAG(verbose).c_str(); | |
1995 } | |
1996 | |
1997 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } | |
1998 | |
1999 LogTestHelper helper_; | 2006 LogTestHelper helper_; |
2000 string original_verbose_; | |
2001 }; | 2007 }; |
2002 | 2008 |
2003 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { | 2009 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { |
2004 GMOCK_FLAG(verbose) = kWarningVerbosity; | 2010 GMOCK_FLAG(verbose) = kWarningVerbosity; |
2005 EXPECT_CALL(helper_, Foo(_)) | 2011 EXPECT_CALL(helper_, Foo(_)) |
2006 .WillOnce(Return(PrintMeNot())); | 2012 .WillOnce(Return(PrintMeNot())); |
2007 helper_.Foo(PrintMeNot()); // This is an expected call. | 2013 helper_.Foo(PrintMeNot()); // This is an expected call. |
2008 } | 2014 } |
2009 | 2015 |
2010 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { | 2016 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2351 | 2357 |
2352 EXPECT_EQ(1, b1.DoB()); | 2358 EXPECT_EQ(1, b1.DoB()); |
2353 EXPECT_EQ(2, b1.DoB(0)); | 2359 EXPECT_EQ(2, b1.DoB(0)); |
2354 } | 2360 } |
2355 | 2361 |
2356 // Tests that a mock function's action can call a mock function | 2362 // Tests that a mock function's action can call a mock function |
2357 // (either the same function or a different one) either as an explicit | 2363 // (either the same function or a different one) either as an explicit |
2358 // action or as a default action without causing a dead lock. It | 2364 // action or as a default action without causing a dead lock. It |
2359 // verifies that the action is not performed inside the critical | 2365 // verifies that the action is not performed inside the critical |
2360 // section. | 2366 // section. |
| 2367 TEST(SynchronizationTest, CanCallMockMethodInAction) { |
| 2368 MockA a; |
| 2369 MockC c; |
| 2370 ON_CALL(a, DoA(_)) |
| 2371 .WillByDefault(IgnoreResult(InvokeWithoutArgs(&c, |
| 2372 &MockC::NonVoidMethod))); |
| 2373 EXPECT_CALL(a, DoA(1)); |
| 2374 EXPECT_CALL(a, DoA(1)) |
| 2375 .WillOnce(Invoke(&a, &MockA::DoA)) |
| 2376 .RetiresOnSaturation(); |
| 2377 EXPECT_CALL(c, NonVoidMethod()); |
2361 | 2378 |
2362 void Helper(MockC* c) { | 2379 a.DoA(1); |
2363 c->NonVoidMethod(); | 2380 // This will match the second EXPECT_CALL() and trigger another a.DoA(1), |
| 2381 // which will in turn match the first EXPECT_CALL() and trigger a call to |
| 2382 // c.NonVoidMethod() that was specified by the ON_CALL() since the first |
| 2383 // EXPECT_CALL() did not specify an action. |
2364 } | 2384 } |
2365 | 2385 |
2366 } // namespace | 2386 } // namespace |
2367 | 2387 |
2368 // Allows the user to define his own main and then invoke gmock_main | 2388 // Allows the user to define his own main and then invoke gmock_main |
2369 // from it. This might be necessary on some platforms which require | 2389 // from it. This might be necessary on some platforms which require |
2370 // specific setup and teardown. | 2390 // specific setup and teardown. |
2371 #if GMOCK_RENAME_MAIN | 2391 #if GMOCK_RENAME_MAIN |
2372 int gmock_main(int argc, char **argv) { | 2392 int gmock_main(int argc, char **argv) { |
2373 #else | 2393 #else |
2374 int main(int argc, char **argv) { | 2394 int main(int argc, char **argv) { |
2375 #endif // GMOCK_RENAME_MAIN | 2395 #endif // GMOCK_RENAME_MAIN |
2376 testing::InitGoogleMock(&argc, argv); | 2396 testing::InitGoogleMock(&argc, argv); |
2377 | 2397 |
2378 // Ensures that the tests pass no matter what value of | 2398 // Ensures that the tests pass no matter what value of |
2379 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | 2399 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
2380 testing::GMOCK_FLAG(catch_leaked_mocks) = true; | 2400 testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
2381 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; | 2401 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
2382 | 2402 |
2383 return RUN_ALL_TESTS(); | 2403 return RUN_ALL_TESTS(); |
2384 } | 2404 } |
OLD | NEW |