gmock_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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::InitGoogleMock;
  38. // Verifies that calling InitGoogleMock() on argv results in new_argv,
  39. // and the gmock_verbose flag's value is set to expected_gmock_verbose.
  40. template <typename Char, int M, int N>
  41. void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
  42. const ::std::string& expected_gmock_verbose) {
  43. const ::std::string old_verbose = GMOCK_FLAG_GET(verbose);
  44. int argc = M - 1;
  45. InitGoogleMock(&argc, const_cast<Char**>(argv));
  46. ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
  47. for (int i = 0; i < N; i++) {
  48. EXPECT_STREQ(new_argv[i], argv[i]);
  49. }
  50. EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG_GET(verbose));
  51. GMOCK_FLAG_SET(verbose, old_verbose); // Restores the gmock_verbose flag.
  52. }
  53. TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
  54. const char* argv[] = {nullptr};
  55. const char* new_argv[] = {nullptr};
  56. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  57. }
  58. TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
  59. const char* argv[] = {"foo.exe", nullptr};
  60. const char* new_argv[] = {"foo.exe", nullptr};
  61. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  62. }
  63. TEST(InitGoogleMockTest, ParsesSingleFlag) {
  64. const char* argv[] = {"foo.exe", "--gmock_verbose=info", nullptr};
  65. const char* new_argv[] = {"foo.exe", nullptr};
  66. TestInitGoogleMock(argv, new_argv, "info");
  67. }
  68. TEST(InitGoogleMockTest, ParsesMultipleFlags) {
  69. int old_default_behavior = GMOCK_FLAG_GET(default_mock_behavior);
  70. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
  71. L"--gmock_default_mock_behavior=2", nullptr};
  72. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  73. TestInitGoogleMock(argv, new_argv, "info");
  74. EXPECT_EQ(2, GMOCK_FLAG_GET(default_mock_behavior));
  75. EXPECT_NE(2, old_default_behavior);
  76. GMOCK_FLAG_SET(default_mock_behavior, old_default_behavior);
  77. }
  78. TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
  79. const char* argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  80. const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  81. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  82. }
  83. TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  84. const char* argv[] = {"foo.exe", "--non_gmock_flag=blah",
  85. "--gmock_verbose=error", nullptr};
  86. const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  87. TestInitGoogleMock(argv, new_argv, "error");
  88. }
  89. TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
  90. const wchar_t* argv[] = {nullptr};
  91. const wchar_t* new_argv[] = {nullptr};
  92. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  93. }
  94. TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
  95. const wchar_t* argv[] = {L"foo.exe", nullptr};
  96. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  97. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  98. }
  99. TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
  100. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info", nullptr};
  101. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  102. TestInitGoogleMock(argv, new_argv, "info");
  103. }
  104. TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
  105. int old_default_behavior = GMOCK_FLAG_GET(default_mock_behavior);
  106. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
  107. L"--gmock_default_mock_behavior=2", nullptr};
  108. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  109. TestInitGoogleMock(argv, new_argv, "info");
  110. EXPECT_EQ(2, GMOCK_FLAG_GET(default_mock_behavior));
  111. EXPECT_NE(2, old_default_behavior);
  112. GMOCK_FLAG_SET(default_mock_behavior, old_default_behavior);
  113. }
  114. TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
  115. const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  116. const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  117. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
  118. }
  119. TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  120. const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah",
  121. L"--gmock_verbose=error", nullptr};
  122. const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  123. TestInitGoogleMock(argv, new_argv, "error");
  124. }
  125. #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  126. // Makes sure Google Mock flags can be accessed in code.
  127. TEST(FlagTest, IsAccessibleInCode) {
  128. bool dummy =
  129. GMOCK_FLAG_GET(catch_leaked_mocks) && GMOCK_FLAG_GET(verbose).empty();
  130. (void)dummy; // Avoids the "unused local variable" warning.
  131. }