Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gmock_test.cc
Go to the documentation of this file.
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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests code in gmock.cc.
35
36#include "gmock/gmock.h"
37
38#include <string>
39#include "gtest/gtest.h"
41
42#if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
43
44using testing::GMOCK_FLAG(default_mock_behavior);
45using testing::GMOCK_FLAG(verbose);
47
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.
50template <typename Char, int M, int N>
51void 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);
54
55 int argc = M - 1;
56 InitGoogleMock(&argc, const_cast<Char**>(argv));
57 ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
58
59 for (int i = 0; i < N; i++) {
60 EXPECT_STREQ(new_argv[i], argv[i]);
61 }
62
63 EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
64 GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag.
65}
66
67TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
68 const char* argv[] = {
69 NULL
70 };
71
72 const char* new_argv[] = {
73 NULL
74 };
75
76 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
77}
78
79TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
80 const char* argv[] = {
81 "foo.exe",
82 NULL
83 };
84
85 const char* new_argv[] = {
86 "foo.exe",
87 NULL
88 };
89
90 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
91}
92
93TEST(InitGoogleMockTest, ParsesSingleFlag) {
94 const char* argv[] = {
95 "foo.exe",
96 "--gmock_verbose=info",
97 NULL
98 };
99
100 const char* new_argv[] = {
101 "foo.exe",
102 NULL
103 };
104
105 TestInitGoogleMock(argv, new_argv, "info");
106}
107
108TEST(InitGoogleMockTest, ParsesMultipleFlags) {
109 int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
110 const wchar_t* argv[] = {
111 L"foo.exe",
112 L"--gmock_verbose=info",
113 L"--gmock_default_mock_behavior=2",
114 NULL
115 };
116
117 const wchar_t* new_argv[] = {
118 L"foo.exe",
119 NULL
120 };
121
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;
126}
127
128TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
129 const char* argv[] = {
130 "foo.exe",
131 "--non_gmock_flag=blah",
132 NULL
133 };
134
135 const char* new_argv[] = {
136 "foo.exe",
137 "--non_gmock_flag=blah",
138 NULL
139 };
140
141 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
142}
143
144TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
145 const char* argv[] = {
146 "foo.exe",
147 "--non_gmock_flag=blah",
148 "--gmock_verbose=error",
149 NULL
150 };
151
152 const char* new_argv[] = {
153 "foo.exe",
154 "--non_gmock_flag=blah",
155 NULL
156 };
157
158 TestInitGoogleMock(argv, new_argv, "error");
159}
160
161TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
162 const wchar_t* argv[] = {
163 NULL
164 };
165
166 const wchar_t* new_argv[] = {
167 NULL
168 };
169
170 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
171}
172
173TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
174 const wchar_t* argv[] = {
175 L"foo.exe",
176 NULL
177 };
178
179 const wchar_t* new_argv[] = {
180 L"foo.exe",
181 NULL
182 };
183
184 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
185}
186
187TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
188 const wchar_t* argv[] = {
189 L"foo.exe",
190 L"--gmock_verbose=info",
191 NULL
192 };
193
194 const wchar_t* new_argv[] = {
195 L"foo.exe",
196 NULL
197 };
198
199 TestInitGoogleMock(argv, new_argv, "info");
200}
201
202TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
203 int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
204 const wchar_t* argv[] = {
205 L"foo.exe",
206 L"--gmock_verbose=info",
207 L"--gmock_default_mock_behavior=2",
208 NULL
209 };
210
211 const wchar_t* new_argv[] = {
212 L"foo.exe",
213 NULL
214 };
215
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;
220}
221
222TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
223 const wchar_t* argv[] = {
224 L"foo.exe",
225 L"--non_gmock_flag=blah",
226 NULL
227 };
228
229 const wchar_t* new_argv[] = {
230 L"foo.exe",
231 L"--non_gmock_flag=blah",
232 NULL
233 };
234
235 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
236}
237
238TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
239 const wchar_t* argv[] = {
240 L"foo.exe",
241 L"--non_gmock_flag=blah",
242 L"--gmock_verbose=error",
243 NULL
244 };
245
246 const wchar_t* new_argv[] = {
247 L"foo.exe",
248 L"--non_gmock_flag=blah",
249 NULL
250 };
251
252 TestInitGoogleMock(argv, new_argv, "error");
253}
254
255#endif // !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
256
257// Makes sure Google Mock flags can be accessed in code.
258TEST(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.
262}
#define GMOCK_FLAG(name)
Definition gmock-port.h:66
void TestInitGoogleMock(const Char *(&argv)[M], const Char *(&new_argv)[N], const ::std::string &expected_gmock_verbose)
Definition gmock_test.cc:51
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_NE(val1, val2)
Definition gtest.h:1958
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:2027
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
char ** argv
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition gmock.cc:195
bool verbose
Definition main.cpp:190
const int N
Definition quantize.cpp:54