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.

309 lines
8.4 KiB

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