OLD | NEW |
(Empty) | |
| 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 // Tests Google Mock's output in various scenarios. This ensures that |
| 33 // Google Mock's messages are readable and useful. |
| 34 |
| 35 #include <gmock/gmock.h> |
| 36 |
| 37 #include <stdio.h> |
| 38 #include <string> |
| 39 |
| 40 #include <gtest/gtest.h> |
| 41 |
| 42 using testing::_; |
| 43 using testing::AnyNumber; |
| 44 using testing::Ge; |
| 45 using testing::InSequence; |
| 46 using testing::Ref; |
| 47 using testing::Return; |
| 48 using testing::Sequence; |
| 49 |
| 50 class MockFoo { |
| 51 public: |
| 52 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x)); |
| 53 MOCK_METHOD2(Bar2, bool(int x, int y)); |
| 54 MOCK_METHOD2(Bar3, void(int x, int y)); |
| 55 }; |
| 56 |
| 57 class GMockOutputTest : public testing::Test { |
| 58 protected: |
| 59 MockFoo foo_; |
| 60 }; |
| 61 |
| 62 TEST_F(GMockOutputTest, ExpectedCall) { |
| 63 testing::GMOCK_FLAG(verbose) = "info"; |
| 64 |
| 65 EXPECT_CALL(foo_, Bar2(0, _)); |
| 66 foo_.Bar2(0, 0); // Expected call |
| 67 |
| 68 testing::GMOCK_FLAG(verbose) = "warning"; |
| 69 } |
| 70 |
| 71 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) { |
| 72 testing::GMOCK_FLAG(verbose) = "info"; |
| 73 |
| 74 EXPECT_CALL(foo_, Bar3(0, _)); |
| 75 foo_.Bar3(0, 0); // Expected call |
| 76 |
| 77 testing::GMOCK_FLAG(verbose) = "warning"; |
| 78 } |
| 79 |
| 80 TEST_F(GMockOutputTest, ExplicitActionsRunOut) { |
| 81 EXPECT_CALL(foo_, Bar2(_, _)) |
| 82 .Times(2) |
| 83 .WillOnce(Return(false)); |
| 84 foo_.Bar2(2, 2); |
| 85 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. |
| 86 } |
| 87 |
| 88 TEST_F(GMockOutputTest, UnexpectedCall) { |
| 89 EXPECT_CALL(foo_, Bar2(0, _)); |
| 90 |
| 91 foo_.Bar2(1, 0); // Unexpected call |
| 92 foo_.Bar2(0, 0); // Expected call |
| 93 } |
| 94 |
| 95 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) { |
| 96 EXPECT_CALL(foo_, Bar3(0, _)); |
| 97 |
| 98 foo_.Bar3(1, 0); // Unexpected call |
| 99 foo_.Bar3(0, 0); // Expected call |
| 100 } |
| 101 |
| 102 TEST_F(GMockOutputTest, ExcessiveCall) { |
| 103 EXPECT_CALL(foo_, Bar2(0, _)); |
| 104 |
| 105 foo_.Bar2(0, 0); // Expected call |
| 106 foo_.Bar2(0, 1); // Excessive call |
| 107 } |
| 108 |
| 109 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) { |
| 110 EXPECT_CALL(foo_, Bar3(0, _)); |
| 111 |
| 112 foo_.Bar3(0, 0); // Expected call |
| 113 foo_.Bar3(0, 1); // Excessive call |
| 114 } |
| 115 |
| 116 TEST_F(GMockOutputTest, UninterestingCall) { |
| 117 foo_.Bar2(0, 1); // Uninteresting call |
| 118 } |
| 119 |
| 120 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) { |
| 121 foo_.Bar3(0, 1); // Uninteresting call |
| 122 } |
| 123 |
| 124 TEST_F(GMockOutputTest, RetiredExpectation) { |
| 125 EXPECT_CALL(foo_, Bar2(_, _)) |
| 126 .RetiresOnSaturation(); |
| 127 EXPECT_CALL(foo_, Bar2(0, 0)); |
| 128 |
| 129 foo_.Bar2(1, 1); |
| 130 foo_.Bar2(1, 1); // Matches a retired expectation |
| 131 foo_.Bar2(0, 0); |
| 132 } |
| 133 |
| 134 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) { |
| 135 { |
| 136 InSequence s; |
| 137 EXPECT_CALL(foo_, Bar(_, 0, _)); |
| 138 EXPECT_CALL(foo_, Bar2(0, 0)); |
| 139 EXPECT_CALL(foo_, Bar2(1, _)); |
| 140 } |
| 141 |
| 142 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite |
| 143 foo_.Bar("Hi", 0, 0); |
| 144 foo_.Bar2(0, 0); |
| 145 foo_.Bar2(1, 0); |
| 146 } |
| 147 |
| 148 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) { |
| 149 Sequence s1, s2; |
| 150 |
| 151 EXPECT_CALL(foo_, Bar(_, 0, _)) |
| 152 .InSequence(s1); |
| 153 EXPECT_CALL(foo_, Bar2(0, 0)) |
| 154 .InSequence(s2); |
| 155 EXPECT_CALL(foo_, Bar2(1, _)) |
| 156 .InSequence(s1, s2); |
| 157 |
| 158 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites |
| 159 foo_.Bar("Hi", 0, 0); |
| 160 foo_.Bar2(0, 0); |
| 161 foo_.Bar2(1, 0); |
| 162 } |
| 163 |
| 164 TEST_F(GMockOutputTest, UnsatisfiedExpectation) { |
| 165 EXPECT_CALL(foo_, Bar(_, _, _)); |
| 166 EXPECT_CALL(foo_, Bar2(0, _)) |
| 167 .Times(2); |
| 168 |
| 169 foo_.Bar2(0, 1); |
| 170 } |
| 171 |
| 172 TEST_F(GMockOutputTest, MismatchArguments) { |
| 173 const std::string s = "Hi"; |
| 174 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0))); |
| 175 |
| 176 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments |
| 177 foo_.Bar(s, 0, 0); |
| 178 } |
| 179 |
| 180 TEST_F(GMockOutputTest, MismatchWithArguments) { |
| 181 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) |
| 182 .WithArguments(Ge()); |
| 183 |
| 184 foo_.Bar2(2, 3); // Mismatch WithArguments() |
| 185 foo_.Bar2(2, 1); |
| 186 } |
| 187 |
| 188 TEST_F(GMockOutputTest, MismatchArgumentsAndWithArguments) { |
| 189 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))) |
| 190 .WithArguments(Ge()); |
| 191 |
| 192 foo_.Bar2(1, 3); // Mismatch arguments and mismatch WithArguments() |
| 193 foo_.Bar2(2, 1); |
| 194 } |
| 195 |
| 196 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) { |
| 197 ON_CALL(foo_, Bar2(_, _)) |
| 198 .WillByDefault(Return(true)); // Default action #1 |
| 199 ON_CALL(foo_, Bar2(1, _)) |
| 200 .WillByDefault(Return(false)); // Default action #2 |
| 201 |
| 202 EXPECT_CALL(foo_, Bar2(2, 2)); |
| 203 foo_.Bar2(1, 0); // Unexpected call, takes default action #2. |
| 204 foo_.Bar2(0, 0); // Unexpected call, takes default action #1. |
| 205 foo_.Bar2(2, 2); // Expected call. |
| 206 } |
| 207 |
| 208 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) { |
| 209 ON_CALL(foo_, Bar2(_, _)) |
| 210 .WillByDefault(Return(true)); // Default action #1 |
| 211 ON_CALL(foo_, Bar2(1, _)) |
| 212 .WillByDefault(Return(false)); // Default action #2 |
| 213 |
| 214 EXPECT_CALL(foo_, Bar2(2, 2)); |
| 215 EXPECT_CALL(foo_, Bar2(1, 1)); |
| 216 |
| 217 foo_.Bar2(2, 2); // Expected call. |
| 218 foo_.Bar2(2, 2); // Excessive call, takes default action #1. |
| 219 foo_.Bar2(1, 1); // Expected call. |
| 220 foo_.Bar2(1, 1); // Excessive call, takes default action #2. |
| 221 } |
| 222 |
| 223 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) { |
| 224 ON_CALL(foo_, Bar2(_, _)) |
| 225 .WillByDefault(Return(true)); // Default action #1 |
| 226 ON_CALL(foo_, Bar2(1, _)) |
| 227 .WillByDefault(Return(false)); // Default action #2 |
| 228 |
| 229 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1. |
| 230 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2. |
| 231 } |
| 232 |
| 233 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) { |
| 234 ON_CALL(foo_, Bar2(_, _)) |
| 235 .WillByDefault(Return(true)); // Default action #1 |
| 236 |
| 237 EXPECT_CALL(foo_, Bar2(_, _)) |
| 238 .Times(2) |
| 239 .WillOnce(Return(false)); |
| 240 foo_.Bar2(2, 2); |
| 241 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out. |
| 242 } |
| 243 |
| 244 TEST_F(GMockOutputTest, CatchesLeakedMocks) { |
| 245 MockFoo* foo1 = new MockFoo; |
| 246 MockFoo* foo2 = new MockFoo; |
| 247 |
| 248 // Invokes ON_CALL on foo1. |
| 249 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a')); |
| 250 |
| 251 // Invokes EXPECT_CALL on foo2. |
| 252 EXPECT_CALL(*foo2, Bar2(_, _)); |
| 253 EXPECT_CALL(*foo2, Bar2(1, _)); |
| 254 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber()); |
| 255 foo2->Bar2(2, 1); |
| 256 foo2->Bar2(1, 1); |
| 257 |
| 258 // Both foo1 and foo2 are deliberately leaked. |
| 259 } |
| 260 |
| 261 void TestCatchesLeakedMocksInAdHocTests() { |
| 262 MockFoo* foo = new MockFoo; |
| 263 |
| 264 // Invokes EXPECT_CALL on foo. |
| 265 EXPECT_CALL(*foo, Bar2(_, _)); |
| 266 foo->Bar2(2, 1); |
| 267 |
| 268 // foo is deliberately leaked. |
| 269 } |
| 270 |
| 271 int main(int argc, char **argv) { |
| 272 testing::InitGoogleMock(&argc, argv); |
| 273 |
| 274 // Ensures that the tests pass no matter what value of |
| 275 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. |
| 276 testing::GMOCK_FLAG(catch_leaked_mocks) = true; |
| 277 testing::GMOCK_FLAG(verbose) = "warning"; |
| 278 |
| 279 TestCatchesLeakedMocksInAdHocTests(); |
| 280 return RUN_ALL_TESTS(); |
| 281 } |
OLD | NEW |