1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests code in gmock.cc.
35 #include "gmock/gmock.h"
38 #include "gtest/gtest.h"
39 #include "gtest/internal/custom/gtest.h"
41 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
43 using testing::GMOCK_FLAG(default_mock_behavior);
44 using testing::GMOCK_FLAG(verbose);
45 using testing::InitGoogleMock;
47 // Verifies that calling InitGoogleMock() on argv results in new_argv,
48 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
49 template <typename Char, int M, int N>
50 void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
51 const ::std::string& expected_gmock_verbose) {
52 const ::std::string old_verbose = GMOCK_FLAG(verbose);
55 InitGoogleMock(&argc, const_cast<Char**>(argv));
56 ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
58 for (int i = 0; i < N; i++) {
59 EXPECT_STREQ(new_argv[i], argv[i]);
62 EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
63 GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag.
66 TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
67 const char* argv[] = {
71 const char* new_argv[] = {
75 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
78 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
79 const char* argv[] = {
84 const char* new_argv[] = {
89 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
92 TEST(InitGoogleMockTest, ParsesSingleFlag) {
93 const char* argv[] = {
95 "--gmock_verbose=info",
99 const char* new_argv[] = {
104 TestInitGoogleMock(argv, new_argv, "info");
107 TEST(InitGoogleMockTest, ParsesMultipleFlags) {
108 int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
109 const wchar_t* argv[] = {
111 L"--gmock_verbose=info",
112 L"--gmock_default_mock_behavior=2",
116 const wchar_t* new_argv[] = {
121 TestInitGoogleMock(argv, new_argv, "info");
122 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
123 EXPECT_NE(2, old_default_behavior);
124 GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
127 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
128 const char* argv[] = {
130 "--non_gmock_flag=blah",
134 const char* new_argv[] = {
136 "--non_gmock_flag=blah",
140 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
143 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
144 const char* argv[] = {
146 "--non_gmock_flag=blah",
147 "--gmock_verbose=error",
151 const char* new_argv[] = {
153 "--non_gmock_flag=blah",
157 TestInitGoogleMock(argv, new_argv, "error");
160 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
161 const wchar_t* argv[] = {
165 const wchar_t* new_argv[] = {
169 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
172 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
173 const wchar_t* argv[] = {
178 const wchar_t* new_argv[] = {
183 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
186 TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
187 const wchar_t* argv[] = {
189 L"--gmock_verbose=info",
193 const wchar_t* new_argv[] = {
198 TestInitGoogleMock(argv, new_argv, "info");
201 TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
202 int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
203 const wchar_t* argv[] = {
205 L"--gmock_verbose=info",
206 L"--gmock_default_mock_behavior=2",
210 const wchar_t* new_argv[] = {
215 TestInitGoogleMock(argv, new_argv, "info");
216 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
217 EXPECT_NE(2, old_default_behavior);
218 GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
221 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
222 const wchar_t* argv[] = {
224 L"--non_gmock_flag=blah",
228 const wchar_t* new_argv[] = {
230 L"--non_gmock_flag=blah",
234 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
237 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
238 const wchar_t* argv[] = {
240 L"--non_gmock_flag=blah",
241 L"--gmock_verbose=error",
245 const wchar_t* new_argv[] = {
247 L"--non_gmock_flag=blah",
251 TestInitGoogleMock(argv, new_argv, "error");
254 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
256 // Makes sure Google Mock flags can be accessed in code.
257 TEST(FlagTest, IsAccessibleInCode) {
258 bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
259 testing::GMOCK_FLAG(verbose) == "";
260 (void)dummy; // Avoids the "unused local variable" warning.