blob: 67b7077f14cb701583beb85cdc08ac98612dcead [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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.
Gennadiy Civila3c0dd02018-08-14 14:04:07 -040029
shiqiane35fdd92008-12-10 05:08:54 +000030
31// Google Mock - a framework for writing C++ mock classes.
32//
33// This file tests the internal utilities.
34
zhanyong.wan53e08c42010-09-14 05:38:21 +000035#include "gmock/internal/gmock-internal-utils.h"
kuzkrydb1b7392019-08-23 11:57:56 -040036
zhanyong.wan16cf4732009-05-14 20:55:30 +000037#include <stdlib.h>
kuzkrydb1b7392019-08-23 11:57:56 -040038
shiqiane35fdd92008-12-10 05:08:54 +000039#include <map>
Billy Donahueb78f8582015-09-08 02:46:25 -040040#include <memory>
shiqiane35fdd92008-12-10 05:08:54 +000041#include <sstream>
kuzkrydb1b7392019-08-23 11:57:56 -040042#include <string>
Krystian Kuzniarekec49fbc2019-08-13 22:30:12 +020043#include <type_traits>
shiqiane35fdd92008-12-10 05:08:54 +000044#include <vector>
kuzkrydb1b7392019-08-23 11:57:56 -040045
zhanyong.wan53e08c42010-09-14 05:38:21 +000046#include "gmock/gmock.h"
47#include "gmock/internal/gmock-port.h"
zhanyong.wan53e08c42010-09-14 05:38:21 +000048#include "gtest/gtest-spi.h"
kuzkrydb1b7392019-08-23 11:57:56 -040049#include "gtest/gtest.h"
Gennadiy Civilb7c56832018-03-22 15:35:37 -040050
51// Indicates that this translation unit is part of Google Test's
52// implementation. It must come before gtest-internal-inl.h is
53// included, or there will be a compiler error. This trick is to
54// prevent a user from accidentally including gtest-internal-inl.h in
55// their code.
56#define GTEST_IMPLEMENTATION_ 1
kosake83085a2015-07-28 00:12:51 +000057#include "src/gtest-internal-inl.h"
Gennadiy Civilb7c56832018-03-22 15:35:37 -040058#undef GTEST_IMPLEMENTATION_
kosake83085a2015-07-28 00:12:51 +000059
zhanyong.wan16cf4732009-05-14 20:55:30 +000060#if GTEST_OS_CYGWIN
zhanyong.wan658ac0b2011-02-24 07:29:13 +000061# include <sys/types.h> // For ssize_t. NOLINT
zhanyong.wan16cf4732009-05-14 20:55:30 +000062#endif
63
zhanyong.wan0ea67f82009-08-14 04:50:02 +000064namespace proto2 {
65class Message;
66} // namespace proto2
67
shiqiane35fdd92008-12-10 05:08:54 +000068namespace testing {
69namespace internal {
70
71namespace {
72
Gennadiy Civilb7c56832018-03-22 15:35:37 -040073TEST(JoinAsTupleTest, JoinsEmptyTuple) {
74 EXPECT_EQ("", JoinAsTuple(Strings()));
75}
76
77TEST(JoinAsTupleTest, JoinsOneTuple) {
78 const char* fields[] = {"1"};
79 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
80}
81
82TEST(JoinAsTupleTest, JoinsTwoTuple) {
83 const char* fields[] = {"1", "a"};
84 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
85}
86
87TEST(JoinAsTupleTest, JoinsTenTuple) {
88 const char* fields[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
89 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
90 JoinAsTuple(Strings(fields, fields + 10)));
91}
92
zhanyong.wance198ff2009-02-12 01:34:27 +000093TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
94 EXPECT_EQ("", ConvertIdentifierNameToWords(""));
95 EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
96 EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
97}
98
99TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
100 EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
101 EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
102 EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
103 EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
104}
105
106TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
107 EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
108 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
109 EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_"));
110 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
111 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
112}
113
114TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
115 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
116 EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
117 EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
118 EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
119}
120
121TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
122 EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
123 EXPECT_EQ("chapter 11 section 1",
124 ConvertIdentifierNameToWords("_Chapter11Section_1_"));
125}
126
shiqiane35fdd92008-12-10 05:08:54 +0000127TEST(PointeeOfTest, WorksForSmartPointers) {
kosakcac6c1b2015-07-27 23:51:16 +0000128 CompileAssertTypesEqual<int, PointeeOf<std::unique_ptr<int> >::type>();
kosakcac6c1b2015-07-27 23:51:16 +0000129 CompileAssertTypesEqual<std::string,
130 PointeeOf<std::shared_ptr<std::string> >::type>();
shiqiane35fdd92008-12-10 05:08:54 +0000131}
132
133TEST(PointeeOfTest, WorksForRawPointers) {
134 CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
135 CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
136 CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
137}
138
139TEST(GetRawPointerTest, WorksForSmartPointers) {
kosakcac6c1b2015-07-27 23:51:16 +0000140 const char* const raw_p1 = new const char('a'); // NOLINT
141 const std::unique_ptr<const char> p1(raw_p1);
142 EXPECT_EQ(raw_p1, GetRawPointer(p1));
kosakcac6c1b2015-07-27 23:51:16 +0000143 double* const raw_p2 = new double(2.5); // NOLINT
144 const std::shared_ptr<double> p2(raw_p2);
145 EXPECT_EQ(raw_p2, GetRawPointer(p2));
shiqiane35fdd92008-12-10 05:08:54 +0000146}
147
148TEST(GetRawPointerTest, WorksForRawPointers) {
Abseil Team4bb49ed2018-10-04 18:28:05 -0400149 int* p = nullptr;
Abseil Team4bb49ed2018-10-04 18:28:05 -0400150 EXPECT_TRUE(nullptr == GetRawPointer(p));
shiqiane35fdd92008-12-10 05:08:54 +0000151 int n = 1;
152 EXPECT_EQ(&n, GetRawPointer(&n));
153}
154
zhanyong.wan02f71062010-05-10 17:14:29 +0000155// Tests KindOf<T>.
156
shiqiane35fdd92008-12-10 05:08:54 +0000157class Base {};
158class Derived : public Base {};
159
zhanyong.wan16cf4732009-05-14 20:55:30 +0000160TEST(KindOfTest, Bool) {
161 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
162}
163
164TEST(KindOfTest, Integer) {
165 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT
166 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT
167 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT
168 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT
169 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT
170 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT
171 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT
172 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT
173 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT
174 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
175 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64)); // NOLINT
176 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64)); // NOLINT
177 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
178#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
179 // ssize_t is not defined on Windows and possibly some other OSes.
180 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
181#endif
182}
183
184TEST(KindOfTest, FloatingPoint) {
185 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT
186 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT
187 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT
188}
189
190TEST(KindOfTest, Other) {
191 EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT
192 EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT
193 EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT
194}
195
196// Tests LosslessArithmeticConvertible<T, U>.
197
198TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
199 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
200}
201
202TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
203 EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
204 EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
205 EXPECT_TRUE(
206 (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT
207}
208
209TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
210 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
211 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
212}
213
214TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
215 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
216 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
217}
218
219TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
220 // Unsigned => larger signed is fine.
221 EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
222
223 // Unsigned => larger unsigned is fine.
224 EXPECT_TRUE(
225 (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
226
227 // Signed => unsigned is not fine.
228 EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
229 EXPECT_FALSE((LosslessArithmeticConvertible<
230 signed char, unsigned int>::value)); // NOLINT
231
232 // Same size and same signedness: fine too.
233 EXPECT_TRUE((LosslessArithmeticConvertible<
234 unsigned char, unsigned char>::value));
235 EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
236 EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
237 EXPECT_TRUE((LosslessArithmeticConvertible<
238 unsigned long, unsigned long>::value)); // NOLINT
239
240 // Same size, different signedness: not fine.
241 EXPECT_FALSE((LosslessArithmeticConvertible<
242 unsigned char, signed char>::value));
243 EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
244 EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
245
246 // Larger size => smaller size is not fine.
247 EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT
248 EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
249 EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
250}
251
252TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
253 // Integers cannot be losslessly converted to floating-points, as
254 // the format of the latter is implementation-defined.
255 EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
256 EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
257 EXPECT_FALSE((LosslessArithmeticConvertible<
258 short, long double>::value)); // NOLINT
259}
260
261TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
262 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
263 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
264}
265
266TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
267 EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT
268 EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
269 EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
270}
271
272TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
273 // Smaller size => larger size is fine.
274 EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
275 EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
276 EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
277
278 // Same size: fine.
279 EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
280 EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
281
282 // Larger size => smaller size is not fine.
283 EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
billydonahue1f5fdea2014-05-19 17:54:51 +0000284 GTEST_INTENTIONAL_CONST_COND_PUSH_()
zhanyong.wan16cf4732009-05-14 20:55:30 +0000285 if (sizeof(double) == sizeof(long double)) { // NOLINT
billydonahue1f5fdea2014-05-19 17:54:51 +0000286 GTEST_INTENTIONAL_CONST_COND_POP_()
zhanyong.wan16cf4732009-05-14 20:55:30 +0000287 // In some implementations (e.g. MSVC), double and long double
288 // have the same size.
289 EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
290 } else {
291 EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
292 }
293}
294
shiqiane35fdd92008-12-10 05:08:54 +0000295// Tests the TupleMatches() template function.
296
297TEST(TupleMatchesTest, WorksForSize0) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400298 std::tuple<> matchers;
299 std::tuple<> values;
shiqiane35fdd92008-12-10 05:08:54 +0000300
301 EXPECT_TRUE(TupleMatches(matchers, values));
302}
303
304TEST(TupleMatchesTest, WorksForSize1) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400305 std::tuple<Matcher<int> > matchers(Eq(1));
306 std::tuple<int> values1(1), values2(2);
shiqiane35fdd92008-12-10 05:08:54 +0000307
308 EXPECT_TRUE(TupleMatches(matchers, values1));
309 EXPECT_FALSE(TupleMatches(matchers, values2));
310}
311
312TEST(TupleMatchesTest, WorksForSize2) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400313 std::tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
314 std::tuple<int, char> values1(1, 'a'), values2(1, 'b'), values3(2, 'a'),
shiqiane35fdd92008-12-10 05:08:54 +0000315 values4(2, 'b');
316
317 EXPECT_TRUE(TupleMatches(matchers, values1));
318 EXPECT_FALSE(TupleMatches(matchers, values2));
319 EXPECT_FALSE(TupleMatches(matchers, values3));
320 EXPECT_FALSE(TupleMatches(matchers, values4));
321}
322
323TEST(TupleMatchesTest, WorksForSize5) {
Abseil Team7d3b73c2018-10-09 14:50:26 -0400324 std::tuple<Matcher<int>, Matcher<char>, Matcher<bool>,
325 Matcher<long>, // NOLINT
326 Matcher<std::string> >
shiqiane35fdd92008-12-10 05:08:54 +0000327 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
Abseil Team7d3b73c2018-10-09 14:50:26 -0400328 std::tuple<int, char, bool, long, std::string> // NOLINT
Nico Weber09fd5b32017-05-15 17:07:03 -0400329 values1(1, 'a', true, 2L, "hi"), values2(1, 'a', true, 2L, "hello"),
shiqiane35fdd92008-12-10 05:08:54 +0000330 values3(2, 'a', true, 2L, "hi");
331
332 EXPECT_TRUE(TupleMatches(matchers, values1));
333 EXPECT_FALSE(TupleMatches(matchers, values2));
334 EXPECT_FALSE(TupleMatches(matchers, values3));
335}
336
337// Tests that Assert(true, ...) succeeds.
338TEST(AssertTest, SucceedsOnTrue) {
339 Assert(true, __FILE__, __LINE__, "This should succeed.");
340 Assert(true, __FILE__, __LINE__); // This should succeed too.
341}
342
shiqiane35fdd92008-12-10 05:08:54 +0000343// Tests that Assert(false, ...) generates a fatal failure.
344TEST(AssertTest, FailsFatallyOnFalse) {
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000345 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000346 Assert(false, __FILE__, __LINE__, "This should fail.");
347 }, "");
348
zhanyong.wan04d6ed82009-09-11 07:01:08 +0000349 EXPECT_DEATH_IF_SUPPORTED({
shiqiane35fdd92008-12-10 05:08:54 +0000350 Assert(false, __FILE__, __LINE__);
351 }, "");
352}
353
shiqiane35fdd92008-12-10 05:08:54 +0000354// Tests that Expect(true, ...) succeeds.
355TEST(ExpectTest, SucceedsOnTrue) {
356 Expect(true, __FILE__, __LINE__, "This should succeed.");
357 Expect(true, __FILE__, __LINE__); // This should succeed too.
358}
359
360// Tests that Expect(false, ...) generates a non-fatal failure.
361TEST(ExpectTest, FailsNonfatallyOnFalse) {
362 EXPECT_NONFATAL_FAILURE({ // NOLINT
363 Expect(false, __FILE__, __LINE__, "This should fail.");
364 }, "This should fail");
365
366 EXPECT_NONFATAL_FAILURE({ // NOLINT
367 Expect(false, __FILE__, __LINE__);
368 }, "Expectation failed");
369}
370
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000371// Tests LogIsVisible().
372
373class LogIsVisibleTest : public ::testing::Test {
374 protected:
Abseil Team26743362018-12-03 11:30:02 -0500375 void SetUp() override { original_verbose_ = GMOCK_FLAG(verbose); }
zhanyong.wan81476f22009-06-22 23:30:47 +0000376
Abseil Team26743362018-12-03 11:30:02 -0500377 void TearDown() override { GMOCK_FLAG(verbose) = original_verbose_; }
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000378
Nico Weber09fd5b32017-05-15 17:07:03 -0400379 std::string original_verbose_;
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000380};
381
382TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
383 GMOCK_FLAG(verbose) = kInfoVerbosity;
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000384 EXPECT_TRUE(LogIsVisible(kInfo));
385 EXPECT_TRUE(LogIsVisible(kWarning));
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000386}
387
388TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
389 GMOCK_FLAG(verbose) = kErrorVerbosity;
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000390 EXPECT_FALSE(LogIsVisible(kInfo));
391 EXPECT_FALSE(LogIsVisible(kWarning));
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000392}
393
394TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
395 GMOCK_FLAG(verbose) = kWarningVerbosity;
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000396 EXPECT_FALSE(LogIsVisible(kInfo));
397 EXPECT_TRUE(LogIsVisible(kWarning));
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000398}
399
zhanyong.wan2516f602010-08-31 18:28:02 +0000400#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000401
402// Tests the Log() function.
403
404// Verifies that Log() behaves correctly for the given verbosity level
405// and log severity.
Nico Weber09fd5b32017-05-15 17:07:03 -0400406void TestLogWithSeverity(const std::string& verbosity, LogSeverity severity,
shiqiane35fdd92008-12-10 05:08:54 +0000407 bool should_print) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400408 const std::string old_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000409 GMOCK_FLAG(verbose) = verbosity;
zhanyong.wan470df422010-02-02 22:34:58 +0000410 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000411 Log(severity, "Test log.\n", 0);
412 if (should_print) {
zhanyong.wan470df422010-02-02 22:34:58 +0000413 EXPECT_THAT(GetCapturedStdout().c_str(),
414 ContainsRegex(
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000415 severity == kWarning ?
zhanyong.wan470df422010-02-02 22:34:58 +0000416 "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
417 "^\nTest log\\.\nStack trace:\n"));
shiqiane35fdd92008-12-10 05:08:54 +0000418 } else {
zhanyong.wan470df422010-02-02 22:34:58 +0000419 EXPECT_STREQ("", GetCapturedStdout().c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000420 }
421 GMOCK_FLAG(verbose) = old_flag;
422}
423
424// Tests that when the stack_frames_to_skip parameter is negative,
425// Log() doesn't include the stack trace in the output.
426TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400427 const std::string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000428 GMOCK_FLAG(verbose) = kInfoVerbosity;
zhanyong.wan470df422010-02-02 22:34:58 +0000429 CaptureStdout();
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000430 Log(kInfo, "Test log.\n", -1);
zhanyong.wan470df422010-02-02 22:34:58 +0000431 EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
vladlosev76c1c612010-05-05 19:47:46 +0000432 GMOCK_FLAG(verbose) = saved_flag;
shiqiane35fdd92008-12-10 05:08:54 +0000433}
434
kosake83085a2015-07-28 00:12:51 +0000435struct MockStackTraceGetter : testing::internal::OsStackTraceGetterInterface {
Abseil Team26743362018-12-03 11:30:02 -0500436 std::string CurrentStackTrace(int max_depth, int skip_count) override {
kosake83085a2015-07-28 00:12:51 +0000437 return (testing::Message() << max_depth << "::" << skip_count << "\n")
438 .GetString();
439 }
Abseil Team26743362018-12-03 11:30:02 -0500440 void UponLeavingGTest() override {}
kosake83085a2015-07-28 00:12:51 +0000441};
442
shiqiane35fdd92008-12-10 05:08:54 +0000443// Tests that in opt mode, a positive stack_frames_to_skip argument is
444// treated as 0.
445TEST(LogTest, NoSkippingStackFrameInOptMode) {
kosake83085a2015-07-28 00:12:51 +0000446 MockStackTraceGetter* mock_os_stack_trace_getter = new MockStackTraceGetter;
447 GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter);
448
zhanyong.wan470df422010-02-02 22:34:58 +0000449 CaptureStdout();
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000450 Log(kWarning, "Test log.\n", 100);
Nico Weber09fd5b32017-05-15 17:07:03 -0400451 const std::string log = GetCapturedStdout();
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000452
Nico Weber09fd5b32017-05-15 17:07:03 -0400453 std::string expected_trace =
kosake83085a2015-07-28 00:12:51 +0000454 (testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
Nico Weber09fd5b32017-05-15 17:07:03 -0400455 std::string expected_message =
kosake83085a2015-07-28 00:12:51 +0000456 "\nGMOCK WARNING:\n"
457 "Test log.\n"
458 "Stack trace:\n" +
459 expected_trace;
460 EXPECT_THAT(log, HasSubstr(expected_message));
461 int skip_count = atoi(log.substr(expected_message.size()).c_str());
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000462
kosake83085a2015-07-28 00:12:51 +0000463# if defined(NDEBUG)
shiqiane35fdd92008-12-10 05:08:54 +0000464 // In opt mode, no stack frame should be skipped.
kosake83085a2015-07-28 00:12:51 +0000465 const int expected_skip_count = 0;
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000466# else
shiqiane35fdd92008-12-10 05:08:54 +0000467 // In dbg mode, the stack frames should be skipped.
kosake83085a2015-07-28 00:12:51 +0000468 const int expected_skip_count = 100;
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000469# endif
kosake83085a2015-07-28 00:12:51 +0000470
471 // Note that each inner implementation layer will +1 the number to remove
472 // itself from the trace. This means that the value is a little higher than
473 // expected, but close enough.
474 EXPECT_THAT(skip_count,
475 AllOf(Ge(expected_skip_count), Le(expected_skip_count + 10)));
476
477 // Restores the default OS stack trace getter.
Abseil Team4bb49ed2018-10-04 18:28:05 -0400478 GetUnitTestImpl()->set_os_stack_trace_getter(nullptr);
shiqiane35fdd92008-12-10 05:08:54 +0000479}
480
481// Tests that all logs are printed when the value of the
482// --gmock_verbose flag is "info".
483TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000484 TestLogWithSeverity(kInfoVerbosity, kInfo, true);
485 TestLogWithSeverity(kInfoVerbosity, kWarning, true);
shiqiane35fdd92008-12-10 05:08:54 +0000486}
487
488// Tests that only warnings are printed when the value of the
489// --gmock_verbose flag is "warning".
490TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000491 TestLogWithSeverity(kWarningVerbosity, kInfo, false);
492 TestLogWithSeverity(kWarningVerbosity, kWarning, true);
shiqiane35fdd92008-12-10 05:08:54 +0000493}
494
495// Tests that no logs are printed when the value of the
496// --gmock_verbose flag is "error".
497TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000498 TestLogWithSeverity(kErrorVerbosity, kInfo, false);
499 TestLogWithSeverity(kErrorVerbosity, kWarning, false);
shiqiane35fdd92008-12-10 05:08:54 +0000500}
501
502// Tests that only warnings are printed when the value of the
503// --gmock_verbose flag is invalid.
504TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000505 TestLogWithSeverity("invalid", kInfo, false);
506 TestLogWithSeverity("invalid", kWarning, true);
shiqiane35fdd92008-12-10 05:08:54 +0000507}
508
zhanyong.wan2516f602010-08-31 18:28:02 +0000509#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000510
shiqiane35fdd92008-12-10 05:08:54 +0000511TEST(TypeTraitsTest, remove_reference) {
Krystian Kuzniarekec49fbc2019-08-13 22:30:12 +0200512 EXPECT_TRUE((std::is_same<char, remove_reference<char&>::type>::value));
513 EXPECT_TRUE(
514 (std::is_same<const int, remove_reference<const int&>::type>::value));
515 EXPECT_TRUE((std::is_same<int, remove_reference<int>::type>::value));
516 EXPECT_TRUE((std::is_same<double*, remove_reference<double*>::type>::value));
shiqiane35fdd92008-12-10 05:08:54 +0000517}
518
zhanyong.wan2516f602010-08-31 18:28:02 +0000519#if GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000520
521// Verifies that Log() behaves correctly for the given verbosity level
522// and log severity.
jgm38513a82012-11-15 15:50:36 +0000523std::string GrabOutput(void(*logger)(), const char* verbosity) {
Nico Weber09fd5b32017-05-15 17:07:03 -0400524 const std::string saved_flag = GMOCK_FLAG(verbose);
shiqiane35fdd92008-12-10 05:08:54 +0000525 GMOCK_FLAG(verbose) = verbosity;
zhanyong.wan470df422010-02-02 22:34:58 +0000526 CaptureStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000527 logger();
528 GMOCK_FLAG(verbose) = saved_flag;
zhanyong.wan470df422010-02-02 22:34:58 +0000529 return GetCapturedStdout();
shiqiane35fdd92008-12-10 05:08:54 +0000530}
531
532class DummyMock {
533 public:
534 MOCK_METHOD0(TestMethod, void());
535 MOCK_METHOD1(TestMethodArg, void(int dummy));
536};
537
538void ExpectCallLogger() {
539 DummyMock mock;
540 EXPECT_CALL(mock, TestMethod());
541 mock.TestMethod();
Abseil Teama1dd0772019-03-04 09:38:02 -0500542}
shiqiane35fdd92008-12-10 05:08:54 +0000543
544// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
545TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
jgm38513a82012-11-15 15:50:36 +0000546 EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)),
shiqiane35fdd92008-12-10 05:08:54 +0000547 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
548}
549
550// Verifies that EXPECT_CALL doesn't log
551// if the --gmock_verbose flag is set to "warning".
552TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
zhanyong.wan470df422010-02-02 22:34:58 +0000553 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000554}
555
556// Verifies that EXPECT_CALL doesn't log
557// if the --gmock_verbose flag is set to "error".
558TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
zhanyong.wan470df422010-02-02 22:34:58 +0000559 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000560}
561
562void OnCallLogger() {
563 DummyMock mock;
564 ON_CALL(mock, TestMethod());
Abseil Teama1dd0772019-03-04 09:38:02 -0500565}
shiqiane35fdd92008-12-10 05:08:54 +0000566
567// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
568TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
jgm38513a82012-11-15 15:50:36 +0000569 EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)),
shiqiane35fdd92008-12-10 05:08:54 +0000570 HasSubstr("ON_CALL(mock, TestMethod())"));
571}
572
573// Verifies that ON_CALL doesn't log
574// if the --gmock_verbose flag is set to "warning".
575TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
zhanyong.wan470df422010-02-02 22:34:58 +0000576 EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000577}
578
579// Verifies that ON_CALL doesn't log if
580// the --gmock_verbose flag is set to "error".
581TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
zhanyong.wan470df422010-02-02 22:34:58 +0000582 EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
shiqiane35fdd92008-12-10 05:08:54 +0000583}
584
585void OnCallAnyArgumentLogger() {
586 DummyMock mock;
587 ON_CALL(mock, TestMethodArg(_));
588}
589
590// Verifies that ON_CALL prints provided _ argument.
591TEST(OnCallTest, LogsAnythingArgument) {
jgm38513a82012-11-15 15:50:36 +0000592 EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)),
shiqiane35fdd92008-12-10 05:08:54 +0000593 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
594}
595
zhanyong.wan2516f602010-08-31 18:28:02 +0000596#endif // GTEST_HAS_STREAM_REDIRECTION
shiqiane35fdd92008-12-10 05:08:54 +0000597
zhanyong.wanb8243162009-06-04 05:48:20 +0000598// Tests StlContainerView.
599
600TEST(StlContainerViewTest, WorksForStlContainer) {
601 StaticAssertTypeEq<std::vector<int>,
602 StlContainerView<std::vector<int> >::type>();
603 StaticAssertTypeEq<const std::vector<double>&,
604 StlContainerView<std::vector<double> >::const_reference>();
605
606 typedef std::vector<char> Chars;
607 Chars v1;
608 const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
609 EXPECT_EQ(&v1, &v2);
610
611 v1.push_back('a');
612 Chars v3 = StlContainerView<Chars>::Copy(v1);
613 EXPECT_THAT(v3, Eq(v3));
614}
615
616TEST(StlContainerViewTest, WorksForStaticNativeArray) {
617 StaticAssertTypeEq<NativeArray<int>,
618 StlContainerView<int[3]>::type>();
619 StaticAssertTypeEq<NativeArray<double>,
620 StlContainerView<const double[4]>::type>();
621 StaticAssertTypeEq<NativeArray<char[3]>,
622 StlContainerView<const char[2][3]>::type>();
623
624 StaticAssertTypeEq<const NativeArray<int>,
625 StlContainerView<int[2]>::const_reference>();
626
627 int a1[3] = { 0, 1, 2 };
628 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
zhanyong.wan32de5f52009-12-23 00:13:23 +0000629 EXPECT_EQ(3U, a2.size());
zhanyong.wanb8243162009-06-04 05:48:20 +0000630 EXPECT_EQ(a1, a2.begin());
631
632 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
zhanyong.wan32de5f52009-12-23 00:13:23 +0000633 ASSERT_EQ(3U, a3.size());
zhanyong.wanb8243162009-06-04 05:48:20 +0000634 EXPECT_EQ(0, a3.begin()[0]);
635 EXPECT_EQ(1, a3.begin()[1]);
636 EXPECT_EQ(2, a3.begin()[2]);
637
638 // Makes sure a1 and a3 aren't aliases.
639 a1[0] = 3;
640 EXPECT_EQ(0, a3.begin()[0]);
641}
642
643TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
644 StaticAssertTypeEq<NativeArray<int>,
Abseil Team7d3b73c2018-10-09 14:50:26 -0400645 StlContainerView<std::tuple<const int*, size_t> >::type>();
646 StaticAssertTypeEq<
647 NativeArray<double>,
misterg80b43d92018-10-29 11:09:33 -0400648 StlContainerView<std::tuple<std::shared_ptr<double>, int> >::type>();
zhanyong.wanb8243162009-06-04 05:48:20 +0000649
Abseil Team7d3b73c2018-10-09 14:50:26 -0400650 StaticAssertTypeEq<
651 const NativeArray<int>,
652 StlContainerView<std::tuple<const int*, int> >::const_reference>();
zhanyong.wanb8243162009-06-04 05:48:20 +0000653
654 int a1[3] = { 0, 1, 2 };
zhanyong.wan90c90f92009-06-17 22:11:04 +0000655 const int* const p1 = a1;
Abseil Team7d3b73c2018-10-09 14:50:26 -0400656 NativeArray<int> a2 =
657 StlContainerView<std::tuple<const int*, int> >::ConstReference(
658 std::make_tuple(p1, 3));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000659 EXPECT_EQ(3U, a2.size());
zhanyong.wanb8243162009-06-04 05:48:20 +0000660 EXPECT_EQ(a1, a2.begin());
661
Abseil Team7d3b73c2018-10-09 14:50:26 -0400662 const NativeArray<int> a3 = StlContainerView<std::tuple<int*, size_t> >::Copy(
663 std::make_tuple(static_cast<int*>(a1), 3));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000664 ASSERT_EQ(3U, a3.size());
zhanyong.wanb8243162009-06-04 05:48:20 +0000665 EXPECT_EQ(0, a3.begin()[0]);
666 EXPECT_EQ(1, a3.begin()[1]);
667 EXPECT_EQ(2, a3.begin()[2]);
668
669 // Makes sure a1 and a3 aren't aliases.
670 a1[0] = 3;
671 EXPECT_EQ(0, a3.begin()[0]);
672}
673
Abseil Team216c37f2019-01-07 17:06:16 -0500674// Tests the Function template struct.
675
676TEST(FunctionTest, Nullary) {
677 typedef Function<int()> F; // NOLINT
678 EXPECT_EQ(0u, F::ArgumentCount);
679 CompileAssertTypesEqual<int, F::Result>();
680 CompileAssertTypesEqual<std::tuple<>, F::ArgumentTuple>();
681 CompileAssertTypesEqual<std::tuple<>, F::ArgumentMatcherTuple>();
682 CompileAssertTypesEqual<void(), F::MakeResultVoid>();
683 CompileAssertTypesEqual<IgnoredValue(), F::MakeResultIgnoredValue>();
684}
685
686TEST(FunctionTest, Unary) {
687 typedef Function<int(bool)> F; // NOLINT
688 EXPECT_EQ(1u, F::ArgumentCount);
689 CompileAssertTypesEqual<int, F::Result>();
690 CompileAssertTypesEqual<bool, F::Arg<0>::type>();
691 CompileAssertTypesEqual<std::tuple<bool>, F::ArgumentTuple>();
692 CompileAssertTypesEqual<std::tuple<Matcher<bool> >,
693 F::ArgumentMatcherTuple>();
694 CompileAssertTypesEqual<void(bool), F::MakeResultVoid>(); // NOLINT
695 CompileAssertTypesEqual<IgnoredValue(bool), // NOLINT
696 F::MakeResultIgnoredValue>();
697}
698
699TEST(FunctionTest, Binary) {
700 typedef Function<int(bool, const long&)> F; // NOLINT
701 EXPECT_EQ(2u, F::ArgumentCount);
702 CompileAssertTypesEqual<int, F::Result>();
703 CompileAssertTypesEqual<bool, F::Arg<0>::type>();
704 CompileAssertTypesEqual<const long&, F::Arg<1>::type>(); // NOLINT
705 CompileAssertTypesEqual<std::tuple<bool, const long&>, // NOLINT
706 F::ArgumentTuple>();
707 CompileAssertTypesEqual<
708 std::tuple<Matcher<bool>, Matcher<const long&> >, // NOLINT
709 F::ArgumentMatcherTuple>();
710 CompileAssertTypesEqual<void(bool, const long&), F::MakeResultVoid>(); // NOLINT
711 CompileAssertTypesEqual<IgnoredValue(bool, const long&), // NOLINT
712 F::MakeResultIgnoredValue>();
713}
714
715TEST(FunctionTest, LongArgumentList) {
716 typedef Function<char(bool, int, char*, int&, const long&)> F; // NOLINT
717 EXPECT_EQ(5u, F::ArgumentCount);
718 CompileAssertTypesEqual<char, F::Result>();
719 CompileAssertTypesEqual<bool, F::Arg<0>::type>();
720 CompileAssertTypesEqual<int, F::Arg<1>::type>();
721 CompileAssertTypesEqual<char*, F::Arg<2>::type>();
722 CompileAssertTypesEqual<int&, F::Arg<3>::type>();
723 CompileAssertTypesEqual<const long&, F::Arg<4>::type>(); // NOLINT
724 CompileAssertTypesEqual<
725 std::tuple<bool, int, char*, int&, const long&>, // NOLINT
726 F::ArgumentTuple>();
727 CompileAssertTypesEqual<
728 std::tuple<Matcher<bool>, Matcher<int>, Matcher<char*>, Matcher<int&>,
729 Matcher<const long&> >, // NOLINT
730 F::ArgumentMatcherTuple>();
731 CompileAssertTypesEqual<void(bool, int, char*, int&, const long&), // NOLINT
732 F::MakeResultVoid>();
733 CompileAssertTypesEqual<
734 IgnoredValue(bool, int, char*, int&, const long&), // NOLINT
735 F::MakeResultIgnoredValue>();
736}
737
shiqiane35fdd92008-12-10 05:08:54 +0000738} // namespace
739} // namespace internal
740} // namespace testing