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
30 // Google Mock - a framework for writing C++ mock classes.
31 //
32 // This file tests code in gmock.cc.
33
34 #include "gmock/gmock.h"
35
36 #include <string>
37
38 #include "gtest/gtest.h"
39 #include "gtest/internal/custom/gtest.h"
40
41 #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
42
43 using testing::InitGoogleMock;
44
45 // Verifies that calling InitGoogleMock() on argv results in new_argv,
46 // and the gmock_verbose flag's value is set to expected_gmock_verbose.
47 template <typename Char, int M, int N>
TestInitGoogleMock(const Char * (& argv)[M],const Char * (& new_argv)[N],const::std::string & expected_gmock_verbose)48 void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
49 const ::std::string& expected_gmock_verbose) {
50 const ::std::string old_verbose = GMOCK_FLAG_GET(verbose);
51
52 int argc = M - 1;
53 InitGoogleMock(&argc, const_cast<Char**>(argv));
54 ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
55
56 for (int i = 0; i < N; i++) {
57 EXPECT_STREQ(new_argv[i], argv[i]);
58 }
59
60 EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG_GET(verbose));
61 GMOCK_FLAG_SET(verbose, old_verbose); // Restores the gmock_verbose flag.
62 }
63
TEST(InitGoogleMockTest,ParsesInvalidCommandLine)64 TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
65 const char* argv[] = {nullptr};
66
67 const char* new_argv[] = {nullptr};
68
69 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
70 }
71
TEST(InitGoogleMockTest,ParsesEmptyCommandLine)72 TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
73 const char* argv[] = {"foo.exe", nullptr};
74
75 const char* new_argv[] = {"foo.exe", nullptr};
76
77 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
78 }
79
TEST(InitGoogleMockTest,ParsesSingleFlag)80 TEST(InitGoogleMockTest, ParsesSingleFlag) {
81 const char* argv[] = {"foo.exe", "--gmock_verbose=info", nullptr};
82
83 const char* new_argv[] = {"foo.exe", nullptr};
84
85 TestInitGoogleMock(argv, new_argv, "info");
86 }
87
TEST(InitGoogleMockTest,ParsesMultipleFlags)88 TEST(InitGoogleMockTest, ParsesMultipleFlags) {
89 int old_default_behavior = GMOCK_FLAG_GET(default_mock_behavior);
90 const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
91 L"--gmock_default_mock_behavior=2", nullptr};
92
93 const wchar_t* new_argv[] = {L"foo.exe", nullptr};
94
95 TestInitGoogleMock(argv, new_argv, "info");
96 EXPECT_EQ(2, GMOCK_FLAG_GET(default_mock_behavior));
97 EXPECT_NE(2, old_default_behavior);
98 GMOCK_FLAG_SET(default_mock_behavior, old_default_behavior);
99 }
100
TEST(InitGoogleMockTest,ParsesUnrecognizedFlag)101 TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
102 const char* argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
103
104 const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
105
106 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
107 }
108
TEST(InitGoogleMockTest,ParsesGoogleMockFlagAndUnrecognizedFlag)109 TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
110 const char* argv[] = {"foo.exe", "--non_gmock_flag=blah",
111 "--gmock_verbose=error", nullptr};
112
113 const char* new_argv[] = {"foo.exe", "--non_gmock_flag=blah", nullptr};
114
115 TestInitGoogleMock(argv, new_argv, "error");
116 }
117
TEST(WideInitGoogleMockTest,ParsesInvalidCommandLine)118 TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
119 const wchar_t* argv[] = {nullptr};
120
121 const wchar_t* new_argv[] = {nullptr};
122
123 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
124 }
125
TEST(WideInitGoogleMockTest,ParsesEmptyCommandLine)126 TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
127 const wchar_t* argv[] = {L"foo.exe", nullptr};
128
129 const wchar_t* new_argv[] = {L"foo.exe", nullptr};
130
131 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
132 }
133
TEST(WideInitGoogleMockTest,ParsesSingleFlag)134 TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
135 const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info", nullptr};
136
137 const wchar_t* new_argv[] = {L"foo.exe", nullptr};
138
139 TestInitGoogleMock(argv, new_argv, "info");
140 }
141
TEST(WideInitGoogleMockTest,ParsesMultipleFlags)142 TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
143 int old_default_behavior = GMOCK_FLAG_GET(default_mock_behavior);
144 const wchar_t* argv[] = {L"foo.exe", L"--gmock_verbose=info",
145 L"--gmock_default_mock_behavior=2", nullptr};
146
147 const wchar_t* new_argv[] = {L"foo.exe", nullptr};
148
149 TestInitGoogleMock(argv, new_argv, "info");
150 EXPECT_EQ(2, GMOCK_FLAG_GET(default_mock_behavior));
151 EXPECT_NE(2, old_default_behavior);
152 GMOCK_FLAG_SET(default_mock_behavior, old_default_behavior);
153 }
154
TEST(WideInitGoogleMockTest,ParsesUnrecognizedFlag)155 TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
156 const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
157
158 const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
159
160 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG_GET(verbose));
161 }
162
TEST(WideInitGoogleMockTest,ParsesGoogleMockFlagAndUnrecognizedFlag)163 TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
164 const wchar_t* argv[] = {L"foo.exe", L"--non_gmock_flag=blah",
165 L"--gmock_verbose=error", nullptr};
166
167 const wchar_t* new_argv[] = {L"foo.exe", L"--non_gmock_flag=blah", nullptr};
168
169 TestInitGoogleMock(argv, new_argv, "error");
170 }
171
172 #endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
173
174 // Makes sure Google Mock flags can be accessed in code.
TEST(FlagTest,IsAccessibleInCode)175 TEST(FlagTest, IsAccessibleInCode) {
176 bool dummy =
177 GMOCK_FLAG_GET(catch_leaked_mocks) && GMOCK_FLAG_GET(verbose).empty();
178 (void)dummy; // Avoids the "unused local variable" warning.
179 }
180