gmock-cardinalities_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file tests the built-in cardinalities.
00035 
00036 #include "gmock/gmock.h"
00037 #include "gtest/gtest.h"
00038 #include "gtest/gtest-spi.h"
00039 
00040 namespace {
00041 
00042 using std::stringstream;
00043 using testing::AnyNumber;
00044 using testing::AtLeast;
00045 using testing::AtMost;
00046 using testing::Between;
00047 using testing::Cardinality;
00048 using testing::CardinalityInterface;
00049 using testing::Exactly;
00050 using testing::IsSubstring;
00051 using testing::MakeCardinality;
00052 
00053 class MockFoo {
00054  public:
00055   MockFoo() {}
00056   MOCK_METHOD0(Bar, int());  // NOLINT
00057 
00058  private:
00059   GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
00060 };
00061 
00062 // Tests that Cardinality objects can be default constructed.
00063 TEST(CardinalityTest, IsDefaultConstructable) {
00064   Cardinality c;
00065 }
00066 
00067 // Tests that Cardinality objects are copyable.
00068 TEST(CardinalityTest, IsCopyable) {
00069   // Tests the copy constructor.
00070   Cardinality c = Exactly(1);
00071   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
00072   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
00073   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
00074 
00075   // Tests the assignment operator.
00076   c = Exactly(2);
00077   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
00078   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00079   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
00080 }
00081 
00082 TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {
00083   const Cardinality c = AtMost(5);
00084   EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));
00085   EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));
00086   EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));
00087 }
00088 
00089 // Tests that Cardinality::DescribeActualCallCountTo() creates the
00090 // correct description.
00091 TEST(CardinalityTest, CanDescribeActualCallCount) {
00092   stringstream ss0;
00093   Cardinality::DescribeActualCallCountTo(0, &ss0);
00094   EXPECT_EQ("never called", ss0.str());
00095 
00096   stringstream ss1;
00097   Cardinality::DescribeActualCallCountTo(1, &ss1);
00098   EXPECT_EQ("called once", ss1.str());
00099 
00100   stringstream ss2;
00101   Cardinality::DescribeActualCallCountTo(2, &ss2);
00102   EXPECT_EQ("called twice", ss2.str());
00103 
00104   stringstream ss3;
00105   Cardinality::DescribeActualCallCountTo(3, &ss3);
00106   EXPECT_EQ("called 3 times", ss3.str());
00107 }
00108 
00109 // Tests AnyNumber()
00110 TEST(AnyNumber, Works) {
00111   const Cardinality c = AnyNumber();
00112   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00113   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00114 
00115   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
00116   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
00117 
00118   EXPECT_TRUE(c.IsSatisfiedByCallCount(9));
00119   EXPECT_FALSE(c.IsSaturatedByCallCount(9));
00120 
00121   stringstream ss;
00122   c.DescribeTo(&ss);
00123   EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times",
00124                       ss.str());
00125 }
00126 
00127 TEST(AnyNumberTest, HasCorrectBounds) {
00128   const Cardinality c = AnyNumber();
00129   EXPECT_EQ(0, c.ConservativeLowerBound());
00130   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
00131 }
00132 
00133 // Tests AtLeast(n).
00134 
00135 TEST(AtLeastTest, OnNegativeNumber) {
00136   EXPECT_NONFATAL_FAILURE({  // NOLINT
00137     AtLeast(-1);
00138   }, "The invocation lower bound must be >= 0");
00139 }
00140 
00141 TEST(AtLeastTest, OnZero) {
00142   const Cardinality c = AtLeast(0);
00143   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00144   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00145 
00146   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
00147   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
00148 
00149   stringstream ss;
00150   c.DescribeTo(&ss);
00151   EXPECT_PRED_FORMAT2(IsSubstring, "any number of times",
00152                       ss.str());
00153 }
00154 
00155 TEST(AtLeastTest, OnPositiveNumber) {
00156   const Cardinality c = AtLeast(2);
00157   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
00158   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00159 
00160   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
00161   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
00162 
00163   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00164   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
00165 
00166   stringstream ss1;
00167   AtLeast(1).DescribeTo(&ss1);
00168   EXPECT_PRED_FORMAT2(IsSubstring, "at least once",
00169                       ss1.str());
00170 
00171   stringstream ss2;
00172   c.DescribeTo(&ss2);
00173   EXPECT_PRED_FORMAT2(IsSubstring, "at least twice",
00174                       ss2.str());
00175 
00176   stringstream ss3;
00177   AtLeast(3).DescribeTo(&ss3);
00178   EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times",
00179                       ss3.str());
00180 }
00181 
00182 TEST(AtLeastTest, HasCorrectBounds) {
00183   const Cardinality c = AtLeast(2);
00184   EXPECT_EQ(2, c.ConservativeLowerBound());
00185   EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
00186 }
00187 
00188 // Tests AtMost(n).
00189 
00190 TEST(AtMostTest, OnNegativeNumber) {
00191   EXPECT_NONFATAL_FAILURE({  // NOLINT
00192     AtMost(-1);
00193   }, "The invocation upper bound must be >= 0");
00194 }
00195 
00196 TEST(AtMostTest, OnZero) {
00197   const Cardinality c = AtMost(0);
00198   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00199   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
00200 
00201   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
00202   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
00203 
00204   stringstream ss;
00205   c.DescribeTo(&ss);
00206   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
00207                       ss.str());
00208 }
00209 
00210 TEST(AtMostTest, OnPositiveNumber) {
00211   const Cardinality c = AtMost(2);
00212   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00213   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00214 
00215   EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
00216   EXPECT_FALSE(c.IsSaturatedByCallCount(1));
00217 
00218   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00219   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
00220 
00221   stringstream ss1;
00222   AtMost(1).DescribeTo(&ss1);
00223   EXPECT_PRED_FORMAT2(IsSubstring, "called at most once",
00224                       ss1.str());
00225 
00226   stringstream ss2;
00227   c.DescribeTo(&ss2);
00228   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
00229                       ss2.str());
00230 
00231   stringstream ss3;
00232   AtMost(3).DescribeTo(&ss3);
00233   EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times",
00234                       ss3.str());
00235 }
00236 
00237 TEST(AtMostTest, HasCorrectBounds) {
00238   const Cardinality c = AtMost(2);
00239   EXPECT_EQ(0, c.ConservativeLowerBound());
00240   EXPECT_EQ(2, c.ConservativeUpperBound());
00241 }
00242 
00243 // Tests Between(m, n).
00244 
00245 TEST(BetweenTest, OnNegativeStart) {
00246   EXPECT_NONFATAL_FAILURE({  // NOLINT
00247     Between(-1, 2);
00248   }, "The invocation lower bound must be >= 0, but is actually -1");
00249 }
00250 
00251 TEST(BetweenTest, OnNegativeEnd) {
00252   EXPECT_NONFATAL_FAILURE({  // NOLINT
00253     Between(1, -2);
00254   }, "The invocation upper bound must be >= 0, but is actually -2");
00255 }
00256 
00257 TEST(BetweenTest, OnStartBiggerThanEnd) {
00258   EXPECT_NONFATAL_FAILURE({  // NOLINT
00259     Between(2, 1);
00260   }, "The invocation upper bound (1) must be >= "
00261      "the invocation lower bound (2)");
00262 }
00263 
00264 TEST(BetweenTest, OnZeroStartAndZeroEnd) {
00265   const Cardinality c = Between(0, 0);
00266 
00267   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00268   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
00269 
00270   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
00271   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
00272 
00273   stringstream ss;
00274   c.DescribeTo(&ss);
00275   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
00276                       ss.str());
00277 }
00278 
00279 TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {
00280   const Cardinality c = Between(0, 2);
00281 
00282   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00283   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00284 
00285   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00286   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
00287 
00288   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
00289   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
00290 
00291   stringstream ss;
00292   c.DescribeTo(&ss);
00293   EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
00294                       ss.str());
00295 }
00296 
00297 TEST(BetweenTest, OnSameStartAndEnd) {
00298   const Cardinality c = Between(3, 3);
00299 
00300   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
00301   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
00302 
00303   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
00304   EXPECT_TRUE(c.IsSaturatedByCallCount(3));
00305 
00306   EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
00307   EXPECT_TRUE(c.IsSaturatedByCallCount(4));
00308 
00309   stringstream ss;
00310   c.DescribeTo(&ss);
00311   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
00312                       ss.str());
00313 }
00314 
00315 TEST(BetweenTest, OnDifferentStartAndEnd) {
00316   const Cardinality c = Between(3, 5);
00317 
00318   EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
00319   EXPECT_FALSE(c.IsSaturatedByCallCount(2));
00320 
00321   EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
00322   EXPECT_FALSE(c.IsSaturatedByCallCount(3));
00323 
00324   EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
00325   EXPECT_TRUE(c.IsSaturatedByCallCount(5));
00326 
00327   EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
00328   EXPECT_TRUE(c.IsSaturatedByCallCount(6));
00329 
00330   stringstream ss;
00331   c.DescribeTo(&ss);
00332   EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times",
00333                       ss.str());
00334 }
00335 
00336 TEST(BetweenTest, HasCorrectBounds) {
00337   const Cardinality c = Between(3, 5);
00338   EXPECT_EQ(3, c.ConservativeLowerBound());
00339   EXPECT_EQ(5, c.ConservativeUpperBound());
00340 }
00341 
00342 // Tests Exactly(n).
00343 
00344 TEST(ExactlyTest, OnNegativeNumber) {
00345   EXPECT_NONFATAL_FAILURE({  // NOLINT
00346     Exactly(-1);
00347   }, "The invocation lower bound must be >= 0");
00348 }
00349 
00350 TEST(ExactlyTest, OnZero) {
00351   const Cardinality c = Exactly(0);
00352   EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
00353   EXPECT_TRUE(c.IsSaturatedByCallCount(0));
00354 
00355   EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
00356   EXPECT_TRUE(c.IsSaturatedByCallCount(1));
00357 
00358   stringstream ss;
00359   c.DescribeTo(&ss);
00360   EXPECT_PRED_FORMAT2(IsSubstring, "never called",
00361                       ss.str());
00362 }
00363 
00364 TEST(ExactlyTest, OnPositiveNumber) {
00365   const Cardinality c = Exactly(2);
00366   EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
00367   EXPECT_FALSE(c.IsSaturatedByCallCount(0));
00368 
00369   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00370   EXPECT_TRUE(c.IsSaturatedByCallCount(2));
00371 
00372   stringstream ss1;
00373   Exactly(1).DescribeTo(&ss1);
00374   EXPECT_PRED_FORMAT2(IsSubstring, "called once",
00375                       ss1.str());
00376 
00377   stringstream ss2;
00378   c.DescribeTo(&ss2);
00379   EXPECT_PRED_FORMAT2(IsSubstring, "called twice",
00380                       ss2.str());
00381 
00382   stringstream ss3;
00383   Exactly(3).DescribeTo(&ss3);
00384   EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
00385                       ss3.str());
00386 }
00387 
00388 TEST(ExactlyTest, HasCorrectBounds) {
00389   const Cardinality c = Exactly(3);
00390   EXPECT_EQ(3, c.ConservativeLowerBound());
00391   EXPECT_EQ(3, c.ConservativeUpperBound());
00392 }
00393 
00394 // Tests that a user can make his own cardinality by implementing
00395 // CardinalityInterface and calling MakeCardinality().
00396 
00397 class EvenCardinality : public CardinalityInterface {
00398  public:
00399   // Returns true iff call_count calls will satisfy this cardinality.
00400   virtual bool IsSatisfiedByCallCount(int call_count) const {
00401     return (call_count % 2 == 0);
00402   }
00403 
00404   // Returns true iff call_count calls will saturate this cardinality.
00405   virtual bool IsSaturatedByCallCount(int /* call_count */) const {
00406     return false;
00407   }
00408 
00409   // Describes self to an ostream.
00410   virtual void DescribeTo(::std::ostream* ss) const {
00411     *ss << "called even number of times";
00412   }
00413 };
00414 
00415 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
00416   const Cardinality c = MakeCardinality(new EvenCardinality);
00417 
00418   EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
00419   EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
00420 
00421   EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
00422 
00423   stringstream ss;
00424   c.DescribeTo(&ss);
00425   EXPECT_EQ("called even number of times", ss.str());
00426 }
00427 
00428 }  // Unnamed namespace


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40