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.

181 lines
6.3 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. // 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. }