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