?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

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