gmock_test.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file tests code in gmock.cc.
  34. #include "gmock/gmock.h"
  35. #include <string>
  36. #include "gtest/gtest.h"
  37. #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  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;
  47. InitGoogleMock(&argc, const_cast<Char**>(argv));
  48. ASSERT_EQ(N, 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, ParsesUnrecognizedFlag) {
  88. const char* argv[] = {
  89. "foo.exe",
  90. "--non_gmock_flag=blah",
  91. NULL
  92. };
  93. const char* new_argv[] = {
  94. "foo.exe",
  95. "--non_gmock_flag=blah",
  96. NULL
  97. };
  98. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  99. }
  100. TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  101. const char* argv[] = {
  102. "foo.exe",
  103. "--non_gmock_flag=blah",
  104. "--gmock_verbose=error",
  105. NULL
  106. };
  107. const char* new_argv[] = {
  108. "foo.exe",
  109. "--non_gmock_flag=blah",
  110. NULL
  111. };
  112. TestInitGoogleMock(argv, new_argv, "error");
  113. }
  114. TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
  115. const wchar_t* argv[] = {
  116. NULL
  117. };
  118. const wchar_t* new_argv[] = {
  119. NULL
  120. };
  121. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  122. }
  123. TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
  124. const wchar_t* argv[] = {
  125. L"foo.exe",
  126. NULL
  127. };
  128. const wchar_t* new_argv[] = {
  129. L"foo.exe",
  130. NULL
  131. };
  132. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  133. }
  134. TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
  135. const wchar_t* argv[] = {
  136. L"foo.exe",
  137. L"--gmock_verbose=info",
  138. NULL
  139. };
  140. const wchar_t* new_argv[] = {
  141. L"foo.exe",
  142. NULL
  143. };
  144. TestInitGoogleMock(argv, new_argv, "info");
  145. }
  146. TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
  147. const wchar_t* argv[] = {
  148. L"foo.exe",
  149. L"--non_gmock_flag=blah",
  150. NULL
  151. };
  152. const wchar_t* new_argv[] = {
  153. L"foo.exe",
  154. L"--non_gmock_flag=blah",
  155. NULL
  156. };
  157. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  158. }
  159. TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  160. const wchar_t* argv[] = {
  161. L"foo.exe",
  162. L"--non_gmock_flag=blah",
  163. L"--gmock_verbose=error",
  164. NULL
  165. };
  166. const wchar_t* new_argv[] = {
  167. L"foo.exe",
  168. L"--non_gmock_flag=blah",
  169. NULL
  170. };
  171. TestInitGoogleMock(argv, new_argv, "error");
  172. }
  173. #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  174. // Makes sure Google Mock flags can be accessed in code.
  175. TEST(FlagTest, IsAccessibleInCode) {
  176. bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
  177. testing::GMOCK_FLAG(verbose) == "";
  178. (void)dummy; // Avoids the "unused local variable" warning.
  179. }