gmock-cardinalities_test.cc 12 KB

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