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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Mock - a framework for writing C++ mock classes.
34 // This file tests code in gmock.cc.
36 #include "gmock/gmock.h"
39 #include "gtest/gtest.h"
40 #include "gtest/internal/custom/gtest.h"
42 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
44 using testing::GMOCK_FLAG(default_mock_behavior
);
45 using testing::GMOCK_FLAG(verbose
);
46 using testing::InitGoogleMock
;
48 // Verifies that calling InitGoogleMock() on argv results in new_argv,
49 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
50 template <typename Char
, int M
, int N
>
51 void TestInitGoogleMock(const Char
* (&argv
)[M
], const Char
* (&new_argv
)[N
],
52 const ::std::string
& expected_gmock_verbose
) {
53 const ::std::string old_verbose
= GMOCK_FLAG(verbose
);
56 InitGoogleMock(&argc
, const_cast<Char
**>(argv
));
57 ASSERT_EQ(N
- 1, argc
) << "The new argv has wrong number of elements.";
59 for (int i
= 0; i
< N
; i
++) {
60 EXPECT_STREQ(new_argv
[i
], argv
[i
]);
63 EXPECT_EQ(expected_gmock_verbose
, GMOCK_FLAG(verbose
).c_str());
64 GMOCK_FLAG(verbose
) = old_verbose
; // Restores the gmock_verbose flag.
67 TEST(InitGoogleMockTest
, ParsesInvalidCommandLine
) {
68 const char* argv
[] = {
72 const char* new_argv
[] = {
76 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
79 TEST(InitGoogleMockTest
, ParsesEmptyCommandLine
) {
80 const char* argv
[] = {
85 const char* new_argv
[] = {
90 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
93 TEST(InitGoogleMockTest
, ParsesSingleFlag
) {
94 const char* argv
[] = {
96 "--gmock_verbose=info",
100 const char* new_argv
[] = {
105 TestInitGoogleMock(argv
, new_argv
, "info");
108 TEST(InitGoogleMockTest
, ParsesMultipleFlags
) {
109 int old_default_behavior
= GMOCK_FLAG(default_mock_behavior
);
110 const wchar_t* argv
[] = {
112 L
"--gmock_verbose=info",
113 L
"--gmock_default_mock_behavior=2",
117 const wchar_t* new_argv
[] = {
122 TestInitGoogleMock(argv
, new_argv
, "info");
123 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior
));
124 EXPECT_NE(2, old_default_behavior
);
125 GMOCK_FLAG(default_mock_behavior
) = old_default_behavior
;
128 TEST(InitGoogleMockTest
, ParsesUnrecognizedFlag
) {
129 const char* argv
[] = {
131 "--non_gmock_flag=blah",
135 const char* new_argv
[] = {
137 "--non_gmock_flag=blah",
141 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
144 TEST(InitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
145 const char* argv
[] = {
147 "--non_gmock_flag=blah",
148 "--gmock_verbose=error",
152 const char* new_argv
[] = {
154 "--non_gmock_flag=blah",
158 TestInitGoogleMock(argv
, new_argv
, "error");
161 TEST(WideInitGoogleMockTest
, ParsesInvalidCommandLine
) {
162 const wchar_t* argv
[] = {
166 const wchar_t* new_argv
[] = {
170 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
173 TEST(WideInitGoogleMockTest
, ParsesEmptyCommandLine
) {
174 const wchar_t* argv
[] = {
179 const wchar_t* new_argv
[] = {
184 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
187 TEST(WideInitGoogleMockTest
, ParsesSingleFlag
) {
188 const wchar_t* argv
[] = {
190 L
"--gmock_verbose=info",
194 const wchar_t* new_argv
[] = {
199 TestInitGoogleMock(argv
, new_argv
, "info");
202 TEST(WideInitGoogleMockTest
, ParsesMultipleFlags
) {
203 int old_default_behavior
= GMOCK_FLAG(default_mock_behavior
);
204 const wchar_t* argv
[] = {
206 L
"--gmock_verbose=info",
207 L
"--gmock_default_mock_behavior=2",
211 const wchar_t* new_argv
[] = {
216 TestInitGoogleMock(argv
, new_argv
, "info");
217 EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior
));
218 EXPECT_NE(2, old_default_behavior
);
219 GMOCK_FLAG(default_mock_behavior
) = old_default_behavior
;
222 TEST(WideInitGoogleMockTest
, ParsesUnrecognizedFlag
) {
223 const wchar_t* argv
[] = {
225 L
"--non_gmock_flag=blah",
229 const wchar_t* new_argv
[] = {
231 L
"--non_gmock_flag=blah",
235 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
238 TEST(WideInitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
239 const wchar_t* argv
[] = {
241 L
"--non_gmock_flag=blah",
242 L
"--gmock_verbose=error",
246 const wchar_t* new_argv
[] = {
248 L
"--non_gmock_flag=blah",
252 TestInitGoogleMock(argv
, new_argv
, "error");
255 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
257 // Makes sure Google Mock flags can be accessed in code.
258 TEST(FlagTest
, IsAccessibleInCode
) {
259 bool dummy
= testing::GMOCK_FLAG(catch_leaked_mocks
) &&
260 testing::GMOCK_FLAG(verbose
) == "";
261 (void)dummy
; // Avoids the "unused local variable" warning.