1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests the internal utilities.
35 #include "gmock/internal/gmock-internal-utils.h"
46 #include "gmock/gmock.h"
47 #include "gmock/internal/gmock-port.h"
48 #include "gtest/gtest-spi.h"
49 #include "gtest/gtest.h"
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
56 #define GTEST_IMPLEMENTATION_ 1
57 #include "src/gtest-internal-inl.h"
58 #undef GTEST_IMPLEMENTATION_
61 # include <sys/types.h> // For ssize_t. NOLINT
73 TEST(JoinAsTupleTest
, JoinsEmptyTuple
) {
74 EXPECT_EQ("", JoinAsTuple(Strings()));
77 TEST(JoinAsTupleTest
, JoinsOneTuple
) {
78 const char* fields
[] = {"1"};
79 EXPECT_EQ("1", JoinAsTuple(Strings(fields
, fields
+ 1)));
82 TEST(JoinAsTupleTest
, JoinsTwoTuple
) {
83 const char* fields
[] = {"1", "a"};
84 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields
, fields
+ 2)));
87 TEST(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)));
93 TEST(ConvertIdentifierNameToWordsTest
, WorksWhenNameContainsNoWord
) {
94 EXPECT_EQ("", ConvertIdentifierNameToWords(""));
95 EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
96 EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
99 TEST(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"));
106 TEST(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"));
114 TEST(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"));
121 TEST(ConvertIdentifierNameToWordsTest
, WorksWhenNameIsMixture
) {
122 EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
123 EXPECT_EQ("chapter 11 section 1",
124 ConvertIdentifierNameToWords("_Chapter11Section_1_"));
127 TEST(GetRawPointerTest
, WorksForSmartPointers
) {
128 const char* const raw_p1
= new const char('a'); // NOLINT
129 const std::unique_ptr
<const char> p1(raw_p1
);
130 EXPECT_EQ(raw_p1
, GetRawPointer(p1
));
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
));
136 TEST(GetRawPointerTest
, WorksForRawPointers
) {
138 EXPECT_TRUE(nullptr == GetRawPointer(p
));
140 EXPECT_EQ(&n
, GetRawPointer(&n
));
146 class Derived
: public Base
{};
148 TEST(KindOfTest
, Bool
) {
149 EXPECT_EQ(kBool
, GMOCK_KIND_OF_(bool)); // NOLINT
152 TEST(KindOfTest
, Integer
) {
153 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(char)); // NOLINT
154 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(signed char)); // NOLINT
155 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(unsigned char)); // NOLINT
156 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(short)); // NOLINT
157 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(unsigned short)); // NOLINT
158 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(int)); // NOLINT
159 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(unsigned int)); // NOLINT
160 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(long)); // NOLINT
161 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(unsigned long)); // NOLINT
162 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(long long)); // NOLINT
163 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(unsigned long long)); // NOLINT
164 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(wchar_t)); // NOLINT
165 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(size_t)); // NOLINT
166 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
167 // ssize_t is not defined on Windows and possibly some other OSes.
168 EXPECT_EQ(kInteger
, GMOCK_KIND_OF_(ssize_t
)); // NOLINT
172 TEST(KindOfTest
, FloatingPoint
) {
173 EXPECT_EQ(kFloatingPoint
, GMOCK_KIND_OF_(float)); // NOLINT
174 EXPECT_EQ(kFloatingPoint
, GMOCK_KIND_OF_(double)); // NOLINT
175 EXPECT_EQ(kFloatingPoint
, GMOCK_KIND_OF_(long double)); // NOLINT
178 TEST(KindOfTest
, Other
) {
179 EXPECT_EQ(kOther
, GMOCK_KIND_OF_(void*)); // NOLINT
180 EXPECT_EQ(kOther
, GMOCK_KIND_OF_(char**)); // NOLINT
181 EXPECT_EQ(kOther
, GMOCK_KIND_OF_(Base
)); // NOLINT
184 // Tests LosslessArithmeticConvertible<T, U>.
186 TEST(LosslessArithmeticConvertibleTest
, BoolToBool
) {
187 EXPECT_TRUE((LosslessArithmeticConvertible
<bool, bool>::value
));
190 TEST(LosslessArithmeticConvertibleTest
, BoolToInteger
) {
191 EXPECT_TRUE((LosslessArithmeticConvertible
<bool, char>::value
));
192 EXPECT_TRUE((LosslessArithmeticConvertible
<bool, int>::value
));
194 (LosslessArithmeticConvertible
<bool, unsigned long>::value
)); // NOLINT
197 TEST(LosslessArithmeticConvertibleTest
, BoolToFloatingPoint
) {
198 EXPECT_TRUE((LosslessArithmeticConvertible
<bool, float>::value
));
199 EXPECT_TRUE((LosslessArithmeticConvertible
<bool, double>::value
));
202 TEST(LosslessArithmeticConvertibleTest
, IntegerToBool
) {
203 EXPECT_FALSE((LosslessArithmeticConvertible
<unsigned char, bool>::value
));
204 EXPECT_FALSE((LosslessArithmeticConvertible
<int, bool>::value
));
207 TEST(LosslessArithmeticConvertibleTest
, IntegerToInteger
) {
208 // Unsigned => larger signed is fine.
209 EXPECT_TRUE((LosslessArithmeticConvertible
<unsigned char, int>::value
));
211 // Unsigned => larger unsigned is fine.
212 EXPECT_TRUE((LosslessArithmeticConvertible
<
213 unsigned short, uint64_t>::value
)); // NOLINT
215 // Signed => unsigned is not fine.
216 EXPECT_FALSE((LosslessArithmeticConvertible
<
217 short, uint64_t>::value
)); // NOLINT
218 EXPECT_FALSE((LosslessArithmeticConvertible
<
219 signed char, unsigned int>::value
)); // NOLINT
221 // Same size and same signedness: fine too.
222 EXPECT_TRUE((LosslessArithmeticConvertible
<
223 unsigned char, unsigned char>::value
));
224 EXPECT_TRUE((LosslessArithmeticConvertible
<int, int>::value
));
225 EXPECT_TRUE((LosslessArithmeticConvertible
<wchar_t, wchar_t>::value
));
226 EXPECT_TRUE((LosslessArithmeticConvertible
<
227 unsigned long, unsigned long>::value
)); // NOLINT
229 // Same size, different signedness: not fine.
230 EXPECT_FALSE((LosslessArithmeticConvertible
<
231 unsigned char, signed char>::value
));
232 EXPECT_FALSE((LosslessArithmeticConvertible
<int, unsigned int>::value
));
233 EXPECT_FALSE((LosslessArithmeticConvertible
<uint64_t, int64_t>::value
));
235 // Larger size => smaller size is not fine.
236 EXPECT_FALSE((LosslessArithmeticConvertible
<long, char>::value
)); // NOLINT
237 EXPECT_FALSE((LosslessArithmeticConvertible
<int, signed char>::value
));
238 EXPECT_FALSE((LosslessArithmeticConvertible
<int64_t, unsigned int>::value
));
241 TEST(LosslessArithmeticConvertibleTest
, IntegerToFloatingPoint
) {
242 // Integers cannot be losslessly converted to floating-points, as
243 // the format of the latter is implementation-defined.
244 EXPECT_FALSE((LosslessArithmeticConvertible
<char, float>::value
));
245 EXPECT_FALSE((LosslessArithmeticConvertible
<int, double>::value
));
246 EXPECT_FALSE((LosslessArithmeticConvertible
<
247 short, long double>::value
)); // NOLINT
250 TEST(LosslessArithmeticConvertibleTest
, FloatingPointToBool
) {
251 EXPECT_FALSE((LosslessArithmeticConvertible
<float, bool>::value
));
252 EXPECT_FALSE((LosslessArithmeticConvertible
<double, bool>::value
));
255 TEST(LosslessArithmeticConvertibleTest
, FloatingPointToInteger
) {
256 EXPECT_FALSE((LosslessArithmeticConvertible
<float, long>::value
)); // NOLINT
257 EXPECT_FALSE((LosslessArithmeticConvertible
<double, int64_t>::value
));
258 EXPECT_FALSE((LosslessArithmeticConvertible
<long double, int>::value
));
261 TEST(LosslessArithmeticConvertibleTest
, FloatingPointToFloatingPoint
) {
262 // Smaller size => larger size is fine.
263 EXPECT_TRUE((LosslessArithmeticConvertible
<float, double>::value
));
264 EXPECT_TRUE((LosslessArithmeticConvertible
<float, long double>::value
));
265 EXPECT_TRUE((LosslessArithmeticConvertible
<double, long double>::value
));
268 EXPECT_TRUE((LosslessArithmeticConvertible
<float, float>::value
));
269 EXPECT_TRUE((LosslessArithmeticConvertible
<double, double>::value
));
271 // Larger size => smaller size is not fine.
272 EXPECT_FALSE((LosslessArithmeticConvertible
<double, float>::value
));
273 GTEST_INTENTIONAL_CONST_COND_PUSH_()
274 if (sizeof(double) == sizeof(long double)) { // NOLINT
275 GTEST_INTENTIONAL_CONST_COND_POP_()
276 // In some implementations (e.g. MSVC), double and long double
277 // have the same size.
278 EXPECT_TRUE((LosslessArithmeticConvertible
<long double, double>::value
));
280 EXPECT_FALSE((LosslessArithmeticConvertible
<long double, double>::value
));
284 // Tests the TupleMatches() template function.
286 TEST(TupleMatchesTest
, WorksForSize0
) {
287 std::tuple
<> matchers
;
290 EXPECT_TRUE(TupleMatches(matchers
, values
));
293 TEST(TupleMatchesTest
, WorksForSize1
) {
294 std::tuple
<Matcher
<int> > matchers(Eq(1));
295 std::tuple
<int> values1(1), values2(2);
297 EXPECT_TRUE(TupleMatches(matchers
, values1
));
298 EXPECT_FALSE(TupleMatches(matchers
, values2
));
301 TEST(TupleMatchesTest
, WorksForSize2
) {
302 std::tuple
<Matcher
<int>, Matcher
<char> > matchers(Eq(1), Eq('a'));
303 std::tuple
<int, char> values1(1, 'a'), values2(1, 'b'), values3(2, 'a'),
306 EXPECT_TRUE(TupleMatches(matchers
, values1
));
307 EXPECT_FALSE(TupleMatches(matchers
, values2
));
308 EXPECT_FALSE(TupleMatches(matchers
, values3
));
309 EXPECT_FALSE(TupleMatches(matchers
, values4
));
312 TEST(TupleMatchesTest
, WorksForSize5
) {
313 std::tuple
<Matcher
<int>, Matcher
<char>, Matcher
<bool>,
314 Matcher
<long>, // NOLINT
315 Matcher
<std::string
> >
316 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
317 std::tuple
<int, char, bool, long, std::string
> // NOLINT
318 values1(1, 'a', true, 2L, "hi"), values2(1, 'a', true, 2L, "hello"),
319 values3(2, 'a', true, 2L, "hi");
321 EXPECT_TRUE(TupleMatches(matchers
, values1
));
322 EXPECT_FALSE(TupleMatches(matchers
, values2
));
323 EXPECT_FALSE(TupleMatches(matchers
, values3
));
326 // Tests that Assert(true, ...) succeeds.
327 TEST(AssertTest
, SucceedsOnTrue
) {
328 Assert(true, __FILE__
, __LINE__
, "This should succeed.");
329 Assert(true, __FILE__
, __LINE__
); // This should succeed too.
332 // Tests that Assert(false, ...) generates a fatal failure.
333 TEST(AssertTest
, FailsFatallyOnFalse
) {
334 EXPECT_DEATH_IF_SUPPORTED({
335 Assert(false, __FILE__
, __LINE__
, "This should fail.");
338 EXPECT_DEATH_IF_SUPPORTED({
339 Assert(false, __FILE__
, __LINE__
);
343 // Tests that Expect(true, ...) succeeds.
344 TEST(ExpectTest
, SucceedsOnTrue
) {
345 Expect(true, __FILE__
, __LINE__
, "This should succeed.");
346 Expect(true, __FILE__
, __LINE__
); // This should succeed too.
349 // Tests that Expect(false, ...) generates a non-fatal failure.
350 TEST(ExpectTest
, FailsNonfatallyOnFalse
) {
351 EXPECT_NONFATAL_FAILURE({ // NOLINT
352 Expect(false, __FILE__
, __LINE__
, "This should fail.");
353 }, "This should fail");
355 EXPECT_NONFATAL_FAILURE({ // NOLINT
356 Expect(false, __FILE__
, __LINE__
);
357 }, "Expectation failed");
360 // Tests LogIsVisible().
362 class LogIsVisibleTest
: public ::testing::Test
{
364 void SetUp() override
{ original_verbose_
= GMOCK_FLAG(verbose
); }
366 void TearDown() override
{ GMOCK_FLAG(verbose
) = original_verbose_
; }
368 std::string original_verbose_
;
371 TEST_F(LogIsVisibleTest
, AlwaysReturnsTrueIfVerbosityIsInfo
) {
372 GMOCK_FLAG(verbose
) = kInfoVerbosity
;
373 EXPECT_TRUE(LogIsVisible(kInfo
));
374 EXPECT_TRUE(LogIsVisible(kWarning
));
377 TEST_F(LogIsVisibleTest
, AlwaysReturnsFalseIfVerbosityIsError
) {
378 GMOCK_FLAG(verbose
) = kErrorVerbosity
;
379 EXPECT_FALSE(LogIsVisible(kInfo
));
380 EXPECT_FALSE(LogIsVisible(kWarning
));
383 TEST_F(LogIsVisibleTest
, WorksWhenVerbosityIsWarning
) {
384 GMOCK_FLAG(verbose
) = kWarningVerbosity
;
385 EXPECT_FALSE(LogIsVisible(kInfo
));
386 EXPECT_TRUE(LogIsVisible(kWarning
));
389 #if GTEST_HAS_STREAM_REDIRECTION
391 // Tests the Log() function.
393 // Verifies that Log() behaves correctly for the given verbosity level
395 void TestLogWithSeverity(const std::string
& verbosity
, LogSeverity severity
,
397 const std::string old_flag
= GMOCK_FLAG(verbose
);
398 GMOCK_FLAG(verbose
) = verbosity
;
400 Log(severity
, "Test log.\n", 0);
402 EXPECT_THAT(GetCapturedStdout().c_str(),
404 severity
== kWarning
?
405 "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
406 "^\nTest log\\.\nStack trace:\n"));
408 EXPECT_STREQ("", GetCapturedStdout().c_str());
410 GMOCK_FLAG(verbose
) = old_flag
;
413 // Tests that when the stack_frames_to_skip parameter is negative,
414 // Log() doesn't include the stack trace in the output.
415 TEST(LogTest
, NoStackTraceWhenStackFramesToSkipIsNegative
) {
416 const std::string saved_flag
= GMOCK_FLAG(verbose
);
417 GMOCK_FLAG(verbose
) = kInfoVerbosity
;
419 Log(kInfo
, "Test log.\n", -1);
420 EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
421 GMOCK_FLAG(verbose
) = saved_flag
;
424 struct MockStackTraceGetter
: testing::internal::OsStackTraceGetterInterface
{
425 std::string
CurrentStackTrace(int max_depth
, int skip_count
) override
{
426 return (testing::Message() << max_depth
<< "::" << skip_count
<< "\n")
429 void UponLeavingGTest() override
{}
432 // Tests that in opt mode, a positive stack_frames_to_skip argument is
434 TEST(LogTest
, NoSkippingStackFrameInOptMode
) {
435 MockStackTraceGetter
* mock_os_stack_trace_getter
= new MockStackTraceGetter
;
436 GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter
);
439 Log(kWarning
, "Test log.\n", 100);
440 const std::string log
= GetCapturedStdout();
442 std::string expected_trace
=
443 (testing::Message() << GTEST_FLAG(stack_trace_depth
) << "::").GetString();
444 std::string expected_message
=
449 EXPECT_THAT(log
, HasSubstr(expected_message
));
450 int skip_count
= atoi(log
.substr(expected_message
.size()).c_str());
453 // In opt mode, no stack frame should be skipped.
454 const int expected_skip_count
= 0;
456 // In dbg mode, the stack frames should be skipped.
457 const int expected_skip_count
= 100;
460 // Note that each inner implementation layer will +1 the number to remove
461 // itself from the trace. This means that the value is a little higher than
462 // expected, but close enough.
463 EXPECT_THAT(skip_count
,
464 AllOf(Ge(expected_skip_count
), Le(expected_skip_count
+ 10)));
466 // Restores the default OS stack trace getter.
467 GetUnitTestImpl()->set_os_stack_trace_getter(nullptr);
470 // Tests that all logs are printed when the value of the
471 // --gmock_verbose flag is "info".
472 TEST(LogTest
, AllLogsArePrintedWhenVerbosityIsInfo
) {
473 TestLogWithSeverity(kInfoVerbosity
, kInfo
, true);
474 TestLogWithSeverity(kInfoVerbosity
, kWarning
, true);
477 // Tests that only warnings are printed when the value of the
478 // --gmock_verbose flag is "warning".
479 TEST(LogTest
, OnlyWarningsArePrintedWhenVerbosityIsWarning
) {
480 TestLogWithSeverity(kWarningVerbosity
, kInfo
, false);
481 TestLogWithSeverity(kWarningVerbosity
, kWarning
, true);
484 // Tests that no logs are printed when the value of the
485 // --gmock_verbose flag is "error".
486 TEST(LogTest
, NoLogsArePrintedWhenVerbosityIsError
) {
487 TestLogWithSeverity(kErrorVerbosity
, kInfo
, false);
488 TestLogWithSeverity(kErrorVerbosity
, kWarning
, false);
491 // Tests that only warnings are printed when the value of the
492 // --gmock_verbose flag is invalid.
493 TEST(LogTest
, OnlyWarningsArePrintedWhenVerbosityIsInvalid
) {
494 TestLogWithSeverity("invalid", kInfo
, false);
495 TestLogWithSeverity("invalid", kWarning
, true);
498 // Verifies that Log() behaves correctly for the given verbosity level
500 std::string
GrabOutput(void(*logger
)(), const char* verbosity
) {
501 const std::string saved_flag
= GMOCK_FLAG(verbose
);
502 GMOCK_FLAG(verbose
) = verbosity
;
505 GMOCK_FLAG(verbose
) = saved_flag
;
506 return GetCapturedStdout();
511 MOCK_METHOD0(TestMethod
, void());
512 MOCK_METHOD1(TestMethodArg
, void(int dummy
));
515 void ExpectCallLogger() {
517 EXPECT_CALL(mock
, TestMethod());
521 // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
522 TEST(ExpectCallTest
, LogsWhenVerbosityIsInfo
) {
523 EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger
, kInfoVerbosity
)),
524 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
527 // Verifies that EXPECT_CALL doesn't log
528 // if the --gmock_verbose flag is set to "warning".
529 TEST(ExpectCallTest
, DoesNotLogWhenVerbosityIsWarning
) {
530 EXPECT_STREQ("", GrabOutput(ExpectCallLogger
, kWarningVerbosity
).c_str());
533 // Verifies that EXPECT_CALL doesn't log
534 // if the --gmock_verbose flag is set to "error".
535 TEST(ExpectCallTest
, DoesNotLogWhenVerbosityIsError
) {
536 EXPECT_STREQ("", GrabOutput(ExpectCallLogger
, kErrorVerbosity
).c_str());
539 void OnCallLogger() {
541 ON_CALL(mock
, TestMethod());
544 // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
545 TEST(OnCallTest
, LogsWhenVerbosityIsInfo
) {
546 EXPECT_THAT(std::string(GrabOutput(OnCallLogger
, kInfoVerbosity
)),
547 HasSubstr("ON_CALL(mock, TestMethod())"));
550 // Verifies that ON_CALL doesn't log
551 // if the --gmock_verbose flag is set to "warning".
552 TEST(OnCallTest
, DoesNotLogWhenVerbosityIsWarning
) {
553 EXPECT_STREQ("", GrabOutput(OnCallLogger
, kWarningVerbosity
).c_str());
556 // Verifies that ON_CALL doesn't log if
557 // the --gmock_verbose flag is set to "error".
558 TEST(OnCallTest
, DoesNotLogWhenVerbosityIsError
) {
559 EXPECT_STREQ("", GrabOutput(OnCallLogger
, kErrorVerbosity
).c_str());
562 void OnCallAnyArgumentLogger() {
564 ON_CALL(mock
, TestMethodArg(_
));
567 // Verifies that ON_CALL prints provided _ argument.
568 TEST(OnCallTest
, LogsAnythingArgument
) {
569 EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger
, kInfoVerbosity
)),
570 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
573 #endif // GTEST_HAS_STREAM_REDIRECTION
575 // Tests StlContainerView.
577 TEST(StlContainerViewTest
, WorksForStlContainer
) {
578 StaticAssertTypeEq
<std::vector
<int>,
579 StlContainerView
<std::vector
<int> >::type
>();
580 StaticAssertTypeEq
<const std::vector
<double>&,
581 StlContainerView
<std::vector
<double> >::const_reference
>();
583 typedef std::vector
<char> Chars
;
585 const Chars
& v2(StlContainerView
<Chars
>::ConstReference(v1
));
589 Chars v3
= StlContainerView
<Chars
>::Copy(v1
);
590 EXPECT_THAT(v3
, Eq(v3
));
593 TEST(StlContainerViewTest
, WorksForStaticNativeArray
) {
594 StaticAssertTypeEq
<NativeArray
<int>,
595 StlContainerView
<int[3]>::type
>();
596 StaticAssertTypeEq
<NativeArray
<double>,
597 StlContainerView
<const double[4]>::type
>();
598 StaticAssertTypeEq
<NativeArray
<char[3]>,
599 StlContainerView
<const char[2][3]>::type
>();
601 StaticAssertTypeEq
<const NativeArray
<int>,
602 StlContainerView
<int[2]>::const_reference
>();
604 int a1
[3] = { 0, 1, 2 };
605 NativeArray
<int> a2
= StlContainerView
<int[3]>::ConstReference(a1
);
606 EXPECT_EQ(3U, a2
.size());
607 EXPECT_EQ(a1
, a2
.begin());
609 const NativeArray
<int> a3
= StlContainerView
<int[3]>::Copy(a1
);
610 ASSERT_EQ(3U, a3
.size());
611 EXPECT_EQ(0, a3
.begin()[0]);
612 EXPECT_EQ(1, a3
.begin()[1]);
613 EXPECT_EQ(2, a3
.begin()[2]);
615 // Makes sure a1 and a3 aren't aliases.
617 EXPECT_EQ(0, a3
.begin()[0]);
620 TEST(StlContainerViewTest
, WorksForDynamicNativeArray
) {
621 StaticAssertTypeEq
<NativeArray
<int>,
622 StlContainerView
<std::tuple
<const int*, size_t> >::type
>();
625 StlContainerView
<std::tuple
<std::shared_ptr
<double>, int> >::type
>();
628 const NativeArray
<int>,
629 StlContainerView
<std::tuple
<const int*, int> >::const_reference
>();
631 int a1
[3] = { 0, 1, 2 };
632 const int* const p1
= a1
;
633 NativeArray
<int> a2
=
634 StlContainerView
<std::tuple
<const int*, int> >::ConstReference(
635 std::make_tuple(p1
, 3));
636 EXPECT_EQ(3U, a2
.size());
637 EXPECT_EQ(a1
, a2
.begin());
639 const NativeArray
<int> a3
= StlContainerView
<std::tuple
<int*, size_t> >::Copy(
640 std::make_tuple(static_cast<int*>(a1
), 3));
641 ASSERT_EQ(3U, a3
.size());
642 EXPECT_EQ(0, a3
.begin()[0]);
643 EXPECT_EQ(1, a3
.begin()[1]);
644 EXPECT_EQ(2, a3
.begin()[2]);
646 // Makes sure a1 and a3 aren't aliases.
648 EXPECT_EQ(0, a3
.begin()[0]);
651 // Tests the Function template struct.
653 TEST(FunctionTest
, Nullary
) {
654 typedef Function
<int()> F
; // NOLINT
655 EXPECT_EQ(0u, F::ArgumentCount
);
656 EXPECT_TRUE((std::is_same
<int, F::Result
>::value
));
657 EXPECT_TRUE((std::is_same
<std::tuple
<>, F::ArgumentTuple
>::value
));
658 EXPECT_TRUE((std::is_same
<std::tuple
<>, F::ArgumentMatcherTuple
>::value
));
659 EXPECT_TRUE((std::is_same
<void(), F::MakeResultVoid
>::value
));
660 EXPECT_TRUE((std::is_same
<IgnoredValue(), F::MakeResultIgnoredValue
>::value
));
663 TEST(FunctionTest
, Unary
) {
664 typedef Function
<int(bool)> F
; // NOLINT
665 EXPECT_EQ(1u, F::ArgumentCount
);
666 EXPECT_TRUE((std::is_same
<int, F::Result
>::value
));
667 EXPECT_TRUE((std::is_same
<bool, F::Arg
<0>::type
>::value
));
668 EXPECT_TRUE((std::is_same
<std::tuple
<bool>, F::ArgumentTuple
>::value
));
670 std::is_same
<std::tuple
<Matcher
<bool>>, F::ArgumentMatcherTuple
>::value
));
671 EXPECT_TRUE((std::is_same
<void(bool), F::MakeResultVoid
>::value
)); // NOLINT
672 EXPECT_TRUE((std::is_same
<IgnoredValue(bool), // NOLINT
673 F::MakeResultIgnoredValue
>::value
));
676 TEST(FunctionTest
, Binary
) {
677 typedef Function
<int(bool, const long&)> F
; // NOLINT
678 EXPECT_EQ(2u, F::ArgumentCount
);
679 EXPECT_TRUE((std::is_same
<int, F::Result
>::value
));
680 EXPECT_TRUE((std::is_same
<bool, F::Arg
<0>::type
>::value
));
681 EXPECT_TRUE((std::is_same
<const long&, F::Arg
<1>::type
>::value
)); // NOLINT
682 EXPECT_TRUE((std::is_same
<std::tuple
<bool, const long&>, // NOLINT
683 F::ArgumentTuple
>::value
));
685 (std::is_same
<std::tuple
<Matcher
<bool>, Matcher
<const long&>>, // NOLINT
686 F::ArgumentMatcherTuple
>::value
));
687 EXPECT_TRUE((std::is_same
<void(bool, const long&), // NOLINT
688 F::MakeResultVoid
>::value
));
689 EXPECT_TRUE((std::is_same
<IgnoredValue(bool, const long&), // NOLINT
690 F::MakeResultIgnoredValue
>::value
));
693 TEST(FunctionTest
, LongArgumentList
) {
694 typedef Function
<char(bool, int, char*, int&, const long&)> F
; // NOLINT
695 EXPECT_EQ(5u, F::ArgumentCount
);
696 EXPECT_TRUE((std::is_same
<char, F::Result
>::value
));
697 EXPECT_TRUE((std::is_same
<bool, F::Arg
<0>::type
>::value
));
698 EXPECT_TRUE((std::is_same
<int, F::Arg
<1>::type
>::value
));
699 EXPECT_TRUE((std::is_same
<char*, F::Arg
<2>::type
>::value
));
700 EXPECT_TRUE((std::is_same
<int&, F::Arg
<3>::type
>::value
));
701 EXPECT_TRUE((std::is_same
<const long&, F::Arg
<4>::type
>::value
)); // NOLINT
703 (std::is_same
<std::tuple
<bool, int, char*, int&, const long&>, // NOLINT
704 F::ArgumentTuple
>::value
));
707 std::tuple
<Matcher
<bool>, Matcher
<int>, Matcher
<char*>, Matcher
<int&>,
708 Matcher
<const long&>>, // NOLINT
709 F::ArgumentMatcherTuple
>::value
));
711 (std::is_same
<void(bool, int, char*, int&, const long&), // NOLINT
712 F::MakeResultVoid
>::value
));
714 std::is_same
<IgnoredValue(bool, int, char*, int&, const long&), // NOLINT
715 F::MakeResultIgnoredValue
>::value
));
719 } // namespace internal
720 } // namespace testing