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"
40
41using testing::GMOCK_FLAG(verbose);
42using testing::InitGoogleMock;
43using testing::internal::g_init_gtest_count;
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.
47template <typename Char, int M, int N>
48void 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(verbose);
51
52  int argc = M;
53  InitGoogleMock(&argc, const_cast<Char**>(argv));
54  ASSERT_EQ(N, 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(verbose).c_str());
61  GMOCK_FLAG(verbose) = old_verbose;  // Restores the gmock_verbose flag.
62}
63
64TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
65  const char* argv[] = {
66    NULL
67  };
68
69  const char* new_argv[] = {
70    NULL
71  };
72
73  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
74}
75
76TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
77  const char* argv[] = {
78    "foo.exe",
79    NULL
80  };
81
82  const char* new_argv[] = {
83    "foo.exe",
84    NULL
85  };
86
87  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
88}
89
90TEST(InitGoogleMockTest, ParsesSingleFlag) {
91  const char* argv[] = {
92    "foo.exe",
93    "--gmock_verbose=info",
94    NULL
95  };
96
97  const char* new_argv[] = {
98    "foo.exe",
99    NULL
100  };
101
102  TestInitGoogleMock(argv, new_argv, "info");
103}
104
105TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
106  const char* argv[] = {
107    "foo.exe",
108    "--non_gmock_flag=blah",
109    NULL
110  };
111
112  const char* new_argv[] = {
113    "foo.exe",
114    "--non_gmock_flag=blah",
115    NULL
116  };
117
118  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
119}
120
121TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
122  const char* argv[] = {
123    "foo.exe",
124    "--non_gmock_flag=blah",
125    "--gmock_verbose=error",
126    NULL
127  };
128
129  const char* new_argv[] = {
130    "foo.exe",
131    "--non_gmock_flag=blah",
132    NULL
133  };
134
135  TestInitGoogleMock(argv, new_argv, "error");
136}
137
138TEST(InitGoogleMockTest, CallsInitGoogleTest) {
139  const int old_init_gtest_count = g_init_gtest_count;
140  const char* argv[] = {
141    "foo.exe",
142    "--non_gmock_flag=blah",
143    "--gmock_verbose=error",
144    NULL
145  };
146
147  const char* new_argv[] = {
148    "foo.exe",
149    "--non_gmock_flag=blah",
150    NULL
151  };
152
153  TestInitGoogleMock(argv, new_argv, "error");
154  EXPECT_EQ(old_init_gtest_count + 1, g_init_gtest_count);
155}
156
157TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
158  const wchar_t* argv[] = {
159    NULL
160  };
161
162  const wchar_t* new_argv[] = {
163    NULL
164  };
165
166  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
167}
168
169TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
170  const wchar_t* argv[] = {
171    L"foo.exe",
172    NULL
173  };
174
175  const wchar_t* new_argv[] = {
176    L"foo.exe",
177    NULL
178  };
179
180  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
181}
182
183TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
184  const wchar_t* argv[] = {
185    L"foo.exe",
186    L"--gmock_verbose=info",
187    NULL
188  };
189
190  const wchar_t* new_argv[] = {
191    L"foo.exe",
192    NULL
193  };
194
195  TestInitGoogleMock(argv, new_argv, "info");
196}
197
198TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
199  const wchar_t* argv[] = {
200    L"foo.exe",
201    L"--non_gmock_flag=blah",
202    NULL
203  };
204
205  const wchar_t* new_argv[] = {
206    L"foo.exe",
207    L"--non_gmock_flag=blah",
208    NULL
209  };
210
211  TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
212}
213
214TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
215  const wchar_t* argv[] = {
216    L"foo.exe",
217    L"--non_gmock_flag=blah",
218    L"--gmock_verbose=error",
219    NULL
220  };
221
222  const wchar_t* new_argv[] = {
223    L"foo.exe",
224    L"--non_gmock_flag=blah",
225    NULL
226  };
227
228  TestInitGoogleMock(argv, new_argv, "error");
229}
230
231TEST(WideInitGoogleMockTest, CallsInitGoogleTest) {
232  const int old_init_gtest_count = g_init_gtest_count;
233  const wchar_t* argv[] = {
234    L"foo.exe",
235    L"--non_gmock_flag=blah",
236    L"--gmock_verbose=error",
237    NULL
238  };
239
240  const wchar_t* new_argv[] = {
241    L"foo.exe",
242    L"--non_gmock_flag=blah",
243    NULL
244  };
245
246  TestInitGoogleMock(argv, new_argv, "error");
247  EXPECT_EQ(old_init_gtest_count + 1, g_init_gtest_count);
248}
249
250// Makes sure Google Mock flags can be accessed in code.
251TEST(FlagTest, IsAccessibleInCode) {
252  bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
253      testing::GMOCK_FLAG(verbose) == "";
254  (void)dummy;  // Avoids the "unused local variable" warning.
255}
256