Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
gmock-internal-utils_test.cc
Go to the documentation of this file.
1// 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
31// Google Mock - a framework for writing C++ mock classes.
32//
33// This file tests the internal utilities.
34
36
37#include <stdlib.h>
38
39#include <cstdint>
40#include <map>
41#include <memory>
42#include <sstream>
43#include <string>
44#include <vector>
45
46#include "gmock/gmock.h"
48#include "gtest/gtest-spi.h"
49#include "gtest/gtest.h"
50
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
58#undef GTEST_IMPLEMENTATION_
59
60#if GTEST_OS_CYGWIN
61# include <sys/types.h> // For ssize_t. NOLINT
62#endif
63
64namespace proto2 {
65class Message;
66} // namespace proto2
67
68namespace testing {
69namespace internal {
70
71namespace {
72
73TEST(JoinAsTupleTest, JoinsEmptyTuple) {
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
93TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
97}
98
99TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
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"));
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"));
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
127TEST(PointeeOfTest, WorksForSmartPointers) {
129 (std::is_same<int, PointeeOf<std::unique_ptr<int>>::type>::value));
131 (std::is_same<std::string,
132 PointeeOf<std::shared_ptr<std::string>>::type>::value));
133}
134
135TEST(PointeeOfTest, WorksForRawPointers) {
136 EXPECT_TRUE((std::is_same<int, PointeeOf<int*>::type>::value));
137 EXPECT_TRUE((std::is_same<const char, PointeeOf<const char*>::type>::value));
139}
140
141TEST(GetRawPointerTest, WorksForSmartPointers) {
142 const char* const raw_p1 = new const char('a'); // NOLINT
143 const std::unique_ptr<const char> p1(raw_p1);
144 EXPECT_EQ(raw_p1, GetRawPointer(p1));
145 double* const raw_p2 = new double(2.5); // NOLINT
146 const std::shared_ptr<double> p2(raw_p2);
147 EXPECT_EQ(raw_p2, GetRawPointer(p2));
148}
149
150TEST(GetRawPointerTest, WorksForRawPointers) {
151 int* p = nullptr;
152 EXPECT_TRUE(nullptr == GetRawPointer(p));
153 int n = 1;
154 EXPECT_EQ(&n, GetRawPointer(&n));
155}
156
157// Tests KindOf<T>.
158
159class Base {};
160class Derived : public Base {};
161
162TEST(KindOfTest, Bool) {
163 EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool)); // NOLINT
164}
165
166TEST(KindOfTest, Integer) {
167 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char)); // NOLINT
168 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char)); // NOLINT
169 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char)); // NOLINT
170 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short)); // NOLINT
171 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short)); // NOLINT
172 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int)); // NOLINT
173 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int)); // NOLINT
174 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long)); // NOLINT
175 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long)); // NOLINT
176 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long long)); // NOLINT
177 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long long)); // NOLINT
178 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t)); // NOLINT
179 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t)); // NOLINT
180#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
181 // ssize_t is not defined on Windows and possibly some other OSes.
182 EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t)); // NOLINT
183#endif
184}
185
186TEST(KindOfTest, FloatingPoint) {
187 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float)); // NOLINT
188 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double)); // NOLINT
189 EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double)); // NOLINT
190}
191
192TEST(KindOfTest, Other) {
193 EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*)); // NOLINT
194 EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**)); // NOLINT
195 EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base)); // NOLINT
196}
197
198// Tests LosslessArithmeticConvertible<T, U>.
199
200TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
201 EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
202}
203
204TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
205 EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
206 EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
208 (LosslessArithmeticConvertible<bool, unsigned long>::value)); // NOLINT
209}
210
211TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
212 EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
213 EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
214}
215
216TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
217 EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
218 EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
219}
220
221TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
222 // Unsigned => larger signed is fine.
223 EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
224
225 // Unsigned => larger unsigned is fine.
227 unsigned short, uint64_t>::value)); // NOLINT
228
229 // Signed => unsigned is not fine.
231 short, uint64_t>::value)); // NOLINT
233 signed char, unsigned int>::value)); // NOLINT
234
235 // Same size and same signedness: fine too.
237 unsigned char, unsigned char>::value));
238 EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
239 EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
241 unsigned long, unsigned long>::value)); // NOLINT
242
243 // Same size, different signedness: not fine.
245 unsigned char, signed char>::value));
246 EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
247 EXPECT_FALSE((LosslessArithmeticConvertible<uint64_t, int64_t>::value));
248
249 // Larger size => smaller size is not fine.
250 EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value)); // NOLINT
251 EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
252 EXPECT_FALSE((LosslessArithmeticConvertible<int64_t, unsigned int>::value));
253}
254
255TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
256 // Integers cannot be losslessly converted to floating-points, as
257 // the format of the latter is implementation-defined.
258 EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
259 EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
261 short, long double>::value)); // NOLINT
262}
263
264TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
265 EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
266 EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
267}
268
269TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
270 EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value)); // NOLINT
271 EXPECT_FALSE((LosslessArithmeticConvertible<double, int64_t>::value));
272 EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
273}
274
275TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
276 // Smaller size => larger size is fine.
277 EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
278 EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
279 EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
280
281 // Same size: fine.
282 EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
283 EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
284
285 // Larger size => smaller size is not fine.
286 EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
288 if (sizeof(double) == sizeof(long double)) { // NOLINT
290 // In some implementations (e.g. MSVC), double and long double
291 // have the same size.
292 EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
293 } else {
294 EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
295 }
296}
297
298// Tests the TupleMatches() template function.
299
300TEST(TupleMatchesTest, WorksForSize0) {
301 std::tuple<> matchers;
302 std::tuple<> values;
303
304 EXPECT_TRUE(TupleMatches(matchers, values));
305}
306
307TEST(TupleMatchesTest, WorksForSize1) {
308 std::tuple<Matcher<int> > matchers(Eq(1));
309 std::tuple<int> values1(1), values2(2);
310
311 EXPECT_TRUE(TupleMatches(matchers, values1));
312 EXPECT_FALSE(TupleMatches(matchers, values2));
313}
314
315TEST(TupleMatchesTest, WorksForSize2) {
316 std::tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
317 std::tuple<int, char> values1(1, 'a'), values2(1, 'b'), values3(2, 'a'),
318 values4(2, 'b');
319
320 EXPECT_TRUE(TupleMatches(matchers, values1));
321 EXPECT_FALSE(TupleMatches(matchers, values2));
322 EXPECT_FALSE(TupleMatches(matchers, values3));
323 EXPECT_FALSE(TupleMatches(matchers, values4));
324}
325
326TEST(TupleMatchesTest, WorksForSize5) {
327 std::tuple<Matcher<int>, Matcher<char>, Matcher<bool>,
328 Matcher<long>, // NOLINT
329 Matcher<std::string> >
330 matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
331 std::tuple<int, char, bool, long, std::string> // NOLINT
332 values1(1, 'a', true, 2L, "hi"), values2(1, 'a', true, 2L, "hello"),
333 values3(2, 'a', true, 2L, "hi");
334
335 EXPECT_TRUE(TupleMatches(matchers, values1));
336 EXPECT_FALSE(TupleMatches(matchers, values2));
337 EXPECT_FALSE(TupleMatches(matchers, values3));
338}
339
340// Tests that Assert(true, ...) succeeds.
341TEST(AssertTest, SucceedsOnTrue) {
342 Assert(true, __FILE__, __LINE__, "This should succeed.");
343 Assert(true, __FILE__, __LINE__); // This should succeed too.
344}
345
346// Tests that Assert(false, ...) generates a fatal failure.
347TEST(AssertTest, FailsFatallyOnFalse) {
349 Assert(false, __FILE__, __LINE__, "This should fail.");
350 }, "");
351
353 Assert(false, __FILE__, __LINE__);
354 }, "");
355}
356
357// Tests that Expect(true, ...) succeeds.
358TEST(ExpectTest, SucceedsOnTrue) {
359 Expect(true, __FILE__, __LINE__, "This should succeed.");
360 Expect(true, __FILE__, __LINE__); // This should succeed too.
361}
362
363// Tests that Expect(false, ...) generates a non-fatal failure.
364TEST(ExpectTest, FailsNonfatallyOnFalse) {
365 EXPECT_NONFATAL_FAILURE({ // NOLINT
366 Expect(false, __FILE__, __LINE__, "This should fail.");
367 }, "This should fail");
368
369 EXPECT_NONFATAL_FAILURE({ // NOLINT
370 Expect(false, __FILE__, __LINE__);
371 }, "Expectation failed");
372}
373
374// Tests LogIsVisible().
375
376class LogIsVisibleTest : public ::testing::Test {
377 protected:
378 void SetUp() override { original_verbose_ = GMOCK_FLAG(verbose); }
379
380 void TearDown() override { GMOCK_FLAG(verbose) = original_verbose_; }
381
382 std::string original_verbose_;
383};
384
385TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
386 GMOCK_FLAG(verbose) = kInfoVerbosity;
389}
390
391TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
392 GMOCK_FLAG(verbose) = kErrorVerbosity;
395}
396
397TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
398 GMOCK_FLAG(verbose) = kWarningVerbosity;
401}
402
403#if GTEST_HAS_STREAM_REDIRECTION
404
405// Tests the Log() function.
406
407// Verifies that Log() behaves correctly for the given verbosity level
408// and log severity.
409void TestLogWithSeverity(const std::string& verbosity, LogSeverity severity,
410 bool should_print) {
411 const std::string old_flag = GMOCK_FLAG(verbose);
412 GMOCK_FLAG(verbose) = verbosity;
413 CaptureStdout();
414 Log(severity, "Test log.\n", 0);
415 if (should_print) {
416 EXPECT_THAT(GetCapturedStdout().c_str(),
417 ContainsRegex(
418 severity == kWarning ?
419 "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
420 "^\nTest log\\.\nStack trace:\n"));
421 } else {
422 EXPECT_STREQ("", GetCapturedStdout().c_str());
423 }
424 GMOCK_FLAG(verbose) = old_flag;
425}
426
427// Tests that when the stack_frames_to_skip parameter is negative,
428// Log() doesn't include the stack trace in the output.
429TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
430 const std::string saved_flag = GMOCK_FLAG(verbose);
431 GMOCK_FLAG(verbose) = kInfoVerbosity;
432 CaptureStdout();
433 Log(kInfo, "Test log.\n", -1);
434 EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
435 GMOCK_FLAG(verbose) = saved_flag;
436}
437
438struct MockStackTraceGetter : testing::internal::OsStackTraceGetterInterface {
439 std::string CurrentStackTrace(int max_depth, int skip_count) override {
440 return (testing::Message() << max_depth << "::" << skip_count << "\n")
441 .GetString();
442 }
443 void UponLeavingGTest() override {}
444};
445
446// Tests that in opt mode, a positive stack_frames_to_skip argument is
447// treated as 0.
448TEST(LogTest, NoSkippingStackFrameInOptMode) {
449 MockStackTraceGetter* mock_os_stack_trace_getter = new MockStackTraceGetter;
450 GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter);
451
452 CaptureStdout();
453 Log(kWarning, "Test log.\n", 100);
454 const std::string log = GetCapturedStdout();
455
456 std::string expected_trace =
457 (testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
458 std::string expected_message =
459 "\nGMOCK WARNING:\n"
460 "Test log.\n"
461 "Stack trace:\n" +
462 expected_trace;
463 EXPECT_THAT(log, HasSubstr(expected_message));
464 int skip_count = atoi(log.substr(expected_message.size()).c_str());
465
466# if defined(NDEBUG)
467 // In opt mode, no stack frame should be skipped.
468 const int expected_skip_count = 0;
469# else
470 // In dbg mode, the stack frames should be skipped.
471 const int expected_skip_count = 100;
472# endif
473
474 // Note that each inner implementation layer will +1 the number to remove
475 // itself from the trace. This means that the value is a little higher than
476 // expected, but close enough.
477 EXPECT_THAT(skip_count,
478 AllOf(Ge(expected_skip_count), Le(expected_skip_count + 10)));
479
480 // Restores the default OS stack trace getter.
481 GetUnitTestImpl()->set_os_stack_trace_getter(nullptr);
482}
483
484// Tests that all logs are printed when the value of the
485// --gmock_verbose flag is "info".
486TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
487 TestLogWithSeverity(kInfoVerbosity, kInfo, true);
488 TestLogWithSeverity(kInfoVerbosity, kWarning, true);
489}
490
491// Tests that only warnings are printed when the value of the
492// --gmock_verbose flag is "warning".
493TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
494 TestLogWithSeverity(kWarningVerbosity, kInfo, false);
495 TestLogWithSeverity(kWarningVerbosity, kWarning, true);
496}
497
498// Tests that no logs are printed when the value of the
499// --gmock_verbose flag is "error".
500TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
501 TestLogWithSeverity(kErrorVerbosity, kInfo, false);
502 TestLogWithSeverity(kErrorVerbosity, kWarning, false);
503}
504
505// Tests that only warnings are printed when the value of the
506// --gmock_verbose flag is invalid.
507TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
508 TestLogWithSeverity("invalid", kInfo, false);
509 TestLogWithSeverity("invalid", kWarning, true);
510}
511
512// Verifies that Log() behaves correctly for the given verbosity level
513// and log severity.
514std::string GrabOutput(void(*logger)(), const char* verbosity) {
515 const std::string saved_flag = GMOCK_FLAG(verbose);
516 GMOCK_FLAG(verbose) = verbosity;
517 CaptureStdout();
518 logger();
519 GMOCK_FLAG(verbose) = saved_flag;
520 return GetCapturedStdout();
521}
522
523class DummyMock {
524 public:
525 MOCK_METHOD0(TestMethod, void());
526 MOCK_METHOD1(TestMethodArg, void(int dummy));
527};
528
529void ExpectCallLogger() {
530 DummyMock mock;
531 EXPECT_CALL(mock, TestMethod());
532 mock.TestMethod();
533}
534
535// Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
536TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
537 EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)),
538 HasSubstr("EXPECT_CALL(mock, TestMethod())"));
539}
540
541// Verifies that EXPECT_CALL doesn't log
542// if the --gmock_verbose flag is set to "warning".
543TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
544 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
545}
546
547// Verifies that EXPECT_CALL doesn't log
548// if the --gmock_verbose flag is set to "error".
549TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsError) {
550 EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
551}
552
553void OnCallLogger() {
554 DummyMock mock;
555 ON_CALL(mock, TestMethod());
556}
557
558// Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
559TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
560 EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)),
561 HasSubstr("ON_CALL(mock, TestMethod())"));
562}
563
564// Verifies that ON_CALL doesn't log
565// if the --gmock_verbose flag is set to "warning".
566TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
567 EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
568}
569
570// Verifies that ON_CALL doesn't log if
571// the --gmock_verbose flag is set to "error".
572TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
573 EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
574}
575
576void OnCallAnyArgumentLogger() {
577 DummyMock mock;
578 ON_CALL(mock, TestMethodArg(_));
579}
580
581// Verifies that ON_CALL prints provided _ argument.
582TEST(OnCallTest, LogsAnythingArgument) {
583 EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)),
584 HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
585}
586
587#endif // GTEST_HAS_STREAM_REDIRECTION
588
589// Tests StlContainerView.
590
591TEST(StlContainerViewTest, WorksForStlContainer) {
592 StaticAssertTypeEq<std::vector<int>,
593 StlContainerView<std::vector<int> >::type>();
594 StaticAssertTypeEq<const std::vector<double>&,
595 StlContainerView<std::vector<double> >::const_reference>();
596
597 typedef std::vector<char> Chars;
598 Chars v1;
599 const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
600 EXPECT_EQ(&v1, &v2);
601
602 v1.push_back('a');
603 Chars v3 = StlContainerView<Chars>::Copy(v1);
604 EXPECT_THAT(v3, Eq(v3));
605}
606
607TEST(StlContainerViewTest, WorksForStaticNativeArray) {
608 StaticAssertTypeEq<NativeArray<int>,
610 StaticAssertTypeEq<NativeArray<double>,
612 StaticAssertTypeEq<NativeArray<char[3]>,
614
615 StaticAssertTypeEq<const NativeArray<int>,
617
618 int a1[3] = { 0, 1, 2 };
619 NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
620 EXPECT_EQ(3U, a2.size());
621 EXPECT_EQ(a1, a2.begin());
622
623 const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
624 ASSERT_EQ(3U, a3.size());
625 EXPECT_EQ(0, a3.begin()[0]);
626 EXPECT_EQ(1, a3.begin()[1]);
627 EXPECT_EQ(2, a3.begin()[2]);
628
629 // Makes sure a1 and a3 aren't aliases.
630 a1[0] = 3;
631 EXPECT_EQ(0, a3.begin()[0]);
632}
633
634TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
635 StaticAssertTypeEq<NativeArray<int>,
636 StlContainerView<std::tuple<const int*, size_t> >::type>();
637 StaticAssertTypeEq<
638 NativeArray<double>,
639 StlContainerView<std::tuple<std::shared_ptr<double>, int> >::type>();
640
641 StaticAssertTypeEq<
642 const NativeArray<int>,
643 StlContainerView<std::tuple<const int*, int> >::const_reference>();
644
645 int a1[3] = { 0, 1, 2 };
646 const int* const p1 = a1;
647 NativeArray<int> a2 =
648 StlContainerView<std::tuple<const int*, int> >::ConstReference(
649 std::make_tuple(p1, 3));
650 EXPECT_EQ(3U, a2.size());
651 EXPECT_EQ(a1, a2.begin());
652
653 const NativeArray<int> a3 = StlContainerView<std::tuple<int*, size_t> >::Copy(
654 std::make_tuple(static_cast<int*>(a1), 3));
655 ASSERT_EQ(3U, a3.size());
656 EXPECT_EQ(0, a3.begin()[0]);
657 EXPECT_EQ(1, a3.begin()[1]);
658 EXPECT_EQ(2, a3.begin()[2]);
659
660 // Makes sure a1 and a3 aren't aliases.
661 a1[0] = 3;
662 EXPECT_EQ(0, a3.begin()[0]);
663}
664
665// Tests the Function template struct.
666
667TEST(FunctionTest, Nullary) {
668 typedef Function<int()> F; // NOLINT
669 EXPECT_EQ(0u, F::ArgumentCount);
670 EXPECT_TRUE((std::is_same<int, F::Result>::value));
671 EXPECT_TRUE((std::is_same<std::tuple<>, F::ArgumentTuple>::value));
672 EXPECT_TRUE((std::is_same<std::tuple<>, F::ArgumentMatcherTuple>::value));
673 EXPECT_TRUE((std::is_same<void(), F::MakeResultVoid>::value));
674 EXPECT_TRUE((std::is_same<IgnoredValue(), F::MakeResultIgnoredValue>::value));
675}
676
677TEST(FunctionTest, Unary) {
678 typedef Function<int(bool)> F; // NOLINT
679 EXPECT_EQ(1u, F::ArgumentCount);
680 EXPECT_TRUE((std::is_same<int, F::Result>::value));
681 EXPECT_TRUE((std::is_same<bool, F::Arg<0>::type>::value));
682 EXPECT_TRUE((std::is_same<std::tuple<bool>, F::ArgumentTuple>::value));
684 std::is_same<std::tuple<Matcher<bool>>, F::ArgumentMatcherTuple>::value));
685 EXPECT_TRUE((std::is_same<void(bool), F::MakeResultVoid>::value)); // NOLINT
686 EXPECT_TRUE((std::is_same<IgnoredValue(bool), // NOLINT
687 F::MakeResultIgnoredValue>::value));
688}
689
690TEST(FunctionTest, Binary) {
691 typedef Function<int(bool, const long&)> F; // NOLINT
692 EXPECT_EQ(2u, F::ArgumentCount);
693 EXPECT_TRUE((std::is_same<int, F::Result>::value));
694 EXPECT_TRUE((std::is_same<bool, F::Arg<0>::type>::value));
695 EXPECT_TRUE((std::is_same<const long&, F::Arg<1>::type>::value)); // NOLINT
696 EXPECT_TRUE((std::is_same<std::tuple<bool, const long&>, // NOLINT
697 F::ArgumentTuple>::value));
699 (std::is_same<std::tuple<Matcher<bool>, Matcher<const long&>>, // NOLINT
700 F::ArgumentMatcherTuple>::value));
701 EXPECT_TRUE((std::is_same<void(bool, const long&), // NOLINT
702 F::MakeResultVoid>::value));
703 EXPECT_TRUE((std::is_same<IgnoredValue(bool, const long&), // NOLINT
704 F::MakeResultIgnoredValue>::value));
705}
706
707TEST(FunctionTest, LongArgumentList) {
708 typedef Function<char(bool, int, char*, int&, const long&)> F; // NOLINT
709 EXPECT_EQ(5u, F::ArgumentCount);
710 EXPECT_TRUE((std::is_same<char, F::Result>::value));
711 EXPECT_TRUE((std::is_same<bool, F::Arg<0>::type>::value));
712 EXPECT_TRUE((std::is_same<int, F::Arg<1>::type>::value));
713 EXPECT_TRUE((std::is_same<char*, F::Arg<2>::type>::value));
714 EXPECT_TRUE((std::is_same<int&, F::Arg<3>::type>::value));
715 EXPECT_TRUE((std::is_same<const long&, F::Arg<4>::type>::value)); // NOLINT
717 (std::is_same<std::tuple<bool, int, char*, int&, const long&>, // NOLINT
718 F::ArgumentTuple>::value));
720 (std::is_same<
721 std::tuple<Matcher<bool>, Matcher<int>, Matcher<char*>, Matcher<int&>,
722 Matcher<const long&>>, // NOLINT
723 F::ArgumentMatcherTuple>::value));
725 (std::is_same<void(bool, int, char*, int&, const long&), // NOLINT
726 F::MakeResultVoid>::value));
728 std::is_same<IgnoredValue(bool, int, char*, int&, const long&), // NOLINT
729 F::MakeResultIgnoredValue>::value));
730}
731
732} // namespace
733} // namespace internal
734} // namespace testing
#define F
static const_reference ConstReference(const RawContainer &container)
static type Copy(const RawContainer &container)
#define MOCK_METHOD0(m,...)
#define MOCK_METHOD1(m,...)
#define GMOCK_KIND_OF_(type)
std::string original_verbose_
#define EXPECT_THAT(value, matcher)
const char * p
#define GMOCK_FLAG(name)
Definition gmock-port.h:67
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition gtest-port.h:727
#define GTEST_FLAG(name)
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition gtest-port.h:729
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:210
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2379
#define ASSERT_EQ(val1, val2)
Definition gtest.h:2068
#define EXPECT_EQ(val1, val2)
Definition gtest.h:2038
#define TEST(test_suite_name, test_name)
Definition gtest.h:2348
#define EXPECT_TRUE(condition)
Definition gtest.h:1979
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:2107
#define EXPECT_FALSE(condition)
Definition gtest.h:1982
GTEST_API_ std::string ConvertIdentifierNameToWords(const char *id_name)
GTEST_API_ std::string JoinAsTuple(const Strings &fields)
::std::vector< ::std::string > Strings
GTEST_API_ bool LogIsVisible(LogSeverity severity)
GTEST_API_ void Log(LogSeverity severity, const std::string &message, int stack_frames_to_skip)
void Assert(bool condition, const char *file, int line, const std::string &msg)
const Pointer::element_type * GetRawPointer(const Pointer &p)
void Expect(bool condition, const char *file, int line, const std::string &msg)
LosslessArithmeticConvertibleImpl< GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To > LosslessArithmeticConvertible