gmock_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. // Google Mock - a framework for writing C++ mock classes.
  30. //
  31. // This file tests code in gmock.cc.
  32. #include "gmock/gmock.h"
  33. #include <string>
  34. #include "gtest/gtest.h"
  35. #include "gtest/internal/custom/gtest.h"
  36. #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  37. using testing::GMOCK_FLAG(default_mock_behavior);
  38. using testing::GMOCK_FLAG(verbose);
  39. using testing::InitGoogleMock;
  40. // Verifies that calling InitGoogleMock() on argv results in new_argv,
  41. // and the gmock_verbose flag's value is set to expected_gmock_verbose.
  42. template <typename Char, int M, int N>
  43. void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
  44. const ::std::string& expected_gmock_verbose) {
  45. const ::std::string old_verbose = GMOCK_FLAG(verbose);
  46. int argc = M - 1;
  47. InitGoogleMock(&argc, const_cast<Char**>(argv));
  48. ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
  49. for (int i = 0; i < N; i++) {
  50. EXPECT_STREQ(new_argv[i], argv[i]);
  51. }
  52. EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
  53. GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag.
  54. }
  55. TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
  56. const char* argv[] = {
  57. NULL
  58. };
  59. const char* new_argv[] = {
  60. NULL
  61. };
  62. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  63. }
  64. TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
  65. const char* argv[] = {
  66. "foo.exe",
  67. NULL
  68. };
  69. const char* new_argv[] = {
  70. "foo.exe",
  71. NULL
  72. };
  73. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  74. }
  75. TEST(InitGoogleMockTest, ParsesSingleFlag) {
  76. const char* argv[] = {
  77. "foo.exe",
  78. "--gmock_verbose=info",
  79. NULL
  80. };
  81. const char* new_argv[] = {
  82. "foo.exe",
  83. NULL
  84. };
  85. TestInitGoogleMock(argv, new_argv, "info");
  86. }
  87. TEST(InitGoogleMockTest, ParsesMultipleFlags) {
  88. int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
  89. const wchar_t* argv[] = {
  90. L"foo.exe",
  91. L"--gmock_verbose=info",
  92. L"--gmock_default_mock_behavior=2",
  93. NULL
  94. };
  95. const wchar_t* new_argv[] = {
  96. L"foo.exe",
  97. NULL
  98. };
  99. TestInitGoogleMock(argv, new_argv, "info");
  100. EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
  101. EXPECT_NE(2, old_default_behavior);
  102. GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
  103. }
  104. TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
  105. const char* argv[] = {
  106. "foo.exe",
  107. "--non_gmock_flag=blah",
  108. NULL
  109. };
  110. const char* new_argv[] = {
  111. "foo.exe",
  112. "--non_gmock_flag=blah",
  113. NULL
  114. };
  115. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  116. }
  117. TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  118. const char* argv[] = {
  119. "foo.exe",
  120. "--non_gmock_flag=blah",
  121. "--gmock_verbose=error",
  122. NULL
  123. };
  124. const char* new_argv[] = {
  125. "foo.exe",
  126. "--non_gmock_flag=blah",
  127. NULL
  128. };
  129. TestInitGoogleMock(argv, new_argv, "error");
  130. }
  131. TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
  132. const wchar_t* argv[] = {
  133. NULL
  134. };
  135. const wchar_t* new_argv[] = {
  136. NULL
  137. };
  138. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  139. }
  140. TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
  141. const wchar_t* argv[] = {
  142. L"foo.exe",
  143. NULL
  144. };
  145. const wchar_t* new_argv[] = {
  146. L"foo.exe",
  147. NULL
  148. };
  149. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  150. }
  151. TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
  152. const wchar_t* argv[] = {
  153. L"foo.exe",
  154. L"--gmock_verbose=info",
  155. NULL
  156. };
  157. const wchar_t* new_argv[] = {
  158. L"foo.exe",
  159. NULL
  160. };
  161. TestInitGoogleMock(argv, new_argv, "info");
  162. }
  163. TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
  164. int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
  165. const wchar_t* argv[] = {
  166. L"foo.exe",
  167. L"--gmock_verbose=info",
  168. L"--gmock_default_mock_behavior=2",
  169. NULL
  170. };
  171. const wchar_t* new_argv[] = {
  172. L"foo.exe",
  173. NULL
  174. };
  175. TestInitGoogleMock(argv, new_argv, "info");
  176. EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
  177. EXPECT_NE(2, old_default_behavior);
  178. GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
  179. }
  180. TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
  181. const wchar_t* argv[] = {
  182. L"foo.exe",
  183. L"--non_gmock_flag=blah",
  184. NULL
  185. };
  186. const wchar_t* new_argv[] = {
  187. L"foo.exe",
  188. L"--non_gmock_flag=blah",
  189. NULL
  190. };
  191. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  192. }
  193. TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  194. const wchar_t* argv[] = {
  195. L"foo.exe",
  196. L"--non_gmock_flag=blah",
  197. L"--gmock_verbose=error",
  198. NULL
  199. };
  200. const wchar_t* new_argv[] = {
  201. L"foo.exe",
  202. L"--non_gmock_flag=blah",
  203. NULL
  204. };
  205. TestInitGoogleMock(argv, new_argv, "error");
  206. }
  207. #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  208. // Makes sure Google Mock flags can be accessed in code.
  209. TEST(FlagTest, IsAccessibleInCode) {
  210. bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
  211. testing::GMOCK_FLAG(verbose) == "";
  212. (void)dummy; // Avoids the "unused local variable" warning.
  213. }