OLD | NEW |
(Empty) | |
| 1 // Copyright 2007, 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 // Google Mock - a framework for writing C++ mock classes. |
| 33 // |
| 34 // This file tests the built-in cardinalities. |
| 35 |
| 36 #include <gmock/gmock.h> |
| 37 #include <gtest/gtest.h> |
| 38 #include <gtest/gtest-spi.h> |
| 39 |
| 40 namespace { |
| 41 |
| 42 using std::stringstream; |
| 43 using testing::AnyNumber; |
| 44 using testing::AtLeast; |
| 45 using testing::AtMost; |
| 46 using testing::Between; |
| 47 using testing::Cardinality; |
| 48 using testing::CardinalityInterface; |
| 49 using testing::Exactly; |
| 50 using testing::IsSubstring; |
| 51 using testing::MakeCardinality; |
| 52 |
| 53 class MockFoo { |
| 54 public: |
| 55 MOCK_METHOD0(Bar, int()); // NOLINT |
| 56 }; |
| 57 |
| 58 // Tests that Cardinality objects can be default constructed. |
| 59 TEST(CardinalityTest, IsDefaultConstructable) { |
| 60 Cardinality c; |
| 61 } |
| 62 |
| 63 // Tests that Cardinality objects are copyable. |
| 64 TEST(CardinalityTest, IsCopyable) { |
| 65 // Tests the copy constructor. |
| 66 Cardinality c = Exactly(1); |
| 67 EXPECT_FALSE(c.IsSatisfiedByCallCount(0)); |
| 68 EXPECT_TRUE(c.IsSatisfiedByCallCount(1)); |
| 69 EXPECT_TRUE(c.IsSaturatedByCallCount(1)); |
| 70 |
| 71 // Tests the assignment operator. |
| 72 c = Exactly(2); |
| 73 EXPECT_FALSE(c.IsSatisfiedByCallCount(1)); |
| 74 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 75 EXPECT_TRUE(c.IsSaturatedByCallCount(2)); |
| 76 } |
| 77 |
| 78 TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) { |
| 79 const Cardinality c = AtMost(5); |
| 80 EXPECT_FALSE(c.IsOverSaturatedByCallCount(4)); |
| 81 EXPECT_FALSE(c.IsOverSaturatedByCallCount(5)); |
| 82 EXPECT_TRUE(c.IsOverSaturatedByCallCount(6)); |
| 83 } |
| 84 |
| 85 // Tests that Cardinality::DescribeActualCallCountTo() creates the |
| 86 // correct description. |
| 87 TEST(CardinalityTest, CanDescribeActualCallCount) { |
| 88 stringstream ss0; |
| 89 Cardinality::DescribeActualCallCountTo(0, &ss0); |
| 90 EXPECT_EQ("never called", ss0.str()); |
| 91 |
| 92 stringstream ss1; |
| 93 Cardinality::DescribeActualCallCountTo(1, &ss1); |
| 94 EXPECT_EQ("called once", ss1.str()); |
| 95 |
| 96 stringstream ss2; |
| 97 Cardinality::DescribeActualCallCountTo(2, &ss2); |
| 98 EXPECT_EQ("called twice", ss2.str()); |
| 99 |
| 100 stringstream ss3; |
| 101 Cardinality::DescribeActualCallCountTo(3, &ss3); |
| 102 EXPECT_EQ("called 3 times", ss3.str()); |
| 103 } |
| 104 |
| 105 // Tests AnyNumber() |
| 106 TEST(AnyNumber, Works) { |
| 107 const Cardinality c = AnyNumber(); |
| 108 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 109 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 110 |
| 111 EXPECT_TRUE(c.IsSatisfiedByCallCount(1)); |
| 112 EXPECT_FALSE(c.IsSaturatedByCallCount(1)); |
| 113 |
| 114 EXPECT_TRUE(c.IsSatisfiedByCallCount(9)); |
| 115 EXPECT_FALSE(c.IsSaturatedByCallCount(9)); |
| 116 |
| 117 stringstream ss; |
| 118 c.DescribeTo(&ss); |
| 119 EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times", |
| 120 ss.str()); |
| 121 } |
| 122 |
| 123 TEST(AnyNumberTest, HasCorrectBounds) { |
| 124 const Cardinality c = AnyNumber(); |
| 125 EXPECT_EQ(0, c.ConservativeLowerBound()); |
| 126 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound()); |
| 127 } |
| 128 |
| 129 // Tests AtLeast(n). |
| 130 |
| 131 TEST(AtLeastTest, OnNegativeNumber) { |
| 132 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 133 AtLeast(-1); |
| 134 }, "The invocation lower bound must be >= 0"); |
| 135 } |
| 136 |
| 137 TEST(AtLeastTest, OnZero) { |
| 138 const Cardinality c = AtLeast(0); |
| 139 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 140 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 141 |
| 142 EXPECT_TRUE(c.IsSatisfiedByCallCount(1)); |
| 143 EXPECT_FALSE(c.IsSaturatedByCallCount(1)); |
| 144 |
| 145 stringstream ss; |
| 146 c.DescribeTo(&ss); |
| 147 EXPECT_PRED_FORMAT2(IsSubstring, "any number of times", |
| 148 ss.str()); |
| 149 } |
| 150 |
| 151 TEST(AtLeastTest, OnPositiveNumber) { |
| 152 const Cardinality c = AtLeast(2); |
| 153 EXPECT_FALSE(c.IsSatisfiedByCallCount(0)); |
| 154 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 155 |
| 156 EXPECT_FALSE(c.IsSatisfiedByCallCount(1)); |
| 157 EXPECT_FALSE(c.IsSaturatedByCallCount(1)); |
| 158 |
| 159 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 160 EXPECT_FALSE(c.IsSaturatedByCallCount(2)); |
| 161 |
| 162 stringstream ss1; |
| 163 AtLeast(1).DescribeTo(&ss1); |
| 164 EXPECT_PRED_FORMAT2(IsSubstring, "at least once", |
| 165 ss1.str()); |
| 166 |
| 167 stringstream ss2; |
| 168 c.DescribeTo(&ss2); |
| 169 EXPECT_PRED_FORMAT2(IsSubstring, "at least twice", |
| 170 ss2.str()); |
| 171 |
| 172 stringstream ss3; |
| 173 AtLeast(3).DescribeTo(&ss3); |
| 174 EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times", |
| 175 ss3.str()); |
| 176 } |
| 177 |
| 178 TEST(AtLeastTest, HasCorrectBounds) { |
| 179 const Cardinality c = AtLeast(2); |
| 180 EXPECT_EQ(2, c.ConservativeLowerBound()); |
| 181 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound()); |
| 182 } |
| 183 |
| 184 // Tests AtMost(n). |
| 185 |
| 186 TEST(AtMostTest, OnNegativeNumber) { |
| 187 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 188 AtMost(-1); |
| 189 }, "The invocation upper bound must be >= 0"); |
| 190 } |
| 191 |
| 192 TEST(AtMostTest, OnZero) { |
| 193 const Cardinality c = AtMost(0); |
| 194 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 195 EXPECT_TRUE(c.IsSaturatedByCallCount(0)); |
| 196 |
| 197 EXPECT_FALSE(c.IsSatisfiedByCallCount(1)); |
| 198 EXPECT_TRUE(c.IsSaturatedByCallCount(1)); |
| 199 |
| 200 stringstream ss; |
| 201 c.DescribeTo(&ss); |
| 202 EXPECT_PRED_FORMAT2(IsSubstring, "never called", |
| 203 ss.str()); |
| 204 } |
| 205 |
| 206 TEST(AtMostTest, OnPositiveNumber) { |
| 207 const Cardinality c = AtMost(2); |
| 208 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 209 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 210 |
| 211 EXPECT_TRUE(c.IsSatisfiedByCallCount(1)); |
| 212 EXPECT_FALSE(c.IsSaturatedByCallCount(1)); |
| 213 |
| 214 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 215 EXPECT_TRUE(c.IsSaturatedByCallCount(2)); |
| 216 |
| 217 stringstream ss1; |
| 218 AtMost(1).DescribeTo(&ss1); |
| 219 EXPECT_PRED_FORMAT2(IsSubstring, "called at most once", |
| 220 ss1.str()); |
| 221 |
| 222 stringstream ss2; |
| 223 c.DescribeTo(&ss2); |
| 224 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", |
| 225 ss2.str()); |
| 226 |
| 227 stringstream ss3; |
| 228 AtMost(3).DescribeTo(&ss3); |
| 229 EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times", |
| 230 ss3.str()); |
| 231 } |
| 232 |
| 233 TEST(AtMostTest, HasCorrectBounds) { |
| 234 const Cardinality c = AtMost(2); |
| 235 EXPECT_EQ(0, c.ConservativeLowerBound()); |
| 236 EXPECT_EQ(2, c.ConservativeUpperBound()); |
| 237 } |
| 238 |
| 239 // Tests Between(m, n). |
| 240 |
| 241 TEST(BetweenTest, OnNegativeStart) { |
| 242 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 243 Between(-1, 2); |
| 244 }, "The invocation lower bound must be >= 0, but is actually -1"); |
| 245 } |
| 246 |
| 247 TEST(BetweenTest, OnNegativeEnd) { |
| 248 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 249 Between(1, -2); |
| 250 }, "The invocation upper bound must be >= 0, but is actually -2"); |
| 251 } |
| 252 |
| 253 TEST(BetweenTest, OnStartBiggerThanEnd) { |
| 254 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 255 Between(2, 1); |
| 256 }, "The invocation upper bound (1) must be >= " |
| 257 "the invocation lower bound (2)"); |
| 258 } |
| 259 |
| 260 TEST(BetweenTest, OnZeroStartAndZeroEnd) { |
| 261 const Cardinality c = Between(0, 0); |
| 262 |
| 263 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 264 EXPECT_TRUE(c.IsSaturatedByCallCount(0)); |
| 265 |
| 266 EXPECT_FALSE(c.IsSatisfiedByCallCount(1)); |
| 267 EXPECT_TRUE(c.IsSaturatedByCallCount(1)); |
| 268 |
| 269 stringstream ss; |
| 270 c.DescribeTo(&ss); |
| 271 EXPECT_PRED_FORMAT2(IsSubstring, "never called", |
| 272 ss.str()); |
| 273 } |
| 274 |
| 275 TEST(BetweenTest, OnZeroStartAndNonZeroEnd) { |
| 276 const Cardinality c = Between(0, 2); |
| 277 |
| 278 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 279 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 280 |
| 281 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 282 EXPECT_TRUE(c.IsSaturatedByCallCount(2)); |
| 283 |
| 284 EXPECT_FALSE(c.IsSatisfiedByCallCount(4)); |
| 285 EXPECT_TRUE(c.IsSaturatedByCallCount(4)); |
| 286 |
| 287 stringstream ss; |
| 288 c.DescribeTo(&ss); |
| 289 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", |
| 290 ss.str()); |
| 291 } |
| 292 |
| 293 TEST(BetweenTest, OnSameStartAndEnd) { |
| 294 const Cardinality c = Between(3, 3); |
| 295 |
| 296 EXPECT_FALSE(c.IsSatisfiedByCallCount(2)); |
| 297 EXPECT_FALSE(c.IsSaturatedByCallCount(2)); |
| 298 |
| 299 EXPECT_TRUE(c.IsSatisfiedByCallCount(3)); |
| 300 EXPECT_TRUE(c.IsSaturatedByCallCount(3)); |
| 301 |
| 302 EXPECT_FALSE(c.IsSatisfiedByCallCount(4)); |
| 303 EXPECT_TRUE(c.IsSaturatedByCallCount(4)); |
| 304 |
| 305 stringstream ss; |
| 306 c.DescribeTo(&ss); |
| 307 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", |
| 308 ss.str()); |
| 309 } |
| 310 |
| 311 TEST(BetweenTest, OnDifferentStartAndEnd) { |
| 312 const Cardinality c = Between(3, 5); |
| 313 |
| 314 EXPECT_FALSE(c.IsSatisfiedByCallCount(2)); |
| 315 EXPECT_FALSE(c.IsSaturatedByCallCount(2)); |
| 316 |
| 317 EXPECT_TRUE(c.IsSatisfiedByCallCount(3)); |
| 318 EXPECT_FALSE(c.IsSaturatedByCallCount(3)); |
| 319 |
| 320 EXPECT_TRUE(c.IsSatisfiedByCallCount(5)); |
| 321 EXPECT_TRUE(c.IsSaturatedByCallCount(5)); |
| 322 |
| 323 EXPECT_FALSE(c.IsSatisfiedByCallCount(6)); |
| 324 EXPECT_TRUE(c.IsSaturatedByCallCount(6)); |
| 325 |
| 326 stringstream ss; |
| 327 c.DescribeTo(&ss); |
| 328 EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times", |
| 329 ss.str()); |
| 330 } |
| 331 |
| 332 TEST(BetweenTest, HasCorrectBounds) { |
| 333 const Cardinality c = Between(3, 5); |
| 334 EXPECT_EQ(3, c.ConservativeLowerBound()); |
| 335 EXPECT_EQ(5, c.ConservativeUpperBound()); |
| 336 } |
| 337 |
| 338 // Tests Exactly(n). |
| 339 |
| 340 TEST(ExactlyTest, OnNegativeNumber) { |
| 341 EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 342 Exactly(-1); |
| 343 }, "The invocation lower bound must be >= 0"); |
| 344 } |
| 345 |
| 346 TEST(ExactlyTest, OnZero) { |
| 347 const Cardinality c = Exactly(0); |
| 348 EXPECT_TRUE(c.IsSatisfiedByCallCount(0)); |
| 349 EXPECT_TRUE(c.IsSaturatedByCallCount(0)); |
| 350 |
| 351 EXPECT_FALSE(c.IsSatisfiedByCallCount(1)); |
| 352 EXPECT_TRUE(c.IsSaturatedByCallCount(1)); |
| 353 |
| 354 stringstream ss; |
| 355 c.DescribeTo(&ss); |
| 356 EXPECT_PRED_FORMAT2(IsSubstring, "never called", |
| 357 ss.str()); |
| 358 } |
| 359 |
| 360 TEST(ExactlyTest, OnPositiveNumber) { |
| 361 const Cardinality c = Exactly(2); |
| 362 EXPECT_FALSE(c.IsSatisfiedByCallCount(0)); |
| 363 EXPECT_FALSE(c.IsSaturatedByCallCount(0)); |
| 364 |
| 365 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 366 EXPECT_TRUE(c.IsSaturatedByCallCount(2)); |
| 367 |
| 368 stringstream ss1; |
| 369 Exactly(1).DescribeTo(&ss1); |
| 370 EXPECT_PRED_FORMAT2(IsSubstring, "called once", |
| 371 ss1.str()); |
| 372 |
| 373 stringstream ss2; |
| 374 c.DescribeTo(&ss2); |
| 375 EXPECT_PRED_FORMAT2(IsSubstring, "called twice", |
| 376 ss2.str()); |
| 377 |
| 378 stringstream ss3; |
| 379 Exactly(3).DescribeTo(&ss3); |
| 380 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", |
| 381 ss3.str()); |
| 382 } |
| 383 |
| 384 TEST(ExactlyTest, HasCorrectBounds) { |
| 385 const Cardinality c = Exactly(3); |
| 386 EXPECT_EQ(3, c.ConservativeLowerBound()); |
| 387 EXPECT_EQ(3, c.ConservativeUpperBound()); |
| 388 } |
| 389 |
| 390 // Tests that a user can make his own cardinality by implementing |
| 391 // CardinalityInterface and calling MakeCardinality(). |
| 392 |
| 393 class EvenCardinality : public CardinalityInterface { |
| 394 public: |
| 395 // Returns true iff call_count calls will satisfy this cardinality. |
| 396 virtual bool IsSatisfiedByCallCount(int call_count) const { |
| 397 return (call_count % 2 == 0); |
| 398 } |
| 399 |
| 400 // Returns true iff call_count calls will saturate this cardinality. |
| 401 virtual bool IsSaturatedByCallCount(int call_count) const { return false; } |
| 402 |
| 403 // Describes self to an ostream. |
| 404 virtual void DescribeTo(::std::ostream* ss) const { |
| 405 *ss << "called even number of times"; |
| 406 } |
| 407 }; |
| 408 |
| 409 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) { |
| 410 const Cardinality c = MakeCardinality(new EvenCardinality); |
| 411 |
| 412 EXPECT_TRUE(c.IsSatisfiedByCallCount(2)); |
| 413 EXPECT_FALSE(c.IsSatisfiedByCallCount(3)); |
| 414 |
| 415 EXPECT_FALSE(c.IsSaturatedByCallCount(10000)); |
| 416 |
| 417 stringstream ss; |
| 418 c.DescribeTo(&ss); |
| 419 EXPECT_EQ("called even number of times", ss.str()); |
| 420 } |
| 421 |
| 422 } // Unnamed namespace |
OLD | NEW |