You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

429 lines
12 KiB

3 weeks ago
  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 if and only if call_count calls will satisfy this
  317. // cardinality.
  318. bool IsSatisfiedByCallCount(int call_count) const override {
  319. return (call_count % 2 == 0);
  320. }
  321. // Returns true if and only if call_count calls will saturate this
  322. // cardinality.
  323. bool IsSaturatedByCallCount(int /* call_count */) const override {
  324. return false;
  325. }
  326. // Describes self to an ostream.
  327. void DescribeTo(::std::ostream* ss) const override {
  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