gmock_output_test_.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2008, 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. // Tests Google Mock's output in various scenarios. This ensures that
  30. // Google Mock's messages are readable and useful.
  31. #include <stdio.h>
  32. #include <string>
  33. #include "gmock/gmock.h"
  34. #include "gtest/gtest.h"
  35. // Silence C4100 (unreferenced formal parameter)
  36. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
  37. using testing::_;
  38. using testing::AnyNumber;
  39. using testing::Ge;
  40. using testing::InSequence;
  41. using testing::NaggyMock;
  42. using testing::Ref;
  43. using testing::Return;
  44. using testing::Sequence;
  45. using testing::Value;
  46. class MockFoo {
  47. public:
  48. MockFoo() = default;
  49. MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
  50. MOCK_METHOD2(Bar2, bool(int x, int y));
  51. MOCK_METHOD2(Bar3, void(int x, int y));
  52. private:
  53. MockFoo(const MockFoo&) = delete;
  54. MockFoo& operator=(const MockFoo&) = delete;
  55. };
  56. class GMockOutputTest : public testing::Test {
  57. protected:
  58. NaggyMock<MockFoo> foo_;
  59. };
  60. TEST_F(GMockOutputTest, ExpectedCall) {
  61. GMOCK_FLAG_SET(verbose, "info");
  62. EXPECT_CALL(foo_, Bar2(0, _));
  63. foo_.Bar2(0, 0); // Expected call
  64. GMOCK_FLAG_SET(verbose, "warning");
  65. }
  66. TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
  67. GMOCK_FLAG_SET(verbose, "info");
  68. EXPECT_CALL(foo_, Bar3(0, _));
  69. foo_.Bar3(0, 0); // Expected call
  70. GMOCK_FLAG_SET(verbose, "warning");
  71. }
  72. TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
  73. EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
  74. foo_.Bar2(2, 2);
  75. foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
  76. }
  77. TEST_F(GMockOutputTest, UnexpectedCall) {
  78. EXPECT_CALL(foo_, Bar2(0, _));
  79. foo_.Bar2(1, 0); // Unexpected call
  80. foo_.Bar2(0, 0); // Expected call
  81. }
  82. TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
  83. EXPECT_CALL(foo_, Bar3(0, _));
  84. foo_.Bar3(1, 0); // Unexpected call
  85. foo_.Bar3(0, 0); // Expected call
  86. }
  87. TEST_F(GMockOutputTest, ExcessiveCall) {
  88. EXPECT_CALL(foo_, Bar2(0, _));
  89. foo_.Bar2(0, 0); // Expected call
  90. foo_.Bar2(0, 1); // Excessive call
  91. }
  92. TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
  93. EXPECT_CALL(foo_, Bar3(0, _));
  94. foo_.Bar3(0, 0); // Expected call
  95. foo_.Bar3(0, 1); // Excessive call
  96. }
  97. TEST_F(GMockOutputTest, UninterestingCall) {
  98. foo_.Bar2(0, 1); // Uninteresting call
  99. }
  100. TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
  101. foo_.Bar3(0, 1); // Uninteresting call
  102. }
  103. TEST_F(GMockOutputTest, RetiredExpectation) {
  104. EXPECT_CALL(foo_, Bar2(_, _)).RetiresOnSaturation();
  105. EXPECT_CALL(foo_, Bar2(0, 0));
  106. foo_.Bar2(1, 1);
  107. foo_.Bar2(1, 1); // Matches a retired expectation
  108. foo_.Bar2(0, 0);
  109. }
  110. TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
  111. {
  112. InSequence s;
  113. EXPECT_CALL(foo_, Bar(_, 0, _));
  114. EXPECT_CALL(foo_, Bar2(0, 0));
  115. EXPECT_CALL(foo_, Bar2(1, _));
  116. }
  117. foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
  118. foo_.Bar("Hi", 0, 0);
  119. foo_.Bar2(0, 0);
  120. foo_.Bar2(1, 0);
  121. }
  122. TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
  123. Sequence s1, s2;
  124. EXPECT_CALL(foo_, Bar(_, 0, _)).InSequence(s1);
  125. EXPECT_CALL(foo_, Bar2(0, 0)).InSequence(s2);
  126. EXPECT_CALL(foo_, Bar2(1, _)).InSequence(s1, s2);
  127. foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
  128. foo_.Bar("Hi", 0, 0);
  129. foo_.Bar2(0, 0);
  130. foo_.Bar2(1, 0);
  131. }
  132. TEST_F(GMockOutputTest, UnsatisfiedWith) {
  133. EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
  134. }
  135. TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
  136. EXPECT_CALL(foo_, Bar(_, _, _));
  137. EXPECT_CALL(foo_, Bar2(0, _)).Times(2);
  138. foo_.Bar2(0, 1);
  139. }
  140. TEST_F(GMockOutputTest, MismatchArguments) {
  141. const std::string s = "Hi";
  142. EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
  143. foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
  144. foo_.Bar(s, 0, 0);
  145. }
  146. TEST_F(GMockOutputTest, MismatchWith) {
  147. EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
  148. foo_.Bar2(2, 3); // Mismatch With()
  149. foo_.Bar2(2, 1);
  150. }
  151. TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
  152. EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1))).With(Ge());
  153. foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
  154. foo_.Bar2(2, 1);
  155. }
  156. TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
  157. ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
  158. ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
  159. EXPECT_CALL(foo_, Bar2(2, 2));
  160. foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
  161. foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
  162. foo_.Bar2(2, 2); // Expected call.
  163. }
  164. TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
  165. ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
  166. ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
  167. EXPECT_CALL(foo_, Bar2(2, 2));
  168. EXPECT_CALL(foo_, Bar2(1, 1));
  169. foo_.Bar2(2, 2); // Expected call.
  170. foo_.Bar2(2, 2); // Excessive call, takes default action #1.
  171. foo_.Bar2(1, 1); // Expected call.
  172. foo_.Bar2(1, 1); // Excessive call, takes default action #2.
  173. }
  174. TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
  175. ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
  176. ON_CALL(foo_, Bar2(1, _)).WillByDefault(Return(false)); // Default action #2
  177. foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
  178. foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
  179. }
  180. TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
  181. ON_CALL(foo_, Bar2(_, _)).WillByDefault(Return(true)); // Default action #1
  182. EXPECT_CALL(foo_, Bar2(_, _)).Times(2).WillOnce(Return(false));
  183. foo_.Bar2(2, 2);
  184. foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
  185. }
  186. TEST_F(GMockOutputTest, CatchesLeakedMocks) {
  187. MockFoo* foo1 = new MockFoo;
  188. MockFoo* foo2 = new MockFoo;
  189. // Invokes ON_CALL on foo1.
  190. ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
  191. // Invokes EXPECT_CALL on foo2.
  192. EXPECT_CALL(*foo2, Bar2(_, _));
  193. EXPECT_CALL(*foo2, Bar2(1, _));
  194. EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
  195. foo2->Bar2(2, 1);
  196. foo2->Bar2(1, 1);
  197. // Both foo1 and foo2 are deliberately leaked.
  198. }
  199. MATCHER_P2(IsPair, first, second, "") {
  200. return Value(arg.first, first) && Value(arg.second, second);
  201. }
  202. TEST_F(GMockOutputTest, PrintsMatcher) {
  203. const testing::Matcher<int> m1 = Ge(48);
  204. EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
  205. }
  206. void TestCatchesLeakedMocksInAdHocTests() {
  207. MockFoo* foo = new MockFoo;
  208. // Invokes EXPECT_CALL on foo.
  209. EXPECT_CALL(*foo, Bar2(_, _));
  210. foo->Bar2(2, 1);
  211. // foo is deliberately leaked.
  212. }
  213. int main(int argc, char** argv) {
  214. testing::InitGoogleMock(&argc, argv);
  215. // Ensures that the tests pass no matter what value of
  216. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
  217. GMOCK_FLAG_SET(catch_leaked_mocks, true);
  218. GMOCK_FLAG_SET(verbose, "warning");
  219. TestCatchesLeakedMocksInAdHocTests();
  220. return RUN_ALL_TESTS();
  221. }
  222. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100