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