| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 using testing::AnyNumber; | 65 using testing::AnyNumber; |
| 66 using testing::AtLeast; | 66 using testing::AtLeast; |
| 67 using testing::AtMost; | 67 using testing::AtMost; |
| 68 using testing::Between; | 68 using testing::Between; |
| 69 using testing::Cardinality; | 69 using testing::Cardinality; |
| 70 using testing::CardinalityInterface; | 70 using testing::CardinalityInterface; |
| 71 using testing::Const; | 71 using testing::Const; |
| 72 using testing::DoAll; | 72 using testing::DoAll; |
| 73 using testing::DoDefault; | 73 using testing::DoDefault; |
| 74 using testing::GMOCK_FLAG(verbose); | 74 using testing::GMOCK_FLAG(verbose); |
| 75 using testing::Gt; |
| 75 using testing::InSequence; | 76 using testing::InSequence; |
| 76 using testing::Invoke; | 77 using testing::Invoke; |
| 77 using testing::InvokeWithoutArgs; | 78 using testing::InvokeWithoutArgs; |
| 78 using testing::IsSubstring; | 79 using testing::IsSubstring; |
| 79 using testing::Lt; | 80 using testing::Lt; |
| 80 using testing::Message; | 81 using testing::Message; |
| 81 using testing::Mock; | 82 using testing::Mock; |
| 82 using testing::Return; | 83 using testing::Return; |
| 83 using testing::Sequence; | 84 using testing::Sequence; |
| 84 using testing::internal::g_gmock_mutex; | 85 using testing::internal::g_gmock_mutex; |
| 85 using testing::internal::kErrorVerbosity; | 86 using testing::internal::kErrorVerbosity; |
| 86 using testing::internal::kInfoVerbosity; | 87 using testing::internal::kInfoVerbosity; |
| 87 using testing::internal::kWarningVerbosity; | 88 using testing::internal::kWarningVerbosity; |
| 88 using testing::internal::Expectation; | 89 using testing::internal::Expectation; |
| 89 using testing::internal::ExpectationTester; | 90 using testing::internal::ExpectationTester; |
| 90 using testing::internal::string; | 91 using testing::internal::string; |
| 91 | 92 |
| 92 class Result {}; | 93 class Result {}; |
| 93 | 94 |
| 94 class MockA { | 95 class MockA { |
| 95 public: | 96 public: |
| 96 MOCK_METHOD1(DoA, void(int n)); // NOLINT | 97 MOCK_METHOD1(DoA, void(int n)); // NOLINT |
| 97 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT | 98 MOCK_METHOD1(ReturnResult, Result(int n)); // NOLINT |
| 98 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT | 99 MOCK_METHOD2(Binary, bool(int x, int y)); // NOLINT |
| 100 MOCK_METHOD2(ReturnInt, int(int x, int y)); // NOLINT |
| 99 }; | 101 }; |
| 100 | 102 |
| 101 class MockB { | 103 class MockB { |
| 102 public: | 104 public: |
| 103 MOCK_CONST_METHOD0(DoB, int()); // NOLINT | 105 MOCK_CONST_METHOD0(DoB, int()); // NOLINT |
| 104 MOCK_METHOD1(DoB, int(int n)); // NOLINT | 106 MOCK_METHOD1(DoB, int(int n)); // NOLINT |
| 105 }; | 107 }; |
| 106 | 108 |
| 107 // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro | 109 // Tests that EXPECT_CALL and ON_CALL compile in a presence of macro |
| 108 // redefining a mock method name. This could happen, for example, when | 110 // redefining a mock method name. This could happen, for example, when |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { | 166 TEST(OnCallSyntaxTest, EvaluatesSecondArgumentOnce) { |
| 165 MockA a; | 167 MockA a; |
| 166 int n = 0; | 168 int n = 0; |
| 167 | 169 |
| 168 ON_CALL(a, DoA(n++)); | 170 ON_CALL(a, DoA(n++)); |
| 169 EXPECT_EQ(1, n); | 171 EXPECT_EQ(1, n); |
| 170 } | 172 } |
| 171 | 173 |
| 172 // Tests that the syntax of ON_CALL() is enforced at run time. | 174 // Tests that the syntax of ON_CALL() is enforced at run time. |
| 173 | 175 |
| 174 TEST(OnCallSyntaxTest, WithArgumentsIsOptional) { | 176 TEST(OnCallSyntaxTest, WithIsOptional) { |
| 175 MockA a; | 177 MockA a; |
| 176 | 178 |
| 177 ON_CALL(a, DoA(5)) | 179 ON_CALL(a, DoA(5)) |
| 178 .WillByDefault(Return()); | 180 .WillByDefault(Return()); |
| 179 ON_CALL(a, DoA(_)) | 181 ON_CALL(a, DoA(_)) |
| 180 .WithArguments(_) | 182 .With(_) |
| 181 .WillByDefault(Return()); | 183 .WillByDefault(Return()); |
| 182 } | 184 } |
| 183 | 185 |
| 184 TEST(OnCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) { | 186 TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { |
| 185 MockA a; | 187 MockA a; |
| 186 | 188 |
| 187 EXPECT_NONFATAL_FAILURE({ // NOLINT | 189 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 188 ON_CALL(a, ReturnResult(_)) | 190 ON_CALL(a, ReturnResult(_)) |
| 189 .WithArguments(_) | 191 .With(_) |
| 190 .WithArguments(_) | 192 .With(_) |
| 191 .WillByDefault(Return(Result())); | 193 .WillByDefault(Return(Result())); |
| 192 }, ".WithArguments() cannot appear more than once in an ON_CALL()"); | 194 }, ".With() cannot appear more than once in an ON_CALL()"); |
| 193 } | 195 } |
| 194 | 196 |
| 195 #if GTEST_HAS_DEATH_TEST | 197 #if GTEST_HAS_DEATH_TEST |
| 196 | 198 |
| 197 TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { | 199 TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { |
| 198 MockA a; | 200 MockA a; |
| 199 | 201 |
| 200 EXPECT_DEATH({ // NOLINT | 202 EXPECT_DEATH({ // NOLINT |
| 201 ON_CALL(a, DoA(5)); | 203 ON_CALL(a, DoA(5)); |
| 202 a.DoA(5); | 204 a.DoA(5); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 230 MockA a; | 232 MockA a; |
| 231 int n = 0; | 233 int n = 0; |
| 232 | 234 |
| 233 EXPECT_CALL(a, DoA(n++)); | 235 EXPECT_CALL(a, DoA(n++)); |
| 234 a.DoA(0); | 236 a.DoA(0); |
| 235 EXPECT_EQ(1, n); | 237 EXPECT_EQ(1, n); |
| 236 } | 238 } |
| 237 | 239 |
| 238 // Tests that the syntax of EXPECT_CALL() is enforced at run time. | 240 // Tests that the syntax of EXPECT_CALL() is enforced at run time. |
| 239 | 241 |
| 240 TEST(ExpectCallSyntaxTest, WithArgumentsIsOptional) { | 242 TEST(ExpectCallSyntaxTest, WithIsOptional) { |
| 241 MockA a; | 243 MockA a; |
| 242 | 244 |
| 243 EXPECT_CALL(a, DoA(5)) | 245 EXPECT_CALL(a, DoA(5)) |
| 244 .Times(0); | 246 .Times(0); |
| 245 EXPECT_CALL(a, DoA(6)) | 247 EXPECT_CALL(a, DoA(6)) |
| 246 .WithArguments(_) | 248 .With(_) |
| 247 .Times(0); | 249 .Times(0); |
| 248 } | 250 } |
| 249 | 251 |
| 250 TEST(ExpectCallSyntaxTest, WithArgumentsCanAppearAtMostOnce) { | 252 TEST(ExpectCallSyntaxTest, WithCanAppearAtMostOnce) { |
| 251 MockA a; | 253 MockA a; |
| 252 | 254 |
| 253 EXPECT_NONFATAL_FAILURE({ // NOLINT | 255 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 254 EXPECT_CALL(a, DoA(6)) | 256 EXPECT_CALL(a, DoA(6)) |
| 255 .WithArguments(_) | 257 .With(_) |
| 256 .WithArguments(_); | 258 .With(_); |
| 257 }, ".WithArguments() cannot appear more than once in " | 259 }, ".With() cannot appear more than once in an EXPECT_CALL()"); |
| 258 "an EXPECT_CALL()"); | |
| 259 | 260 |
| 260 a.DoA(6); | 261 a.DoA(6); |
| 261 } | 262 } |
| 262 | 263 |
| 263 TEST(ExpectCallSyntaxTest, WithArgumentsMustBeFirstClause) { | 264 TEST(ExpectCallSyntaxTest, WithMustBeFirstClause) { |
| 264 MockA a; | 265 MockA a; |
| 265 | 266 |
| 266 EXPECT_NONFATAL_FAILURE({ // NOLINT | 267 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 267 EXPECT_CALL(a, DoA(1)) | 268 EXPECT_CALL(a, DoA(1)) |
| 268 .Times(1) | 269 .Times(1) |
| 269 .WithArguments(_); | 270 .With(_); |
| 270 }, ".WithArguments() must be the first clause in an " | 271 }, ".With() must be the first clause in an EXPECT_CALL()"); |
| 271 "EXPECT_CALL()"); | |
| 272 | 272 |
| 273 a.DoA(1); | 273 a.DoA(1); |
| 274 | 274 |
| 275 EXPECT_NONFATAL_FAILURE({ // NOLINT | 275 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 276 EXPECT_CALL(a, DoA(2)) | 276 EXPECT_CALL(a, DoA(2)) |
| 277 .WillOnce(Return()) | 277 .WillOnce(Return()) |
| 278 .WithArguments(_); | 278 .With(_); |
| 279 }, ".WithArguments() must be the first clause in an " | 279 }, ".With() must be the first clause in an EXPECT_CALL()"); |
| 280 "EXPECT_CALL()"); | |
| 281 | 280 |
| 282 a.DoA(2); | 281 a.DoA(2); |
| 283 } | 282 } |
| 284 | 283 |
| 285 TEST(ExpectCallSyntaxTest, TimesCanBeInferred) { | 284 TEST(ExpectCallSyntaxTest, TimesCanBeInferred) { |
| 286 MockA a; | 285 MockA a; |
| 287 | 286 |
| 288 EXPECT_CALL(a, DoA(1)) | 287 EXPECT_CALL(a, DoA(1)) |
| 289 .WillOnce(Return()); | 288 .WillOnce(Return()); |
| 290 | 289 |
| (...skipping 1314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1605 // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect | 1604 // Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect |
| 1606 // as --gmock_verbose=warning. | 1605 // as --gmock_verbose=warning. |
| 1607 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { | 1606 TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { |
| 1608 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". | 1607 GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning". |
| 1609 TestExpectedCall(false); | 1608 TestExpectedCall(false); |
| 1610 TestUninterestingCall(true); | 1609 TestUninterestingCall(true); |
| 1611 } | 1610 } |
| 1612 | 1611 |
| 1613 #endif // 0 | 1612 #endif // 0 |
| 1614 | 1613 |
| 1614 // A helper class that generates a failure when printed. We use it to |
| 1615 // ensure that Google Mock doesn't print a value (even to an internal |
| 1616 // buffer) when it is not supposed to do so. |
| 1617 class PrintMeNot {}; |
| 1618 |
| 1619 void PrintTo(PrintMeNot /* dummy */, ::std::ostream* /* os */) { |
| 1620 ADD_FAILURE() << "Google Mock is printing a value that shouldn't be " |
| 1621 << "printed even to an internal buffer."; |
| 1622 } |
| 1623 |
| 1624 class LogTestHelper { |
| 1625 public: |
| 1626 MOCK_METHOD1(Foo, PrintMeNot(PrintMeNot)); |
| 1627 }; |
| 1628 |
| 1629 class GMockLogTest : public ::testing::Test { |
| 1630 protected: |
| 1631 virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } |
| 1632 virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } |
| 1633 |
| 1634 LogTestHelper helper_; |
| 1635 string original_verbose_; |
| 1636 }; |
| 1637 |
| 1638 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsWarning) { |
| 1639 GMOCK_FLAG(verbose) = kWarningVerbosity; |
| 1640 EXPECT_CALL(helper_, Foo(_)) |
| 1641 .WillOnce(Return(PrintMeNot())); |
| 1642 helper_.Foo(PrintMeNot()); // This is an expected call. |
| 1643 } |
| 1644 |
| 1645 TEST_F(GMockLogTest, DoesNotPrintGoodCallInternallyIfVerbosityIsError) { |
| 1646 GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1647 EXPECT_CALL(helper_, Foo(_)) |
| 1648 .WillOnce(Return(PrintMeNot())); |
| 1649 helper_.Foo(PrintMeNot()); // This is an expected call. |
| 1650 } |
| 1651 |
| 1652 TEST_F(GMockLogTest, DoesNotPrintWarningInternallyIfVerbosityIsError) { |
| 1653 GMOCK_FLAG(verbose) = kErrorVerbosity; |
| 1654 ON_CALL(helper_, Foo(_)) |
| 1655 .WillByDefault(Return(PrintMeNot())); |
| 1656 helper_.Foo(PrintMeNot()); // This should generate a warning. |
| 1657 } |
| 1658 |
| 1659 // Tests Mock::AllowLeak(). |
| 1660 |
| 1615 TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { | 1661 TEST(AllowLeakTest, AllowsLeakingUnusedMockObject) { |
| 1616 MockA* a = new MockA; | 1662 MockA* a = new MockA; |
| 1617 Mock::AllowLeak(a); | 1663 Mock::AllowLeak(a); |
| 1618 } | 1664 } |
| 1619 | 1665 |
| 1620 TEST(AllowLeakTest, CanBeCalledBeforeOnCall) { | 1666 TEST(AllowLeakTest, CanBeCalledBeforeOnCall) { |
| 1621 MockA* a = new MockA; | 1667 MockA* a = new MockA; |
| 1622 Mock::AllowLeak(a); | 1668 Mock::AllowLeak(a); |
| 1623 ON_CALL(*a, DoA(_)).WillByDefault(Return()); | 1669 ON_CALL(*a, DoA(_)).WillByDefault(Return()); |
| 1624 a->DoA(0); | 1670 a->DoA(0); |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1957 int main(int argc, char **argv) { | 2003 int main(int argc, char **argv) { |
| 1958 testing::InitGoogleMock(&argc, argv); | 2004 testing::InitGoogleMock(&argc, argv); |
| 1959 | 2005 |
| 1960 // Ensures that the tests pass no matter what value of | 2006 // Ensures that the tests pass no matter what value of |
| 1961 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. | 2007 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 1962 testing::GMOCK_FLAG(catch_leaked_mocks) = true; | 2008 testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 1963 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; | 2009 testing::GMOCK_FLAG(verbose) = testing::internal::kWarningVerbosity; |
| 1964 | 2010 |
| 1965 return RUN_ALL_TESTS(); | 2011 return RUN_ALL_TESTS(); |
| 1966 } | 2012 } |
| OLD | NEW |