1 |
2 |
jamieiles |
// Copyright 2007, 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 the internal utilities.
|
35 |
|
|
|
36 |
|
|
#include "gmock/internal/gmock-internal-utils.h"
|
37 |
|
|
#include <stdlib.h>
|
38 |
|
|
#include <map>
|
39 |
|
|
#include <memory>
|
40 |
|
|
#include <string>
|
41 |
|
|
#include <sstream>
|
42 |
|
|
#include <vector>
|
43 |
|
|
#include "gmock/gmock.h"
|
44 |
|
|
#include "gmock/internal/gmock-port.h"
|
45 |
|
|
#include "gtest/gtest.h"
|
46 |
|
|
#include "gtest/gtest-spi.h"
|
47 |
|
|
|
48 |
|
|
// Indicates that this translation unit is part of Google Test's
|
49 |
|
|
// implementation. It must come before gtest-internal-inl.h is
|
50 |
|
|
// included, or there will be a compiler error. This trick is to
|
51 |
|
|
// prevent a user from accidentally including gtest-internal-inl.h in
|
52 |
|
|
// his code.
|
53 |
|
|
#define GTEST_IMPLEMENTATION_ 1
|
54 |
|
|
#include "src/gtest-internal-inl.h"
|
55 |
|
|
#undef GTEST_IMPLEMENTATION_
|
56 |
|
|
|
57 |
|
|
#if GTEST_OS_CYGWIN
|
58 |
|
|
# include <sys/types.h> // For ssize_t. NOLINT
|
59 |
|
|
#endif
|
60 |
|
|
|
61 |
|
|
class ProtocolMessage;
|
62 |
|
|
|
63 |
|
|
namespace proto2 {
|
64 |
|
|
class Message;
|
65 |
|
|
} // namespace proto2
|
66 |
|
|
|
67 |
|
|
namespace testing {
|
68 |
|
|
namespace internal {
|
69 |
|
|
|
70 |
|
|
namespace {
|
71 |
|
|
|
72 |
|
|
TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
|
73 |
|
|
EXPECT_EQ("", ConvertIdentifierNameToWords(""));
|
74 |
|
|
EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
|
75 |
|
|
EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
|
79 |
|
|
EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
|
80 |
|
|
EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
|
81 |
|
|
EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
|
82 |
|
|
EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
|
86 |
|
|
EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
|
87 |
|
|
EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
|
88 |
|
|
EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_"));
|
89 |
|
|
EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
|
90 |
|
|
EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
|
94 |
|
|
EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
|
95 |
|
|
EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
|
96 |
|
|
EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
|
97 |
|
|
EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
|
101 |
|
|
EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
|
102 |
|
|
EXPECT_EQ("chapter 11 section 1",
|
103 |
|
|
ConvertIdentifierNameToWords("_Chapter11Section_1_"));
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
TEST(PointeeOfTest, WorksForSmartPointers) {
|
107 |
|
|
CompileAssertTypesEqual<const char,
|
108 |
|
|
PointeeOf<internal::linked_ptr<const char> >::type>();
|
109 |
|
|
#if GTEST_HAS_STD_UNIQUE_PTR_
|
110 |
|
|
CompileAssertTypesEqual<int, PointeeOf<std::unique_ptr<int> >::type>();
|
111 |
|
|
#endif // GTEST_HAS_STD_UNIQUE_PTR_
|
112 |
|
|
#if GTEST_HAS_STD_SHARED_PTR_
|
113 |
|
|
CompileAssertTypesEqual<std::string,
|
114 |
|
|
PointeeOf<std::shared_ptr<std::string> >::type>();
|
115 |
|
|
#endif // GTEST_HAS_STD_SHARED_PTR_
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
TEST(PointeeOfTest, WorksForRawPointers) {
|
119 |
|
|
CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
|
120 |
|
|
CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
|
121 |
|
|
CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
TEST(GetRawPointerTest, WorksForSmartPointers) {
|
125 |
|
|
#if GTEST_HAS_STD_UNIQUE_PTR_
|
126 |
|
|
const char* const raw_p1 = new const char('a'); // NOLINT
|
127 |
|
|
const std::unique_ptr<const char> p1(raw_p1);
|
128 |
|
|
EXPECT_EQ(raw_p1, GetRawPointer(p1));
|
129 |
|
|
#endif // GTEST_HAS_STD_UNIQUE_PTR_
|
130 |
|
|
#if GTEST_HAS_STD_SHARED_PTR_
|
131 |
|
|
double* const raw_p2 = new double(2.5); // NOLINT
|
132 |
|
|
const std::shared_ptr<double> p2(raw_p2);
|
133 |
|
|
EXPECT_EQ(raw_p2, GetRawPointer(p2));
|
134 |
|
|
#endif // GTEST_HAS_STD_SHARED_PTR_
|
135 |
|
|
|
136 |
|
|
const char* const raw_p4 = new const char('a'); // NOLINT
|
137 |
|
|
const internal::linked_ptr<const char> p4(raw_p4);
|
138 |
|
|
EXPECT_EQ(raw_p4, GetRawPointer(p4));
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
TEST(GetRawPointerTest, WorksForRawPointers) {
|
142 |
|
|
int* p = NULL;
|
143 |
|
|
// Don't use EXPECT_EQ as no NULL-testing magic on Symbian.
|
144 |
|
|
EXPECT_TRUE(NULL == GetRawPointer(p));
|
145 |
|
|
int n = 1;
|
146 |
|
|
EXPECT_EQ(&n, GetRawPointer(&n));
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
// Tests KindOf<T>.
|
150 |
|
|
|
151 |
|
|
class Base {};
|
152 |
|
|
class Derived : public Base {};
|
153 |
|
|
|
154 |
|
|
TEST(KindOfTest, Bool) {
|
155 |
|
|
EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
TEST(KindOfTest, Integer) {
|
159 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT
|
160 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT
|
161 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT
|
162 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT
|
163 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT
|
164 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT
|
165 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT
|
166 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT
|
167 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT
|
168 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
|
169 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT
|
170 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT
|
171 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
|
172 |
|
|
#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
|
173 |
|
|
// ssize_t is not defined on Windows and possibly some other OSes.
|
174 |
|
|
EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
|
175 |
|
|
#endif
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
TEST(KindOfTest, FloatingPoint) {
|
179 |
|
|
EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT
|
180 |
|
|
EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT
|
181 |
|
|
EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
TEST(KindOfTest, Other) {
|
185 |
|
|
EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT
|
186 |
|
|
EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT
|
187 |
|
|
EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// Tests LosslessArithmeticConvertible<T, U>.
|
191 |
|
|
|
192 |
|
|
TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
|
193 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
|
197 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
|
198 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
|
199 |
|
|
EXPECT_TRUE(
|
200 |
|
|
(LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
|
204 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
|
205 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
|
209 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
|
210 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
|
214 |
|
|
// Unsigned => larger signed is fine.
|
215 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
|
216 |
|
|
|
217 |
|
|
// Unsigned => larger unsigned is fine.
|
218 |
|
|
EXPECT_TRUE(
|
219 |
|
|
(LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
|
220 |
|
|
|
221 |
|
|
// Signed => unsigned is not fine.
|
222 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
|
223 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<
|
224 |
|
|
signed char, unsigned int>::value)); // NOLINT
|
225 |
|
|
|
226 |
|
|
// Same size and same signedness: fine too.
|
227 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<
|
228 |
|
|
unsigned char, unsigned char>::value));
|
229 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
|
230 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
|
231 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<
|
232 |
|
|
unsigned long, unsigned long>::value)); // NOLINT
|
233 |
|
|
|
234 |
|
|
// Same size, different signedness: not fine.
|
235 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<
|
236 |
|
|
unsigned char, signed char>::value));
|
237 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
|
238 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
|
239 |
|
|
|
240 |
|
|
// Larger size => smaller size is not fine.
|
241 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT
|
242 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
|
243 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
|
247 |
|
|
// Integers cannot be losslessly converted to floating-points, as
|
248 |
|
|
// the format of the latter is implementation-defined.
|
249 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
|
250 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
|
251 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<
|
252 |
|
|
short, long double>::value)); // NOLINT
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
|
256 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
|
257 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
|
261 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT
|
262 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
|
263 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
|
267 |
|
|
// Smaller size => larger size is fine.
|
268 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
|
269 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
|
270 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
|
271 |
|
|
|
272 |
|
|
// Same size: fine.
|
273 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
|
274 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
|
275 |
|
|
|
276 |
|
|
// Larger size => smaller size is not fine.
|
277 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
|
278 |
|
|
GTEST_INTENTIONAL_CONST_COND_PUSH_()
|
279 |
|
|
if (sizeof(double) == sizeof(long double)) { // NOLINT
|
280 |
|
|
GTEST_INTENTIONAL_CONST_COND_POP_()
|
281 |
|
|
// In some implementations (e.g. MSVC), double and long double
|
282 |
|
|
// have the same size.
|
283 |
|
|
EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
|
284 |
|
|
} else {
|
285 |
|
|
EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
|
289 |
|
|
// Tests the TupleMatches() template function.
|
290 |
|
|
|
291 |
|
|
TEST(TupleMatchesTest, WorksForSize0) {
|
292 |
|
|
tuple<> matchers;
|
293 |
|
|
tuple<> values;
|
294 |
|
|
|
295 |
|
|
EXPECT_TRUE(TupleMatches(matchers, values));
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
TEST(TupleMatchesTest, WorksForSize1) {
|
299 |
|
|
tuple<Matcher<int> > matchers(Eq(1));
|
300 |
|
|
tuple<int> values1(1),
|
301 |
|
|
values2(2);
|
302 |
|
|
|
303 |
|
|
EXPECT_TRUE(TupleMatches(matchers, values1));
|
304 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values2));
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
TEST(TupleMatchesTest, WorksForSize2) {
|
308 |
|
|
tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
|
309 |
|
|
tuple<int, char> values1(1, 'a'),
|
310 |
|
|
values2(1, 'b'),
|
311 |
|
|
values3(2, 'a'),
|
312 |
|
|
values4(2, 'b');
|
313 |
|
|
|
314 |
|
|
EXPECT_TRUE(TupleMatches(matchers, values1));
|
315 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values2));
|
316 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values3));
|
317 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values4));
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
TEST(TupleMatchesTest, WorksForSize5) {
|
321 |
|
|
tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>, // NOLINT
|
322 |
|
|
Matcher<string> >
|
323 |
|
|
matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
|
324 |
|
|
tuple<int, char, bool, long, string> // NOLINT
|
325 |
|
|
values1(1, 'a', true, 2L, "hi"),
|
326 |
|
|
values2(1, 'a', true, 2L, "hello"),
|
327 |
|
|
values3(2, 'a', true, 2L, "hi");
|
328 |
|
|
|
329 |
|
|
EXPECT_TRUE(TupleMatches(matchers, values1));
|
330 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values2));
|
331 |
|
|
EXPECT_FALSE(TupleMatches(matchers, values3));
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
// Tests that Assert(true, ...) succeeds.
|
335 |
|
|
TEST(AssertTest, SucceedsOnTrue) {
|
336 |
|
|
Assert(true, __FILE__, __LINE__, "This should succeed.");
|
337 |
|
|
Assert(true, __FILE__, __LINE__); // This should succeed too.
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
// Tests that Assert(false, ...) generates a fatal failure.
|
341 |
|
|
TEST(AssertTest, FailsFatallyOnFalse) {
|
342 |
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
343 |
|
|
Assert(false, __FILE__, __LINE__, "This should fail.");
|
344 |
|
|
}, "");
|
345 |
|
|
|
346 |
|
|
EXPECT_DEATH_IF_SUPPORTED({
|
347 |
|
|
Assert(false, __FILE__, __LINE__);
|
348 |
|
|
}, "");
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
// Tests that Expect(true, ...) succeeds.
|
352 |
|
|
TEST(ExpectTest, SucceedsOnTrue) {
|
353 |
|
|
Expect(true, __FILE__, __LINE__, "This should succeed.");
|
354 |
|
|
Expect(true, __FILE__, __LINE__); // This should succeed too.
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
// Tests that Expect(false, ...) generates a non-fatal failure.
|
358 |
|
|
TEST(ExpectTest, FailsNonfatallyOnFalse) {
|
359 |
|
|
EXPECT_NONFATAL_FAILURE({ // NOLINT
|
360 |
|
|
Expect(false, __FILE__, __LINE__, "This should fail.");
|
361 |
|
|
}, "This should fail");
|
362 |
|
|
|
363 |
|
|
EXPECT_NONFATAL_FAILURE({ // NOLINT
|
364 |
|
|
Expect(false, __FILE__, __LINE__);
|
365 |
|
|
}, "Expectation failed");
|
366 |
|
|
}
|
367 |
|
|
|
368 |
|
|
// Tests LogIsVisible().
|
369 |
|
|
|
370 |
|
|
class LogIsVisibleTest : public ::testing::Test {
|
371 |
|
|
protected:
|
372 |
|
|
virtual void SetUp() {
|
373 |
|
|
original_verbose_ = GMOCK_FLAG(verbose);
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
|
377 |
|
|
|
378 |
|
|
string original_verbose_;
|
379 |
|
|
};
|
380 |
|
|
|
381 |
|
|
TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
|
382 |
|
|
GMOCK_FLAG(verbose) = kInfoVerbosity;
|
383 |
|
|
EXPECT_TRUE(LogIsVisible(kInfo));
|
384 |
|
|
EXPECT_TRUE(LogIsVisible(kWarning));
|
385 |
|
|
}
|
386 |
|
|
|
387 |
|
|
TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
|
388 |
|
|
GMOCK_FLAG(verbose) = kErrorVerbosity;
|
389 |
|
|
EXPECT_FALSE(LogIsVisible(kInfo));
|
390 |
|
|
EXPECT_FALSE(LogIsVisible(kWarning));
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
|
394 |
|
|
GMOCK_FLAG(verbose) = kWarningVerbosity;
|
395 |
|
|
EXPECT_FALSE(LogIsVisible(kInfo));
|
396 |
|
|
EXPECT_TRUE(LogIsVisible(kWarning));
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
400 |
|
|
|
401 |
|
|
// Tests the Log() function.
|
402 |
|
|
|
403 |
|
|
// Verifies that Log() behaves correctly for the given verbosity level
|
404 |
|
|
// and log severity.
|
405 |
|
|
void TestLogWithSeverity(const string& verbosity, LogSeverity severity,
|
406 |
|
|
bool should_print) {
|
407 |
|
|
const string old_flag = GMOCK_FLAG(verbose);
|
408 |
|
|
GMOCK_FLAG(verbose) = verbosity;
|
409 |
|
|
CaptureStdout();
|
410 |
|
|
Log(severity, "Test log.\n", 0);
|
411 |
|
|
if (should_print) {
|
412 |
|
|
EXPECT_THAT(GetCapturedStdout().c_str(),
|
413 |
|
|
ContainsRegex(
|
414 |
|
|
severity == kWarning ?
|
415 |
|
|
"^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
|
416 |
|
|
"^\nTest log\\.\nStack trace:\n"));
|
417 |
|
|
} else {
|
418 |
|
|
EXPECT_STREQ("", GetCapturedStdout().c_str());
|
419 |
|
|
}
|
420 |
|
|
GMOCK_FLAG(verbose) = old_flag;
|
421 |
|
|
}
|
422 |
|
|
|
423 |
|
|
// Tests that when the stack_frames_to_skip parameter is negative,
|
424 |
|
|
// Log() doesn't include the stack trace in the output.
|
425 |
|
|
TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
|
426 |
|
|
const string saved_flag = GMOCK_FLAG(verbose);
|
427 |
|
|
GMOCK_FLAG(verbose) = kInfoVerbosity;
|
428 |
|
|
CaptureStdout();
|
429 |
|
|
Log(kInfo, "Test log.\n", -1);
|
430 |
|
|
EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
|
431 |
|
|
GMOCK_FLAG(verbose) = saved_flag;
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
struct MockStackTraceGetter : testing::internal::OsStackTraceGetterInterface {
|
435 |
|
|
virtual string CurrentStackTrace(int max_depth, int skip_count) {
|
436 |
|
|
return (testing::Message() << max_depth << "::" << skip_count << "\n")
|
437 |
|
|
.GetString();
|
438 |
|
|
}
|
439 |
|
|
virtual void UponLeavingGTest() {}
|
440 |
|
|
};
|
441 |
|
|
|
442 |
|
|
// Tests that in opt mode, a positive stack_frames_to_skip argument is
|
443 |
|
|
// treated as 0.
|
444 |
|
|
TEST(LogTest, NoSkippingStackFrameInOptMode) {
|
445 |
|
|
MockStackTraceGetter* mock_os_stack_trace_getter = new MockStackTraceGetter;
|
446 |
|
|
GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter);
|
447 |
|
|
|
448 |
|
|
CaptureStdout();
|
449 |
|
|
Log(kWarning, "Test log.\n", 100);
|
450 |
|
|
const string log = GetCapturedStdout();
|
451 |
|
|
|
452 |
|
|
string expected_trace =
|
453 |
|
|
(testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
|
454 |
|
|
string expected_message =
|
455 |
|
|
"\nGMOCK WARNING:\n"
|
456 |
|
|
"Test log.\n"
|
457 |
|
|
"Stack trace:\n" +
|
458 |
|
|
expected_trace;
|
459 |
|
|
EXPECT_THAT(log, HasSubstr(expected_message));
|
460 |
|
|
int skip_count = atoi(log.substr(expected_message.size()).c_str());
|
461 |
|
|
|
462 |
|
|
# if defined(NDEBUG)
|
463 |
|
|
// In opt mode, no stack frame should be skipped.
|
464 |
|
|
const int expected_skip_count = 0;
|
465 |
|
|
# else
|
466 |
|
|
// In dbg mode, the stack frames should be skipped.
|
467 |
|
|
const int expected_skip_count = 100;
|
468 |
|
|
# endif
|
469 |
|
|
|
470 |
|
|
// Note that each inner implementation layer will +1 the number to remove
|
471 |
|
|
// itself from the trace. This means that the value is a little higher than
|
472 |
|
|
// expected, but close enough.
|
473 |
|
|
EXPECT_THAT(skip_count,
|
474 |
|
|
AllOf(Ge(expected_skip_count), Le(expected_skip_count + 10)));
|
475 |
|
|
|
476 |
|
|
// Restores the default OS stack trace getter.
|
477 |
|
|
GetUnitTestImpl()->set_os_stack_trace_getter(NULL);
|
478 |
|
|
}
|
479 |
|
|
|
480 |
|
|
// Tests that all logs are printed when the value of the
|
481 |
|
|
// --gmock_verbose flag is "info".
|
482 |
|
|
TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
|
483 |
|
|
TestLogWithSeverity(kInfoVerbosity, kInfo, true);
|
484 |
|
|
TestLogWithSeverity(kInfoVerbosity, kWarning, true);
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
// Tests that only warnings are printed when the value of the
|
488 |
|
|
// --gmock_verbose flag is "warning".
|
489 |
|
|
TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
|
490 |
|
|
TestLogWithSeverity(kWarningVerbosity, kInfo, false);
|
491 |
|
|
TestLogWithSeverity(kWarningVerbosity, kWarning, true);
|
492 |
|
|
}
|
493 |
|
|
|
494 |
|
|
// Tests that no logs are printed when the value of the
|
495 |
|
|
// --gmock_verbose flag is "error".
|
496 |
|
|
TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
|
497 |
|
|
TestLogWithSeverity(kErrorVerbosity, kInfo, false);
|
498 |
|
|
TestLogWithSeverity(kErrorVerbosity, kWarning, false);
|
499 |
|
|
}
|
500 |
|
|
|
501 |
|
|
// Tests that only warnings are printed when the value of the
|
502 |
|
|
// --gmock_verbose flag is invalid.
|
503 |
|
|
TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
|
504 |
|
|
TestLogWithSeverity("invalid", kInfo, false);
|
505 |
|
|
TestLogWithSeverity("invalid", kWarning, true);
|
506 |
|
|
}
|
507 |
|
|
|
508 |
|
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
509 |
|
|
|
510 |
|
|
TEST(TypeTraitsTest, true_type) {
|
511 |
|
|
EXPECT_TRUE(true_type::value);
|
512 |
|
|
}
|
513 |
|
|
|
514 |
|
|
TEST(TypeTraitsTest, false_type) {
|
515 |
|
|
EXPECT_FALSE(false_type::value);
|
516 |
|
|
}
|
517 |
|
|
|
518 |
|
|
TEST(TypeTraitsTest, is_reference) {
|
519 |
|
|
EXPECT_FALSE(is_reference<int>::value);
|
520 |
|
|
EXPECT_FALSE(is_reference<char*>::value);
|
521 |
|
|
EXPECT_TRUE(is_reference<const int&>::value);
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
TEST(TypeTraitsTest, is_pointer) {
|
525 |
|
|
EXPECT_FALSE(is_pointer<int>::value);
|
526 |
|
|
EXPECT_FALSE(is_pointer<char&>::value);
|
527 |
|
|
EXPECT_TRUE(is_pointer<const int*>::value);
|
528 |
|
|
}
|
529 |
|
|
|
530 |
|
|
TEST(TypeTraitsTest, type_equals) {
|
531 |
|
|
EXPECT_FALSE((type_equals<int, const int>::value));
|
532 |
|
|
EXPECT_FALSE((type_equals<int, int&>::value));
|
533 |
|
|
EXPECT_FALSE((type_equals<int, double>::value));
|
534 |
|
|
EXPECT_TRUE((type_equals<char, char>::value));
|
535 |
|
|
}
|
536 |
|
|
|
537 |
|
|
TEST(TypeTraitsTest, remove_reference) {
|
538 |
|
|
EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value));
|
539 |
|
|
EXPECT_TRUE((type_equals<const int,
|
540 |
|
|
remove_reference<const int&>::type>::value));
|
541 |
|
|
EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value));
|
542 |
|
|
EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
|
543 |
|
|
}
|
544 |
|
|
|
545 |
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
546 |
|
|
|
547 |
|
|
// Verifies that Log() behaves correctly for the given verbosity level
|
548 |
|
|
// and log severity.
|
549 |
|
|
std::string GrabOutput(void(*logger)(), const char* verbosity) {
|
550 |
|
|
const string saved_flag = GMOCK_FLAG(verbose);
|
551 |
|
|
GMOCK_FLAG(verbose) = verbosity;
|
552 |
|
|
CaptureStdout();
|
553 |
|
|
logger();
|
554 |
|
|
GMOCK_FLAG(verbose) = saved_flag;
|
555 |
|
|
return GetCapturedStdout();
|
556 |
|
|
}
|
557 |
|
|
|
558 |
|
|
class DummyMock {
|
559 |
|
|
public:
|
560 |
|
|
MOCK_METHOD0(TestMethod, void());
|
561 |
|
|
MOCK_METHOD1(TestMethodArg, void(int dummy));
|
562 |
|
|
};
|
563 |
|
|
|
564 |
|
|
void ExpectCallLogger() {
|
565 |
|
|
DummyMock mock;
|
566 |
|
|
EXPECT_CALL(mock, TestMethod());
|
567 |
|
|
mock.TestMethod();
|
568 |
|
|
};
|
569 |
|
|
|
570 |
|
|
// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
|
571 |
|
|
TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
|
572 |
|
|
EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)),
|
573 |
|
|
HasSubstr("EXPECT_CALL(mock, TestMethod())"));
|
574 |
|
|
}
|
575 |
|
|
|
576 |
|
|
// Verifies that EXPECT_CALL doesn't log
|
577 |
|
|
// if the --gmock_verbose flag is set to "warning".
|
578 |
|
|
TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
|
579 |
|
|
EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
|
580 |
|
|
}
|
581 |
|
|
|
582 |
|
|
// Verifies that EXPECT_CALL doesn't log
|
583 |
|
|
// if the --gmock_verbose flag is set to "error".
|
584 |
|
|
TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
|
585 |
|
|
EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
void OnCallLogger() {
|
589 |
|
|
DummyMock mock;
|
590 |
|
|
ON_CALL(mock, TestMethod());
|
591 |
|
|
};
|
592 |
|
|
|
593 |
|
|
// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
|
594 |
|
|
TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
|
595 |
|
|
EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)),
|
596 |
|
|
HasSubstr("ON_CALL(mock, TestMethod())"));
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
// Verifies that ON_CALL doesn't log
|
600 |
|
|
// if the --gmock_verbose flag is set to "warning".
|
601 |
|
|
TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
|
602 |
|
|
EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
|
603 |
|
|
}
|
604 |
|
|
|
605 |
|
|
// Verifies that ON_CALL doesn't log if
|
606 |
|
|
// the --gmock_verbose flag is set to "error".
|
607 |
|
|
TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
|
608 |
|
|
EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
|
609 |
|
|
}
|
610 |
|
|
|
611 |
|
|
void OnCallAnyArgumentLogger() {
|
612 |
|
|
DummyMock mock;
|
613 |
|
|
ON_CALL(mock, TestMethodArg(_));
|
614 |
|
|
}
|
615 |
|
|
|
616 |
|
|
// Verifies that ON_CALL prints provided _ argument.
|
617 |
|
|
TEST(OnCallTest, LogsAnythingArgument) {
|
618 |
|
|
EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)),
|
619 |
|
|
HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
623 |
|
|
|
624 |
|
|
// Tests StlContainerView.
|
625 |
|
|
|
626 |
|
|
TEST(StlContainerViewTest, WorksForStlContainer) {
|
627 |
|
|
StaticAssertTypeEq<std::vector<int>,
|
628 |
|
|
StlContainerView<std::vector<int> >::type>();
|
629 |
|
|
StaticAssertTypeEq<const std::vector<double>&,
|
630 |
|
|
StlContainerView<std::vector<double> >::const_reference>();
|
631 |
|
|
|
632 |
|
|
typedef std::vector<char> Chars;
|
633 |
|
|
Chars v1;
|
634 |
|
|
const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
|
635 |
|
|
EXPECT_EQ(&v1, &v2);
|
636 |
|
|
|
637 |
|
|
v1.push_back('a');
|
638 |
|
|
Chars v3 = StlContainerView<Chars>::Copy(v1);
|
639 |
|
|
EXPECT_THAT(v3, Eq(v3));
|
640 |
|
|
}
|
641 |
|
|
|
642 |
|
|
TEST(StlContainerViewTest, WorksForStaticNativeArray) {
|
643 |
|
|
StaticAssertTypeEq<NativeArray<int>,
|
644 |
|
|
StlContainerView<int[3]>::type>();
|
645 |
|
|
StaticAssertTypeEq<NativeArray<double>,
|
646 |
|
|
StlContainerView<const double[4]>::type>();
|
647 |
|
|
StaticAssertTypeEq<NativeArray<char[3]>,
|
648 |
|
|
StlContainerView<const char[2][3]>::type>();
|
649 |
|
|
|
650 |
|
|
StaticAssertTypeEq<const NativeArray<int>,
|
651 |
|
|
StlContainerView<int[2]>::const_reference>();
|
652 |
|
|
|
653 |
|
|
int a1[3] = { 0, 1, 2 };
|
654 |
|
|
NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
|
655 |
|
|
EXPECT_EQ(3U, a2.size());
|
656 |
|
|
EXPECT_EQ(a1, a2.begin());
|
657 |
|
|
|
658 |
|
|
const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
|
659 |
|
|
ASSERT_EQ(3U, a3.size());
|
660 |
|
|
EXPECT_EQ(0, a3.begin()[0]);
|
661 |
|
|
EXPECT_EQ(1, a3.begin()[1]);
|
662 |
|
|
EXPECT_EQ(2, a3.begin()[2]);
|
663 |
|
|
|
664 |
|
|
// Makes sure a1 and a3 aren't aliases.
|
665 |
|
|
a1[0] = 3;
|
666 |
|
|
EXPECT_EQ(0, a3.begin()[0]);
|
667 |
|
|
}
|
668 |
|
|
|
669 |
|
|
TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
|
670 |
|
|
StaticAssertTypeEq<NativeArray<int>,
|
671 |
|
|
StlContainerView<tuple<const int*, size_t> >::type>();
|
672 |
|
|
StaticAssertTypeEq<NativeArray<double>,
|
673 |
|
|
StlContainerView<tuple<linked_ptr<double>, int> >::type>();
|
674 |
|
|
|
675 |
|
|
StaticAssertTypeEq<const NativeArray<int>,
|
676 |
|
|
StlContainerView<tuple<const int*, int> >::const_reference>();
|
677 |
|
|
|
678 |
|
|
int a1[3] = { 0, 1, 2 };
|
679 |
|
|
const int* const p1 = a1;
|
680 |
|
|
NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
|
681 |
|
|
ConstReference(make_tuple(p1, 3));
|
682 |
|
|
EXPECT_EQ(3U, a2.size());
|
683 |
|
|
EXPECT_EQ(a1, a2.begin());
|
684 |
|
|
|
685 |
|
|
const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
|
686 |
|
|
Copy(make_tuple(static_cast<int*>(a1), 3));
|
687 |
|
|
ASSERT_EQ(3U, a3.size());
|
688 |
|
|
EXPECT_EQ(0, a3.begin()[0]);
|
689 |
|
|
EXPECT_EQ(1, a3.begin()[1]);
|
690 |
|
|
EXPECT_EQ(2, a3.begin()[2]);
|
691 |
|
|
|
692 |
|
|
// Makes sure a1 and a3 aren't aliases.
|
693 |
|
|
a1[0] = 3;
|
694 |
|
|
EXPECT_EQ(0, a3.begin()[0]);
|
695 |
|
|
}
|
696 |
|
|
|
697 |
|
|
} // namespace
|
698 |
|
|
} // namespace internal
|
699 |
|
|
} // namespace testing
|