gmock-cardinalities_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 <ostream>
  33. #include "gmock/gmock.h"
  34. #include "gtest/gtest-spi.h"
  35. #include "gtest/gtest.h"
  36. namespace {
  37. using std::stringstream;
  38. using testing::AnyNumber;
  39. using testing::AtLeast;
  40. using testing::AtMost;
  41. using testing::Between;
  42. using testing::Cardinality;
  43. using testing::CardinalityInterface;
  44. using testing::Exactly;
  45. using testing::IsSubstring;
  46. using testing::MakeCardinality;
  47. class MockFoo {
  48. public:
  49. MockFoo() = default;
  50. MOCK_METHOD0(Bar, int()); // NOLINT
  51. private:
  52. MockFoo(const MockFoo&) = delete;
  53. MockFoo& operator=(const MockFoo&) = delete;
  54. };
  55. // Tests that Cardinality objects can be default constructed.
  56. TEST(CardinalityTest, IsDefaultConstructable) { Cardinality c; }
  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", ss.str());
  104. }
  105. TEST(AnyNumberTest, HasCorrectBounds) {
  106. const Cardinality c = AnyNumber();
  107. EXPECT_EQ(0, c.ConservativeLowerBound());
  108. EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
  109. }
  110. // Tests AtLeast(n).
  111. TEST(AtLeastTest, OnNegativeNumber) {
  112. EXPECT_NONFATAL_FAILURE(
  113. { // NOLINT
  114. AtLeast(-1);
  115. },
  116. "The invocation lower bound must be >= 0");
  117. }
  118. TEST(AtLeastTest, OnZero) {
  119. const Cardinality c = AtLeast(0);
  120. EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
  121. EXPECT_FALSE(c.IsSaturatedByCallCount(0));
  122. EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
  123. EXPECT_FALSE(c.IsSaturatedByCallCount(1));
  124. stringstream ss;
  125. c.DescribeTo(&ss);
  126. EXPECT_PRED_FORMAT2(IsSubstring, "any number of times", 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", ss1.str());
  139. stringstream ss2;
  140. c.DescribeTo(&ss2);
  141. EXPECT_PRED_FORMAT2(IsSubstring, "at least twice", ss2.str());
  142. stringstream ss3;
  143. AtLeast(3).DescribeTo(&ss3);
  144. EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times", ss3.str());
  145. }
  146. TEST(AtLeastTest, HasCorrectBounds) {
  147. const Cardinality c = AtLeast(2);
  148. EXPECT_EQ(2, c.ConservativeLowerBound());
  149. EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
  150. }
  151. // Tests AtMost(n).
  152. TEST(AtMostTest, OnNegativeNumber) {
  153. EXPECT_NONFATAL_FAILURE(
  154. { // NOLINT
  155. AtMost(-1);
  156. },
  157. "The invocation upper bound must be >= 0");
  158. }
  159. TEST(AtMostTest, OnZero) {
  160. const Cardinality c = AtMost(0);
  161. EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
  162. EXPECT_TRUE(c.IsSaturatedByCallCount(0));
  163. EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
  164. EXPECT_TRUE(c.IsSaturatedByCallCount(1));
  165. stringstream ss;
  166. c.DescribeTo(&ss);
  167. EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());
  168. }
  169. TEST(AtMostTest, OnPositiveNumber) {
  170. const Cardinality c = AtMost(2);
  171. EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
  172. EXPECT_FALSE(c.IsSaturatedByCallCount(0));
  173. EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
  174. EXPECT_FALSE(c.IsSaturatedByCallCount(1));
  175. EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
  176. EXPECT_TRUE(c.IsSaturatedByCallCount(2));
  177. stringstream ss1;
  178. AtMost(1).DescribeTo(&ss1);
  179. EXPECT_PRED_FORMAT2(IsSubstring, "called at most once", ss1.str());
  180. stringstream ss2;
  181. c.DescribeTo(&ss2);
  182. EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", ss2.str());
  183. stringstream ss3;
  184. AtMost(3).DescribeTo(&ss3);
  185. EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times", ss3.str());
  186. }
  187. TEST(AtMostTest, HasCorrectBounds) {
  188. const Cardinality c = AtMost(2);
  189. EXPECT_EQ(0, c.ConservativeLowerBound());
  190. EXPECT_EQ(2, c.ConservativeUpperBound());
  191. }
  192. // Tests Between(m, n).
  193. TEST(BetweenTest, OnNegativeStart) {
  194. EXPECT_NONFATAL_FAILURE(
  195. { // NOLINT
  196. Between(-1, 2);
  197. },
  198. "The invocation lower bound must be >= 0, but is actually -1");
  199. }
  200. TEST(BetweenTest, OnNegativeEnd) {
  201. EXPECT_NONFATAL_FAILURE(
  202. { // NOLINT
  203. Between(1, -2);
  204. },
  205. "The invocation upper bound must be >= 0, but is actually -2");
  206. }
  207. TEST(BetweenTest, OnStartBiggerThanEnd) {
  208. EXPECT_NONFATAL_FAILURE(
  209. { // NOLINT
  210. Between(2, 1);
  211. },
  212. "The invocation upper bound (1) must be >= "
  213. "the invocation lower bound (2)");
  214. }
  215. TEST(BetweenTest, OnZeroStartAndZeroEnd) {
  216. const Cardinality c = Between(0, 0);
  217. EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
  218. EXPECT_TRUE(c.IsSaturatedByCallCount(0));
  219. EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
  220. EXPECT_TRUE(c.IsSaturatedByCallCount(1));
  221. stringstream ss;
  222. c.DescribeTo(&ss);
  223. EXPECT_PRED_FORMAT2(IsSubstring, "never called", 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", ss.str());
  236. }
  237. TEST(BetweenTest, OnSameStartAndEnd) {
  238. const Cardinality c = Between(3, 3);
  239. EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
  240. EXPECT_FALSE(c.IsSaturatedByCallCount(2));
  241. EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
  242. EXPECT_TRUE(c.IsSaturatedByCallCount(3));
  243. EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
  244. EXPECT_TRUE(c.IsSaturatedByCallCount(4));
  245. stringstream ss;
  246. c.DescribeTo(&ss);
  247. EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss.str());
  248. }
  249. TEST(BetweenTest, OnDifferentStartAndEnd) {
  250. const Cardinality c = Between(3, 5);
  251. EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
  252. EXPECT_FALSE(c.IsSaturatedByCallCount(2));
  253. EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
  254. EXPECT_FALSE(c.IsSaturatedByCallCount(3));
  255. EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
  256. EXPECT_TRUE(c.IsSaturatedByCallCount(5));
  257. EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
  258. EXPECT_TRUE(c.IsSaturatedByCallCount(6));
  259. stringstream ss;
  260. c.DescribeTo(&ss);
  261. EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times", ss.str());
  262. }
  263. TEST(BetweenTest, HasCorrectBounds) {
  264. const Cardinality c = Between(3, 5);
  265. EXPECT_EQ(3, c.ConservativeLowerBound());
  266. EXPECT_EQ(5, c.ConservativeUpperBound());
  267. }
  268. // Tests Exactly(n).
  269. TEST(ExactlyTest, OnNegativeNumber) {
  270. EXPECT_NONFATAL_FAILURE(
  271. { // NOLINT
  272. Exactly(-1);
  273. },
  274. "The invocation lower bound must be >= 0");
  275. }
  276. TEST(ExactlyTest, OnZero) {
  277. const Cardinality c = Exactly(0);
  278. EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
  279. EXPECT_TRUE(c.IsSaturatedByCallCount(0));
  280. EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
  281. EXPECT_TRUE(c.IsSaturatedByCallCount(1));
  282. stringstream ss;
  283. c.DescribeTo(&ss);
  284. EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());
  285. }
  286. TEST(ExactlyTest, OnPositiveNumber) {
  287. const Cardinality c = Exactly(2);
  288. EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
  289. EXPECT_FALSE(c.IsSaturatedByCallCount(0));
  290. EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
  291. EXPECT_TRUE(c.IsSaturatedByCallCount(2));
  292. stringstream ss1;
  293. Exactly(1).DescribeTo(&ss1);
  294. EXPECT_PRED_FORMAT2(IsSubstring, "called once", ss1.str());
  295. stringstream ss2;
  296. c.DescribeTo(&ss2);
  297. EXPECT_PRED_FORMAT2(IsSubstring, "called twice", ss2.str());
  298. stringstream ss3;
  299. Exactly(3).DescribeTo(&ss3);
  300. EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss3.str());
  301. }
  302. TEST(ExactlyTest, HasCorrectBounds) {
  303. const Cardinality c = Exactly(3);
  304. EXPECT_EQ(3, c.ConservativeLowerBound());
  305. EXPECT_EQ(3, c.ConservativeUpperBound());
  306. }
  307. // Tests that a user can make their own cardinality by implementing
  308. // CardinalityInterface and calling MakeCardinality().
  309. class EvenCardinality : public CardinalityInterface {
  310. public:
  311. // Returns true if and only if call_count calls will satisfy this
  312. // cardinality.
  313. bool IsSatisfiedByCallCount(int call_count) const override {
  314. return (call_count % 2 == 0);
  315. }
  316. // Returns true if and only if call_count calls will saturate this
  317. // cardinality.
  318. bool IsSaturatedByCallCount(int /* call_count */) const override {
  319. return false;
  320. }
  321. // Describes self to an ostream.
  322. void DescribeTo(::std::ostream* ss) const override {
  323. *ss << "called even number of times";
  324. }
  325. };
  326. TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
  327. const Cardinality c = MakeCardinality(new EvenCardinality);
  328. EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
  329. EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
  330. EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
  331. stringstream ss;
  332. c.DescribeTo(&ss);
  333. EXPECT_EQ("called even number of times", ss.str());
  334. }
  335. } // Unnamed namespace