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
[] = {nullptr};
69 const char* new_argv
[] = {nullptr};
71 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
74 TEST(InitGoogleMockTest
, ParsesEmptyCommandLine
) {
75 const char* argv
[] = {"foo.exe", nullptr};
77 const char* new_argv
[] = {"foo.exe", nullptr};
79 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
82 TEST(InitGoogleMockTest
, ParsesSingleFlag
) {
83 const char* argv
[] = {"foo.exe", "--gmock_verbose=info", nullptr};
85 const char* new_argv
[] = {"foo.exe", nullptr};
87 TestInitGoogleMock(argv
, new_argv
, "info");
90 TEST(InitGoogleMockTest
, ParsesMultipleFlags
) {
91 int old_default_behavior
= GMOCK_FLAG(default_mock_behavior
);
92 const wchar_t* argv
[] = {L
"foo.exe", L
"--gmock_verbose=info",
93 L
"--gmock_default_mock_behavior=2", nullptr};
95 const wchar_t* new_argv
[] = {L
"foo.exe", nullptr};
97 TestInitGoogleMock(argv
, new_argv
, "info");
98 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior
));
99 EXPECT_NE(2, old_default_behavior
);
100 GMOCK_FLAG(default_mock_behavior
) = old_default_behavior
;
103 TEST(InitGoogleMockTest
, ParsesUnrecognizedFlag
) {
104 const char* argv
[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
106 const char* new_argv
[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
108 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
111 TEST(InitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
112 const char* argv
[] = {"foo.exe", "--non_gmock_flag=blah",
113 "--gmock_verbose=error", nullptr};
115 const char* new_argv
[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
117 TestInitGoogleMock(argv
, new_argv
, "error");
120 TEST(WideInitGoogleMockTest
, ParsesInvalidCommandLine
) {
121 const wchar_t* argv
[] = {nullptr};
123 const wchar_t* new_argv
[] = {nullptr};
125 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
128 TEST(WideInitGoogleMockTest
, ParsesEmptyCommandLine
) {
129 const wchar_t* argv
[] = {L
"foo.exe", nullptr};
131 const wchar_t* new_argv
[] = {L
"foo.exe", nullptr};
133 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
136 TEST(WideInitGoogleMockTest
, ParsesSingleFlag
) {
137 const wchar_t* argv
[] = {L
"foo.exe", L
"--gmock_verbose=info", nullptr};
139 const wchar_t* new_argv
[] = {L
"foo.exe", nullptr};
141 TestInitGoogleMock(argv
, new_argv
, "info");
144 TEST(WideInitGoogleMockTest
, ParsesMultipleFlags
) {
145 int old_default_behavior
= GMOCK_FLAG(default_mock_behavior
);
146 const wchar_t* argv
[] = {L
"foo.exe", L
"--gmock_verbose=info",
147 L
"--gmock_default_mock_behavior=2", nullptr};
149 const wchar_t* new_argv
[] = {L
"foo.exe", nullptr};
151 TestInitGoogleMock(argv
, new_argv
, "info");
152 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior
));
153 EXPECT_NE(2, old_default_behavior
);
154 GMOCK_FLAG(default_mock_behavior
) = old_default_behavior
;
157 TEST(WideInitGoogleMockTest
, ParsesUnrecognizedFlag
) {
158 const wchar_t* argv
[] = {L
"foo.exe", L
"--non_gmock_flag=blah", nullptr};
160 const wchar_t* new_argv
[] = {L
"foo.exe", L
"--non_gmock_flag=blah", nullptr};
162 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
165 TEST(WideInitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
166 const wchar_t* argv
[] = {L
"foo.exe", L
"--non_gmock_flag=blah",
167 L
"--gmock_verbose=error", nullptr};
169 const wchar_t* new_argv
[] = {L
"foo.exe", L
"--non_gmock_flag=blah", nullptr};
171 TestInitGoogleMock(argv
, new_argv
, "error");
174 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
176 // Makes sure Google Mock flags can be accessed in code.
177 TEST(FlagTest
, IsAccessibleInCode
) {
178 bool dummy
= testing::GMOCK_FLAG(catch_leaked_mocks
) &&
179 testing::GMOCK_FLAG(verbose
) == "";
180 (void)dummy
; // Avoids the "unused local variable" warning.