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"
41 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
43 using testing::GMOCK_FLAG(verbose
);
44 using testing::InitGoogleMock
;
46 // Verifies that calling InitGoogleMock() on argv results in new_argv,
47 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
48 template <typename Char
, int M
, int N
>
49 void TestInitGoogleMock(const Char
* (&argv
)[M
], const Char
* (&new_argv
)[N
],
50 const ::std::string
& expected_gmock_verbose
) {
51 const ::std::string old_verbose
= GMOCK_FLAG(verbose
);
54 InitGoogleMock(&argc
, const_cast<Char
**>(argv
));
55 ASSERT_EQ(N
, argc
) << "The new argv has wrong number of elements.";
57 for (int i
= 0; i
< N
; i
++) {
58 EXPECT_STREQ(new_argv
[i
], argv
[i
]);
61 EXPECT_EQ(expected_gmock_verbose
, GMOCK_FLAG(verbose
).c_str());
62 GMOCK_FLAG(verbose
) = old_verbose
; // Restores the gmock_verbose flag.
65 TEST(InitGoogleMockTest
, ParsesInvalidCommandLine
) {
66 const char* argv
[] = {
70 const char* new_argv
[] = {
74 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
77 TEST(InitGoogleMockTest
, ParsesEmptyCommandLine
) {
78 const char* argv
[] = {
83 const char* new_argv
[] = {
88 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
91 TEST(InitGoogleMockTest
, ParsesSingleFlag
) {
92 const char* argv
[] = {
94 "--gmock_verbose=info",
98 const char* new_argv
[] = {
103 TestInitGoogleMock(argv
, new_argv
, "info");
106 TEST(InitGoogleMockTest
, ParsesUnrecognizedFlag
) {
107 const char* argv
[] = {
109 "--non_gmock_flag=blah",
113 const char* new_argv
[] = {
115 "--non_gmock_flag=blah",
119 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
122 TEST(InitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
123 const char* argv
[] = {
125 "--non_gmock_flag=blah",
126 "--gmock_verbose=error",
130 const char* new_argv
[] = {
132 "--non_gmock_flag=blah",
136 TestInitGoogleMock(argv
, new_argv
, "error");
139 TEST(WideInitGoogleMockTest
, ParsesInvalidCommandLine
) {
140 const wchar_t* argv
[] = {
144 const wchar_t* new_argv
[] = {
148 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
151 TEST(WideInitGoogleMockTest
, ParsesEmptyCommandLine
) {
152 const wchar_t* argv
[] = {
157 const wchar_t* new_argv
[] = {
162 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
165 TEST(WideInitGoogleMockTest
, ParsesSingleFlag
) {
166 const wchar_t* argv
[] = {
168 L
"--gmock_verbose=info",
172 const wchar_t* new_argv
[] = {
177 TestInitGoogleMock(argv
, new_argv
, "info");
180 TEST(WideInitGoogleMockTest
, ParsesUnrecognizedFlag
) {
181 const wchar_t* argv
[] = {
183 L
"--non_gmock_flag=blah",
187 const wchar_t* new_argv
[] = {
189 L
"--non_gmock_flag=blah",
193 TestInitGoogleMock(argv
, new_argv
, GMOCK_FLAG(verbose
));
196 TEST(WideInitGoogleMockTest
, ParsesGoogleMockFlagAndUnrecognizedFlag
) {
197 const wchar_t* argv
[] = {
199 L
"--non_gmock_flag=blah",
200 L
"--gmock_verbose=error",
204 const wchar_t* new_argv
[] = {
206 L
"--non_gmock_flag=blah",
210 TestInitGoogleMock(argv
, new_argv
, "error");
213 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
215 // Makes sure Google Mock flags can be accessed in code.
216 TEST(FlagTest
, IsAccessibleInCode
) {
217 bool dummy
= testing::GMOCK_FLAG(catch_leaked_mocks
) &&
218 testing::GMOCK_FLAG(verbose
) == "";
219 (void)dummy
; // Avoids the "unused local variable" warning.