blob: baba71788fbc0fa98e3a3809eb68ffdca61d862b [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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
zhanyong.wan53e08c42010-09-14 05:38:21 +000036#include "gmock/gmock.h"
shiqiane35fdd92008-12-10 05:08:54 +000037
38#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000039#include "gtest/gtest.h"
shiqiane35fdd92008-12-10 05:08:54 +000040
41using testing::GMOCK_FLAG(verbose);
42using testing::InitGoogleMock;
shiqiane35fdd92008-12-10 05:08:54 +000043
44// Verifies that calling InitGoogleMock() on argv results in new_argv,
45// and the gmock_verbose flag's value is set to expected_gmock_verbose.
46template <typename Char, int M, int N>
47void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
48 const ::std::string& expected_gmock_verbose) {
49 const ::std::string old_verbose = GMOCK_FLAG(verbose);
50
51 int argc = M;
52 InitGoogleMock(&argc, const_cast<Char**>(argv));
53 ASSERT_EQ(N, argc) << "The new argv has wrong number of elements.";
54
55 for (int i = 0; i < N; i++) {
56 EXPECT_STREQ(new_argv[i], argv[i]);
57 }
58
59 EXPECT_EQ(expected_gmock_verbose, GMOCK_FLAG(verbose).c_str());
60 GMOCK_FLAG(verbose) = old_verbose; // Restores the gmock_verbose flag.
61}
62
63TEST(InitGoogleMockTest, ParsesInvalidCommandLine) {
64 const char* argv[] = {
65 NULL
66 };
67
68 const char* new_argv[] = {
69 NULL
70 };
71
72 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
73}
74
75TEST(InitGoogleMockTest, ParsesEmptyCommandLine) {
76 const char* argv[] = {
77 "foo.exe",
78 NULL
79 };
80
81 const char* new_argv[] = {
82 "foo.exe",
83 NULL
84 };
85
86 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
87}
88
89TEST(InitGoogleMockTest, ParsesSingleFlag) {
90 const char* argv[] = {
91 "foo.exe",
92 "--gmock_verbose=info",
93 NULL
94 };
95
96 const char* new_argv[] = {
97 "foo.exe",
98 NULL
99 };
100
101 TestInitGoogleMock(argv, new_argv, "info");
102}
103
104TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
105 const char* argv[] = {
106 "foo.exe",
107 "--non_gmock_flag=blah",
108 NULL
109 };
110
111 const char* new_argv[] = {
112 "foo.exe",
113 "--non_gmock_flag=blah",
114 NULL
115 };
116
117 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
118}
119
120TEST(InitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
121 const char* argv[] = {
122 "foo.exe",
123 "--non_gmock_flag=blah",
124 "--gmock_verbose=error",
125 NULL
126 };
127
128 const char* new_argv[] = {
129 "foo.exe",
130 "--non_gmock_flag=blah",
131 NULL
132 };
133
134 TestInitGoogleMock(argv, new_argv, "error");
135}
136
shiqiane35fdd92008-12-10 05:08:54 +0000137TEST(WideInitGoogleMockTest, ParsesInvalidCommandLine) {
138 const wchar_t* argv[] = {
139 NULL
140 };
141
142 const wchar_t* new_argv[] = {
143 NULL
144 };
145
146 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
147}
148
149TEST(WideInitGoogleMockTest, ParsesEmptyCommandLine) {
150 const wchar_t* argv[] = {
151 L"foo.exe",
152 NULL
153 };
154
155 const wchar_t* new_argv[] = {
156 L"foo.exe",
157 NULL
158 };
159
160 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
161}
162
163TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
164 const wchar_t* argv[] = {
165 L"foo.exe",
166 L"--gmock_verbose=info",
167 NULL
168 };
169
170 const wchar_t* new_argv[] = {
171 L"foo.exe",
172 NULL
173 };
174
175 TestInitGoogleMock(argv, new_argv, "info");
176}
177
178TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
179 const wchar_t* argv[] = {
180 L"foo.exe",
181 L"--non_gmock_flag=blah",
182 NULL
183 };
184
185 const wchar_t* new_argv[] = {
186 L"foo.exe",
187 L"--non_gmock_flag=blah",
188 NULL
189 };
190
191 TestInitGoogleMock(argv, new_argv, GMOCK_FLAG(verbose));
192}
193
194TEST(WideInitGoogleMockTest, ParsesGoogleMockFlagAndUnrecognizedFlag) {
195 const wchar_t* argv[] = {
196 L"foo.exe",
197 L"--non_gmock_flag=blah",
198 L"--gmock_verbose=error",
199 NULL
200 };
201
202 const wchar_t* new_argv[] = {
203 L"foo.exe",
204 L"--non_gmock_flag=blah",
205 NULL
206 };
207
208 TestInitGoogleMock(argv, new_argv, "error");
209}
210
zhanyong.wandf35a762009-04-22 22:25:31 +0000211// Makes sure Google Mock flags can be accessed in code.
212TEST(FlagTest, IsAccessibleInCode) {
213 bool dummy = testing::GMOCK_FLAG(catch_leaked_mocks) &&
214 testing::GMOCK_FLAG(verbose) == "";
zhanyong.wana684b5a2010-12-02 23:30:50 +0000215 (void)dummy; // Avoids the "unused local variable" warning.
zhanyong.wandf35a762009-04-22 22:25:31 +0000216}