shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 32 | #include "gmock/gmock-generated-nice-strict.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 33 | |
| 34 | #include <string> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 35 | #include "gmock/gmock.h" |
| 36 | #include "gtest/gtest.h" |
| 37 | #include "gtest/gtest-spi.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 38 | |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 39 | // This must not be defined inside the ::testing namespace, or it will |
| 40 | // clash with ::testing::Mock. |
| 41 | class Mock { |
| 42 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 43 | Mock() {} |
| 44 | |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 45 | MOCK_METHOD0(DoThis, void()); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 51 | namespace testing { |
| 52 | namespace gmock_nice_strict_test { |
| 53 | |
| 54 | using testing::internal::string; |
| 55 | using testing::GMOCK_FLAG(verbose); |
| 56 | using testing::HasSubstr; |
| 57 | using testing::NiceMock; |
| 58 | using testing::StrictMock; |
| 59 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 60 | #if GTEST_HAS_STREAM_REDIRECTION |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 61 | using testing::internal::CaptureStdout; |
| 62 | using testing::internal::GetCapturedStdout; |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 63 | #endif |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 64 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 65 | // Defines some mock classes needed by the tests. |
| 66 | |
| 67 | class Foo { |
| 68 | public: |
| 69 | virtual ~Foo() {} |
| 70 | |
| 71 | virtual void DoThis() = 0; |
| 72 | virtual int DoThat(bool flag) = 0; |
| 73 | }; |
| 74 | |
| 75 | class MockFoo : public Foo { |
| 76 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 77 | MockFoo() {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 78 | void Delete() { delete this; } |
| 79 | |
| 80 | MOCK_METHOD0(DoThis, void()); |
| 81 | MOCK_METHOD1(DoThat, int(bool flag)); |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class MockBar { |
| 88 | public: |
| 89 | explicit MockBar(const string& s) : str_(s) {} |
| 90 | |
| 91 | MockBar(char a1, char a2, string a3, string a4, int a5, int a6, |
| 92 | const string& a7, const string& a8, bool a9, bool a10) { |
| 93 | str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
| 94 | static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
| 95 | } |
| 96 | |
| 97 | virtual ~MockBar() {} |
| 98 | |
| 99 | const string& str() const { return str_; } |
| 100 | |
| 101 | MOCK_METHOD0(This, int()); |
| 102 | MOCK_METHOD2(That, string(int, bool)); |
| 103 | |
| 104 | private: |
| 105 | string str_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 106 | |
| 107 | GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 110 | #if GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | |
| 112 | // Tests that a nice mock generates no warning for uninteresting calls. |
| 113 | TEST(NiceMockTest, NoWarningForUninterestingCall) { |
| 114 | NiceMock<MockFoo> nice_foo; |
| 115 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 116 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 117 | nice_foo.DoThis(); |
| 118 | nice_foo.DoThat(true); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 119 | EXPECT_STREQ("", GetCapturedStdout().c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Tests that a nice mock generates no warning for uninteresting calls |
| 123 | // that delete the mock object. |
| 124 | TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) { |
| 125 | NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>; |
| 126 | |
| 127 | ON_CALL(*nice_foo, DoThis()) |
| 128 | .WillByDefault(Invoke(nice_foo, &MockFoo::Delete)); |
| 129 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 130 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 131 | nice_foo->DoThis(); |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 132 | EXPECT_STREQ("", GetCapturedStdout().c_str()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Tests that a nice mock generates informational logs for |
| 136 | // uninteresting calls. |
| 137 | TEST(NiceMockTest, InfoForUninterestingCall) { |
| 138 | NiceMock<MockFoo> nice_foo; |
| 139 | |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 140 | const string saved_flag = GMOCK_FLAG(verbose); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 141 | GMOCK_FLAG(verbose) = "info"; |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 142 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 143 | nice_foo.DoThis(); |
jgm | 38513a8 | 2012-11-15 15:50:36 +0000 | [diff] [blame^] | 144 | EXPECT_THAT(std::string(GetCapturedStdout()), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 145 | HasSubstr("Uninteresting mock function call")); |
| 146 | |
zhanyong.wan | 470df42 | 2010-02-02 22:34:58 +0000 | [diff] [blame] | 147 | CaptureStdout(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 148 | nice_foo.DoThat(true); |
jgm | 38513a8 | 2012-11-15 15:50:36 +0000 | [diff] [blame^] | 149 | EXPECT_THAT(std::string(GetCapturedStdout()), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 150 | HasSubstr("Uninteresting mock function call")); |
vladlosev | 76c1c61 | 2010-05-05 19:47:46 +0000 | [diff] [blame] | 151 | GMOCK_FLAG(verbose) = saved_flag; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 152 | } |
| 153 | |
zhanyong.wan | 2516f60 | 2010-08-31 18:28:02 +0000 | [diff] [blame] | 154 | #endif // GTEST_HAS_STREAM_REDIRECTION |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 155 | |
| 156 | // Tests that a nice mock allows expected calls. |
| 157 | TEST(NiceMockTest, AllowsExpectedCall) { |
| 158 | NiceMock<MockFoo> nice_foo; |
| 159 | |
| 160 | EXPECT_CALL(nice_foo, DoThis()); |
| 161 | nice_foo.DoThis(); |
| 162 | } |
| 163 | |
| 164 | // Tests that an unexpected call on a nice mock fails. |
| 165 | TEST(NiceMockTest, UnexpectedCallFails) { |
| 166 | NiceMock<MockFoo> nice_foo; |
| 167 | |
| 168 | EXPECT_CALL(nice_foo, DoThis()).Times(0); |
| 169 | EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected"); |
| 170 | } |
| 171 | |
| 172 | // Tests that NiceMock works with a mock class that has a non-default |
| 173 | // constructor. |
| 174 | TEST(NiceMockTest, NonDefaultConstructor) { |
| 175 | NiceMock<MockBar> nice_bar("hi"); |
| 176 | EXPECT_EQ("hi", nice_bar.str()); |
| 177 | |
| 178 | nice_bar.This(); |
| 179 | nice_bar.That(5, true); |
| 180 | } |
| 181 | |
| 182 | // Tests that NiceMock works with a mock class that has a 10-ary |
| 183 | // non-default constructor. |
| 184 | TEST(NiceMockTest, NonDefaultConstructor10) { |
| 185 | NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
| 186 | "g", "h", true, false); |
| 187 | EXPECT_EQ("abcdefghTF", nice_bar.str()); |
| 188 | |
| 189 | nice_bar.This(); |
| 190 | nice_bar.That(5, true); |
| 191 | } |
| 192 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 193 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 194 | // Tests that NiceMock<Mock> compiles where Mock is a user-defined |
| 195 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 196 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 197 | // NiceMock to be looked up in the wrong context, and this test |
| 198 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 199 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 200 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 201 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 202 | TEST(NiceMockTest, AcceptsClassNamedMock) { |
| 203 | NiceMock< ::Mock> nice; |
| 204 | EXPECT_CALL(nice, DoThis()); |
| 205 | nice.DoThis(); |
| 206 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 207 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 208 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 209 | // Tests that a strict mock allows expected calls. |
| 210 | TEST(StrictMockTest, AllowsExpectedCall) { |
| 211 | StrictMock<MockFoo> strict_foo; |
| 212 | |
| 213 | EXPECT_CALL(strict_foo, DoThis()); |
| 214 | strict_foo.DoThis(); |
| 215 | } |
| 216 | |
| 217 | // Tests that an unexpected call on a strict mock fails. |
| 218 | TEST(StrictMockTest, UnexpectedCallFails) { |
| 219 | StrictMock<MockFoo> strict_foo; |
| 220 | |
| 221 | EXPECT_CALL(strict_foo, DoThis()).Times(0); |
| 222 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 223 | "called more times than expected"); |
| 224 | } |
| 225 | |
| 226 | // Tests that an uninteresting call on a strict mock fails. |
| 227 | TEST(StrictMockTest, UninterestingCallFails) { |
| 228 | StrictMock<MockFoo> strict_foo; |
| 229 | |
| 230 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 231 | "Uninteresting mock function call"); |
| 232 | } |
| 233 | |
| 234 | // Tests that an uninteresting call on a strict mock fails, even if |
| 235 | // the call deletes the mock object. |
| 236 | TEST(StrictMockTest, UninterestingCallFailsAfterDeath) { |
| 237 | StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>; |
| 238 | |
| 239 | ON_CALL(*strict_foo, DoThis()) |
| 240 | .WillByDefault(Invoke(strict_foo, &MockFoo::Delete)); |
| 241 | |
| 242 | EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(), |
| 243 | "Uninteresting mock function call"); |
| 244 | } |
| 245 | |
| 246 | // Tests that StrictMock works with a mock class that has a |
| 247 | // non-default constructor. |
| 248 | TEST(StrictMockTest, NonDefaultConstructor) { |
| 249 | StrictMock<MockBar> strict_bar("hi"); |
| 250 | EXPECT_EQ("hi", strict_bar.str()); |
| 251 | |
| 252 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 253 | "Uninteresting mock function call"); |
| 254 | } |
| 255 | |
| 256 | // Tests that StrictMock works with a mock class that has a 10-ary |
| 257 | // non-default constructor. |
| 258 | TEST(StrictMockTest, NonDefaultConstructor10) { |
| 259 | StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
| 260 | "g", "h", true, false); |
| 261 | EXPECT_EQ("abcdefghTF", strict_bar.str()); |
| 262 | |
| 263 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 264 | "Uninteresting mock function call"); |
| 265 | } |
| 266 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 267 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 268 | // Tests that StrictMock<Mock> compiles where Mock is a user-defined |
| 269 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 270 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 271 | // StrictMock to be looked up in the wrong context, and this test |
| 272 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 273 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 274 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 275 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 276 | TEST(StrictMockTest, AcceptsClassNamedMock) { |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 277 | StrictMock< ::Mock> strict; |
| 278 | EXPECT_CALL(strict, DoThis()); |
| 279 | strict.DoThis(); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 280 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 281 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 282 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 283 | } // namespace gmock_nice_strict_test |
| 284 | } // namespace testing |