Home | History | Annotate | Download | only in test
      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 <memory>
     40 #include <string>
     41 #include <sstream>
     42 #include <vector>
     43 #include "gmock/gmock.h"
     44 #include "gmock/internal/gmock-port.h"
     45 #include "gtest/gtest.h"
     46 #include "gtest/gtest-spi.h"
     47 
     48 // Indicates that this translation unit is part of Google Test's
     49 // implementation.  It must come before gtest-internal-inl.h is
     50 // included, or there will be a compiler error.  This trick is to
     51 // prevent a user from accidentally including gtest-internal-inl.h in
     52 // their code.
     53 #define GTEST_IMPLEMENTATION_ 1
     54 #include "src/gtest-internal-inl.h"
     55 #undef GTEST_IMPLEMENTATION_
     56 
     57 #if GTEST_OS_CYGWIN
     58 # include <sys/types.h>  // For ssize_t. NOLINT
     59 #endif
     60 
     61 class ProtocolMessage;
     62 
     63 namespace proto2 {
     64 class Message;
     65 }  // namespace proto2
     66 
     67 namespace testing {
     68 namespace internal {
     69 
     70 namespace {
     71 
     72 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
     73   EXPECT_EQ("", ConvertIdentifierNameToWords(""));
     74   EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
     75   EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
     76 }
     77 
     78 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
     79   EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
     80   EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
     81   EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
     82   EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
     83 }
     84 
     85 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
     86   EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
     87   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
     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 
     93 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
     94   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
     95   EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
     96   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
     97   EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
     98 }
     99 
    100 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
    101   EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
    102   EXPECT_EQ("chapter 11 section 1",
    103             ConvertIdentifierNameToWords("_Chapter11Section_1_"));
    104 }
    105 
    106 TEST(PointeeOfTest, WorksForSmartPointers) {
    107   CompileAssertTypesEqual<const char,
    108       PointeeOf<internal::linked_ptr<const char> >::type>();
    109 #if GTEST_HAS_STD_UNIQUE_PTR_
    110   CompileAssertTypesEqual<int, PointeeOf<std::unique_ptr<int> >::type>();
    111 #endif  // GTEST_HAS_STD_UNIQUE_PTR_
    112 #if GTEST_HAS_STD_SHARED_PTR_
    113   CompileAssertTypesEqual<std::string,
    114                           PointeeOf<std::shared_ptr<std::string> >::type>();
    115 #endif  // GTEST_HAS_STD_SHARED_PTR_
    116 }
    117 
    118 TEST(PointeeOfTest, WorksForRawPointers) {
    119   CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
    120   CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
    121   CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
    122 }
    123 
    124 TEST(GetRawPointerTest, WorksForSmartPointers) {
    125 #if GTEST_HAS_STD_UNIQUE_PTR_
    126   const char* const raw_p1 = new const char('a');  // NOLINT
    127   const std::unique_ptr<const char> p1(raw_p1);
    128   EXPECT_EQ(raw_p1, GetRawPointer(p1));
    129 #endif  // GTEST_HAS_STD_UNIQUE_PTR_
    130 #if GTEST_HAS_STD_SHARED_PTR_
    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));
    134 #endif  // GTEST_HAS_STD_SHARED_PTR_
    135 
    136   const char* const raw_p4 = new const char('a');  // NOLINT
    137   const internal::linked_ptr<const char> p4(raw_p4);
    138   EXPECT_EQ(raw_p4, GetRawPointer(p4));
    139 }
    140 
    141 TEST(GetRawPointerTest, WorksForRawPointers) {
    142   int* p = NULL;
    143   // Don't use EXPECT_EQ as no NULL-testing magic on Symbian.
    144   EXPECT_TRUE(NULL == GetRawPointer(p));
    145   int n = 1;
    146   EXPECT_EQ(&n, GetRawPointer(&n));
    147 }
    148 
    149 // Tests KindOf<T>.
    150 
    151 class Base {};
    152 class Derived : public Base {};
    153 
    154 TEST(KindOfTest, Bool) {
    155   EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool));  // NOLINT
    156 }
    157 
    158 TEST(KindOfTest, Integer) {
    159   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char));  // NOLINT
    160   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char));  // NOLINT
    161   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char));  // NOLINT
    162   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short));  // NOLINT
    163   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short));  // NOLINT
    164   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int));  // NOLINT
    165   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int));  // NOLINT
    166   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long));  // NOLINT
    167   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long));  // NOLINT
    168   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t));  // NOLINT
    169   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64));  // NOLINT
    170   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64));  // NOLINT
    171   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t));  // NOLINT
    172 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
    173   // ssize_t is not defined on Windows and possibly some other OSes.
    174   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t));  // NOLINT
    175 #endif
    176 }
    177 
    178 TEST(KindOfTest, FloatingPoint) {
    179   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float));  // NOLINT
    180   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double));  // NOLINT
    181   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double));  // NOLINT
    182 }
    183 
    184 TEST(KindOfTest, Other) {
    185   EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*));  // NOLINT
    186   EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**));  // NOLINT
    187   EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base));  // NOLINT
    188 }
    189 
    190 // Tests LosslessArithmeticConvertible<T, U>.
    191 
    192 TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
    193   EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
    194 }
    195 
    196 TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
    197   EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
    198   EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
    199   EXPECT_TRUE(
    200       (LosslessArithmeticConvertible<bool, unsigned long>::value));  // NOLINT
    201 }
    202 
    203 TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
    204   EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
    205   EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
    206 }
    207 
    208 TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
    209   EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
    210   EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
    211 }
    212 
    213 TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
    214   // Unsigned => larger signed is fine.
    215   EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
    216 
    217   // Unsigned => larger unsigned is fine.
    218   EXPECT_TRUE(
    219       (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
    220 
    221   // Signed => unsigned is not fine.
    222   EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
    223   EXPECT_FALSE((LosslessArithmeticConvertible<
    224       signed char, unsigned int>::value));  // NOLINT
    225 
    226   // Same size and same signedness: fine too.
    227   EXPECT_TRUE((LosslessArithmeticConvertible<
    228                unsigned char, unsigned char>::value));
    229   EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
    230   EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
    231   EXPECT_TRUE((LosslessArithmeticConvertible<
    232                unsigned long, unsigned long>::value));  // NOLINT
    233 
    234   // Same size, different signedness: not fine.
    235   EXPECT_FALSE((LosslessArithmeticConvertible<
    236                 unsigned char, signed char>::value));
    237   EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
    238   EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
    239 
    240   // Larger size => smaller size is not fine.
    241   EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value));  // NOLINT
    242   EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
    243   EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
    244 }
    245 
    246 TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
    247   // Integers cannot be losslessly converted to floating-points, as
    248   // the format of the latter is implementation-defined.
    249   EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
    250   EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
    251   EXPECT_FALSE((LosslessArithmeticConvertible<
    252                 short, long double>::value));  // NOLINT
    253 }
    254 
    255 TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
    256   EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
    257   EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
    258 }
    259 
    260 TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
    261   EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value));  // NOLINT
    262   EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
    263   EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
    264 }
    265 
    266 TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
    267   // Smaller size => larger size is fine.
    268   EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
    269   EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
    270   EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
    271 
    272   // Same size: fine.
    273   EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
    274   EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
    275 
    276   // Larger size => smaller size is not fine.
    277   EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
    278   GTEST_INTENTIONAL_CONST_COND_PUSH_()
    279   if (sizeof(double) == sizeof(long double)) {  // NOLINT
    280   GTEST_INTENTIONAL_CONST_COND_POP_()
    281     // In some implementations (e.g. MSVC), double and long double
    282     // have the same size.
    283     EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
    284   } else {
    285     EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
    286   }
    287 }
    288 
    289 // Tests the TupleMatches() template function.
    290 
    291 TEST(TupleMatchesTest, WorksForSize0) {
    292   tuple<> matchers;
    293   tuple<> values;
    294 
    295   EXPECT_TRUE(TupleMatches(matchers, values));
    296 }
    297 
    298 TEST(TupleMatchesTest, WorksForSize1) {
    299   tuple<Matcher<int> > matchers(Eq(1));
    300   tuple<int> values1(1),
    301       values2(2);
    302 
    303   EXPECT_TRUE(TupleMatches(matchers, values1));
    304   EXPECT_FALSE(TupleMatches(matchers, values2));
    305 }
    306 
    307 TEST(TupleMatchesTest, WorksForSize2) {
    308   tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
    309   tuple<int, char> values1(1, 'a'),
    310       values2(1, 'b'),
    311       values3(2, 'a'),
    312       values4(2, 'b');
    313 
    314   EXPECT_TRUE(TupleMatches(matchers, values1));
    315   EXPECT_FALSE(TupleMatches(matchers, values2));
    316   EXPECT_FALSE(TupleMatches(matchers, values3));
    317   EXPECT_FALSE(TupleMatches(matchers, values4));
    318 }
    319 
    320 TEST(TupleMatchesTest, WorksForSize5) {
    321   tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>,  // NOLINT
    322         Matcher<std::string> >
    323       matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
    324   tuple<int, char, bool, long, std::string>  // NOLINT
    325       values1(1, 'a', true, 2L, "hi"), 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   std::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 std::string& verbosity, LogSeverity severity,
    405                          bool should_print) {
    406   const std::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 std::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 std::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 std::string log = GetCapturedStdout();
    450 
    451   std::string expected_trace =
    452       (testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
    453   std::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 std::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