gmock-cardinalities_test.cc 12 KB

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