gmock-cardinalities_test.cc 12 KB

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