gmock_output_test_.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. //
  30. // Author: wan@google.com (Zhanyong Wan)
  31. // Tests Google Mock's output in various scenarios. This ensures that
  32. // Google Mock's messages are readable and useful.
  33. #include "gmock/gmock.h"
  34. #include <stdio.h>
  35. #include <string>
  36. #include "gtest/gtest.h"
  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. class MockFoo {
  46. public:
  47. MockFoo() {}
  48. MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
  49. MOCK_METHOD2(Bar2, bool(int x, int y));
  50. MOCK_METHOD2(Bar3, void(int x, int y));
  51. private:
  52. GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
  53. };
  54. class GMockOutputTest : public testing::Test {
  55. protected:
  56. NaggyMock<MockFoo> foo_;
  57. };
  58. TEST_F(GMockOutputTest, ExpectedCall) {
  59. testing::GMOCK_FLAG(verbose) = "info";
  60. EXPECT_CALL(foo_, Bar2(0, _));
  61. foo_.Bar2(0, 0); // Expected call
  62. testing::GMOCK_FLAG(verbose) = "warning";
  63. }
  64. TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
  65. testing::GMOCK_FLAG(verbose) = "info";
  66. EXPECT_CALL(foo_, Bar3(0, _));
  67. foo_.Bar3(0, 0); // Expected call
  68. testing::GMOCK_FLAG(verbose) = "warning";
  69. }
  70. TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
  71. EXPECT_CALL(foo_, Bar2(_, _))
  72. .Times(2)
  73. .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(_, _))
  105. .RetiresOnSaturation();
  106. EXPECT_CALL(foo_, Bar2(0, 0));
  107. foo_.Bar2(1, 1);
  108. foo_.Bar2(1, 1); // Matches a retired expectation
  109. foo_.Bar2(0, 0);
  110. }
  111. TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
  112. {
  113. InSequence s;
  114. EXPECT_CALL(foo_, Bar(_, 0, _));
  115. EXPECT_CALL(foo_, Bar2(0, 0));
  116. EXPECT_CALL(foo_, Bar2(1, _));
  117. }
  118. foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
  119. foo_.Bar("Hi", 0, 0);
  120. foo_.Bar2(0, 0);
  121. foo_.Bar2(1, 0);
  122. }
  123. TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
  124. Sequence s1, s2;
  125. EXPECT_CALL(foo_, Bar(_, 0, _))
  126. .InSequence(s1);
  127. EXPECT_CALL(foo_, Bar2(0, 0))
  128. .InSequence(s2);
  129. EXPECT_CALL(foo_, Bar2(1, _))
  130. .InSequence(s1, s2);
  131. foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
  132. foo_.Bar("Hi", 0, 0);
  133. foo_.Bar2(0, 0);
  134. foo_.Bar2(1, 0);
  135. }
  136. TEST_F(GMockOutputTest, UnsatisfiedWith) {
  137. EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
  138. }
  139. TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
  140. EXPECT_CALL(foo_, Bar(_, _, _));
  141. EXPECT_CALL(foo_, Bar2(0, _))
  142. .Times(2);
  143. foo_.Bar2(0, 1);
  144. }
  145. TEST_F(GMockOutputTest, MismatchArguments) {
  146. const std::string s = "Hi";
  147. EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
  148. foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
  149. foo_.Bar(s, 0, 0);
  150. }
  151. TEST_F(GMockOutputTest, MismatchWith) {
  152. EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
  153. .With(Ge());
  154. foo_.Bar2(2, 3); // Mismatch With()
  155. foo_.Bar2(2, 1);
  156. }
  157. TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
  158. EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
  159. .With(Ge());
  160. foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
  161. foo_.Bar2(2, 1);
  162. }
  163. TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
  164. ON_CALL(foo_, Bar2(_, _))
  165. .WillByDefault(Return(true)); // Default action #1
  166. ON_CALL(foo_, Bar2(1, _))
  167. .WillByDefault(Return(false)); // Default action #2
  168. EXPECT_CALL(foo_, Bar2(2, 2));
  169. foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
  170. foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
  171. foo_.Bar2(2, 2); // Expected call.
  172. }
  173. TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
  174. ON_CALL(foo_, Bar2(_, _))
  175. .WillByDefault(Return(true)); // Default action #1
  176. ON_CALL(foo_, Bar2(1, _))
  177. .WillByDefault(Return(false)); // Default action #2
  178. EXPECT_CALL(foo_, Bar2(2, 2));
  179. EXPECT_CALL(foo_, Bar2(1, 1));
  180. foo_.Bar2(2, 2); // Expected call.
  181. foo_.Bar2(2, 2); // Excessive call, takes default action #1.
  182. foo_.Bar2(1, 1); // Expected call.
  183. foo_.Bar2(1, 1); // Excessive call, takes default action #2.
  184. }
  185. TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
  186. ON_CALL(foo_, Bar2(_, _))
  187. .WillByDefault(Return(true)); // Default action #1
  188. ON_CALL(foo_, Bar2(1, _))
  189. .WillByDefault(Return(false)); // Default action #2
  190. foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
  191. foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
  192. }
  193. TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
  194. ON_CALL(foo_, Bar2(_, _))
  195. .WillByDefault(Return(true)); // Default action #1
  196. EXPECT_CALL(foo_, Bar2(_, _))
  197. .Times(2)
  198. .WillOnce(Return(false));
  199. foo_.Bar2(2, 2);
  200. foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
  201. }
  202. TEST_F(GMockOutputTest, CatchesLeakedMocks) {
  203. MockFoo* foo1 = new MockFoo;
  204. MockFoo* foo2 = new MockFoo;
  205. // Invokes ON_CALL on foo1.
  206. ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
  207. // Invokes EXPECT_CALL on foo2.
  208. EXPECT_CALL(*foo2, Bar2(_, _));
  209. EXPECT_CALL(*foo2, Bar2(1, _));
  210. EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
  211. foo2->Bar2(2, 1);
  212. foo2->Bar2(1, 1);
  213. // Both foo1 and foo2 are deliberately leaked.
  214. }
  215. void TestCatchesLeakedMocksInAdHocTests() {
  216. MockFoo* foo = new MockFoo;
  217. // Invokes EXPECT_CALL on foo.
  218. EXPECT_CALL(*foo, Bar2(_, _));
  219. foo->Bar2(2, 1);
  220. // foo is deliberately leaked.
  221. }
  222. int main(int argc, char **argv) {
  223. testing::InitGoogleMock(&argc, argv);
  224. // Ensures that the tests pass no matter what value of
  225. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
  226. testing::GMOCK_FLAG(catch_leaked_mocks) = true;
  227. testing::GMOCK_FLAG(verbose) = "warning";
  228. TestCatchesLeakedMocksInAdHocTests();
  229. return RUN_ALL_TESTS();
  230. }