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 | |
| 32 | #include <gmock/gmock-generated-nice-strict.h> |
| 33 | |
| 34 | #include <string> |
| 35 | #include <gmock/gmock.h> |
| 36 | #include <gtest/gtest.h> |
| 37 | #include <gtest/gtest-spi.h> |
| 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: |
| 43 | MOCK_METHOD0(DoThis, void()); |
| 44 | }; |
| 45 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 46 | namespace testing { |
| 47 | namespace gmock_nice_strict_test { |
| 48 | |
| 49 | using testing::internal::string; |
| 50 | using testing::GMOCK_FLAG(verbose); |
| 51 | using testing::HasSubstr; |
| 52 | using testing::NiceMock; |
| 53 | using testing::StrictMock; |
| 54 | |
| 55 | // Defines some mock classes needed by the tests. |
| 56 | |
| 57 | class Foo { |
| 58 | public: |
| 59 | virtual ~Foo() {} |
| 60 | |
| 61 | virtual void DoThis() = 0; |
| 62 | virtual int DoThat(bool flag) = 0; |
| 63 | }; |
| 64 | |
| 65 | class MockFoo : public Foo { |
| 66 | public: |
| 67 | void Delete() { delete this; } |
| 68 | |
| 69 | MOCK_METHOD0(DoThis, void()); |
| 70 | MOCK_METHOD1(DoThat, int(bool flag)); |
| 71 | }; |
| 72 | |
| 73 | class MockBar { |
| 74 | public: |
| 75 | explicit MockBar(const string& s) : str_(s) {} |
| 76 | |
| 77 | MockBar(char a1, char a2, string a3, string a4, int a5, int a6, |
| 78 | const string& a7, const string& a8, bool a9, bool a10) { |
| 79 | str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
| 80 | static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
| 81 | } |
| 82 | |
| 83 | virtual ~MockBar() {} |
| 84 | |
| 85 | const string& str() const { return str_; } |
| 86 | |
| 87 | MOCK_METHOD0(This, int()); |
| 88 | MOCK_METHOD2(That, string(int, bool)); |
| 89 | |
| 90 | private: |
| 91 | string str_; |
| 92 | }; |
| 93 | |
| 94 | // TODO(wan@google.com): find a way to re-enable these tests. |
| 95 | #if 0 |
| 96 | |
| 97 | // Tests that a nice mock generates no warning for uninteresting calls. |
| 98 | TEST(NiceMockTest, NoWarningForUninterestingCall) { |
| 99 | NiceMock<MockFoo> nice_foo; |
| 100 | |
| 101 | CaptureTestStdout(); |
| 102 | nice_foo.DoThis(); |
| 103 | nice_foo.DoThat(true); |
| 104 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 105 | } |
| 106 | |
| 107 | // Tests that a nice mock generates no warning for uninteresting calls |
| 108 | // that delete the mock object. |
| 109 | TEST(NiceMockTest, NoWarningForUninterestingCallAfterDeath) { |
| 110 | NiceMock<MockFoo>* const nice_foo = new NiceMock<MockFoo>; |
| 111 | |
| 112 | ON_CALL(*nice_foo, DoThis()) |
| 113 | .WillByDefault(Invoke(nice_foo, &MockFoo::Delete)); |
| 114 | |
| 115 | CaptureTestStdout(); |
| 116 | nice_foo->DoThis(); |
| 117 | EXPECT_EQ("", GetCapturedTestStdout()); |
| 118 | } |
| 119 | |
| 120 | // Tests that a nice mock generates informational logs for |
| 121 | // uninteresting calls. |
| 122 | TEST(NiceMockTest, InfoForUninterestingCall) { |
| 123 | NiceMock<MockFoo> nice_foo; |
| 124 | |
| 125 | GMOCK_FLAG(verbose) = "info"; |
| 126 | CaptureTestStdout(); |
| 127 | nice_foo.DoThis(); |
| 128 | EXPECT_THAT(GetCapturedTestStdout(), |
| 129 | HasSubstr("Uninteresting mock function call")); |
| 130 | |
| 131 | CaptureTestStdout(); |
| 132 | nice_foo.DoThat(true); |
| 133 | EXPECT_THAT(GetCapturedTestStdout(), |
| 134 | HasSubstr("Uninteresting mock function call")); |
| 135 | } |
| 136 | |
| 137 | #endif // 0 |
| 138 | |
| 139 | // Tests that a nice mock allows expected calls. |
| 140 | TEST(NiceMockTest, AllowsExpectedCall) { |
| 141 | NiceMock<MockFoo> nice_foo; |
| 142 | |
| 143 | EXPECT_CALL(nice_foo, DoThis()); |
| 144 | nice_foo.DoThis(); |
| 145 | } |
| 146 | |
| 147 | // Tests that an unexpected call on a nice mock fails. |
| 148 | TEST(NiceMockTest, UnexpectedCallFails) { |
| 149 | NiceMock<MockFoo> nice_foo; |
| 150 | |
| 151 | EXPECT_CALL(nice_foo, DoThis()).Times(0); |
| 152 | EXPECT_NONFATAL_FAILURE(nice_foo.DoThis(), "called more times than expected"); |
| 153 | } |
| 154 | |
| 155 | // Tests that NiceMock works with a mock class that has a non-default |
| 156 | // constructor. |
| 157 | TEST(NiceMockTest, NonDefaultConstructor) { |
| 158 | NiceMock<MockBar> nice_bar("hi"); |
| 159 | EXPECT_EQ("hi", nice_bar.str()); |
| 160 | |
| 161 | nice_bar.This(); |
| 162 | nice_bar.That(5, true); |
| 163 | } |
| 164 | |
| 165 | // Tests that NiceMock works with a mock class that has a 10-ary |
| 166 | // non-default constructor. |
| 167 | TEST(NiceMockTest, NonDefaultConstructor10) { |
| 168 | NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
| 169 | "g", "h", true, false); |
| 170 | EXPECT_EQ("abcdefghTF", nice_bar.str()); |
| 171 | |
| 172 | nice_bar.This(); |
| 173 | nice_bar.That(5, true); |
| 174 | } |
| 175 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 176 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 177 | // Tests that NiceMock<Mock> compiles where Mock is a user-defined |
| 178 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 179 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 180 | // NiceMock to be looked up in the wrong context, and this test |
| 181 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 182 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 183 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 184 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 185 | TEST(NiceMockTest, AcceptsClassNamedMock) { |
| 186 | NiceMock< ::Mock> nice; |
| 187 | EXPECT_CALL(nice, DoThis()); |
| 188 | nice.DoThis(); |
| 189 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 190 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 191 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 192 | // Tests that a strict mock allows expected calls. |
| 193 | TEST(StrictMockTest, AllowsExpectedCall) { |
| 194 | StrictMock<MockFoo> strict_foo; |
| 195 | |
| 196 | EXPECT_CALL(strict_foo, DoThis()); |
| 197 | strict_foo.DoThis(); |
| 198 | } |
| 199 | |
| 200 | // Tests that an unexpected call on a strict mock fails. |
| 201 | TEST(StrictMockTest, UnexpectedCallFails) { |
| 202 | StrictMock<MockFoo> strict_foo; |
| 203 | |
| 204 | EXPECT_CALL(strict_foo, DoThis()).Times(0); |
| 205 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 206 | "called more times than expected"); |
| 207 | } |
| 208 | |
| 209 | // Tests that an uninteresting call on a strict mock fails. |
| 210 | TEST(StrictMockTest, UninterestingCallFails) { |
| 211 | StrictMock<MockFoo> strict_foo; |
| 212 | |
| 213 | EXPECT_NONFATAL_FAILURE(strict_foo.DoThis(), |
| 214 | "Uninteresting mock function call"); |
| 215 | } |
| 216 | |
| 217 | // Tests that an uninteresting call on a strict mock fails, even if |
| 218 | // the call deletes the mock object. |
| 219 | TEST(StrictMockTest, UninterestingCallFailsAfterDeath) { |
| 220 | StrictMock<MockFoo>* const strict_foo = new StrictMock<MockFoo>; |
| 221 | |
| 222 | ON_CALL(*strict_foo, DoThis()) |
| 223 | .WillByDefault(Invoke(strict_foo, &MockFoo::Delete)); |
| 224 | |
| 225 | EXPECT_NONFATAL_FAILURE(strict_foo->DoThis(), |
| 226 | "Uninteresting mock function call"); |
| 227 | } |
| 228 | |
| 229 | // Tests that StrictMock works with a mock class that has a |
| 230 | // non-default constructor. |
| 231 | TEST(StrictMockTest, NonDefaultConstructor) { |
| 232 | StrictMock<MockBar> strict_bar("hi"); |
| 233 | EXPECT_EQ("hi", strict_bar.str()); |
| 234 | |
| 235 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 236 | "Uninteresting mock function call"); |
| 237 | } |
| 238 | |
| 239 | // Tests that StrictMock works with a mock class that has a 10-ary |
| 240 | // non-default constructor. |
| 241 | TEST(StrictMockTest, NonDefaultConstructor10) { |
| 242 | StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
| 243 | "g", "h", true, false); |
| 244 | EXPECT_EQ("abcdefghTF", strict_bar.str()); |
| 245 | |
| 246 | EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
| 247 | "Uninteresting mock function call"); |
| 248 | } |
| 249 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 250 | #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 251 | // Tests that StrictMock<Mock> compiles where Mock is a user-defined |
| 252 | // class (as opposed to ::testing::Mock). We had to workaround an |
| 253 | // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
| 254 | // StrictMock to be looked up in the wrong context, and this test |
| 255 | // ensures that our fix works. |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 256 | // |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 257 | // We have to skip this test on Symbian and Windows Mobile, as it |
| 258 | // causes the program to crash there, for reasons unclear to us yet. |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 259 | TEST(StrictMockTest, AcceptsClassNamedMock) { |
zhanyong.wan | 93244dc | 2009-09-17 19:11:00 +0000 | [diff] [blame] | 260 | StrictMock< ::Mock> strict; |
| 261 | EXPECT_CALL(strict, DoThis()); |
| 262 | strict.DoThis(); |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 263 | } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame^] | 264 | #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 4bd79e4 | 2009-09-16 17:38:08 +0000 | [diff] [blame] | 265 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 266 | } // namespace gmock_nice_strict_test |
| 267 | } // namespace testing |