gmock_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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[] = {nullptr};
  57. const char* new_argv[] = {nullptr};
  58. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  59. }
  60. TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
  61. const char* argv[] = {"foo.exe", nullptr};
  62. const char* new_argv[] = {"foo.exe", nullptr};
  63. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  64. }
  65. TEST(InitGoogleMockTest, ParsesSingleFlag) {
  66. const char* argv[] = {"foo.exe", "--gmock_verbose=info", nullptr};
  67. const char* new_argv[] = {"foo.exe", nullptr};
  68. TestInitGoogleMock(argv, new_argv, "info");
  69. }
  70. TEST(InitGoogleMockTest, ParsesMultipleFlags) {
  71. int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
  72. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
  73. L"--gmock_default_mock_behavior=2", nullptr};
  74. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  75. TestInitGoogleMock(argv, new_argv, "info");
  76. EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
  77. EXPECT_NE(2, old_default_behavior);
  78. GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
  79. }
  80. TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
  81. const char* argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  82. const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  83. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  84. }
  85. TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  86. const char* argv[] = {"foo.exe", "--non_gmock_flag=blah",
  87. "--gmock_verbose=error", nullptr};
  88. const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
  89. TestInitGoogleMock(argv, new_argv, "error");
  90. }
  91. TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
  92. const wchar_t* argv[] = {nullptr};
  93. const wchar_t* new_argv[] = {nullptr};
  94. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  95. }
  96. TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
  97. const wchar_t* argv[] = {L"foo.exe", nullptr};
  98. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  99. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  100. }
  101. TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
  102. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info", nullptr};
  103. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  104. TestInitGoogleMock(argv, new_argv, "info");
  105. }
  106. TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
  107. int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
  108. const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
  109. L"--gmock_default_mock_behavior=2", nullptr};
  110. const wchar_t* new_argv[] = {L"foo.exe", nullptr};
  111. TestInitGoogleMock(argv, new_argv, "info");
  112. EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
  113. EXPECT_NE(2, old_default_behavior);
  114. GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
  115. }
  116. TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
  117. const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  118. const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  119. TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
  120. }
  121. TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
  122. const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah",
  123. L"--gmock_verbose=error", nullptr};
  124. const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
  125. TestInitGoogleMock(argv, new_argv, "error");
  126. }
  127. #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
  128. // Makes sure Google Mock flags can be accessed in code.
  129. TEST(FlagTest, IsAccessibleInCode) {
  130. bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
  131. testing::GMOCK_FLAG(verbose) == "";
  132. (void)dummy; // Avoids the "unused local variable" warning.
  133. }