gmock-internal-utils_test.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file tests the internal utilities.
00035 
00036 #include "gmock/internal/gmock-internal-utils.h"
00037 #include <stdlib.h>
00038 #include <map>
00039 #include <string>
00040 #include <sstream>
00041 #include <vector>
00042 #include "gmock/gmock.h"
00043 #include "gmock/internal/gmock-port.h"
00044 #include "gtest/gtest.h"
00045 #include "gtest/gtest-spi.h"
00046 
00047 #if GTEST_OS_CYGWIN
00048 # include <sys/types.h>  // For ssize_t. NOLINT
00049 #endif
00050 
00051 class ProtocolMessage;
00052 
00053 namespace proto2 {
00054 class Message;
00055 }  // namespace proto2
00056 
00057 namespace testing {
00058 namespace internal {
00059 
00060 namespace {
00061 
00062 using ::std::tr1::make_tuple;
00063 using ::std::tr1::tuple;
00064 
00065 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsNoWord) {
00066   EXPECT_EQ("", ConvertIdentifierNameToWords(""));
00067   EXPECT_EQ("", ConvertIdentifierNameToWords("_"));
00068   EXPECT_EQ("", ConvertIdentifierNameToWords("__"));
00069 }
00070 
00071 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsDigits) {
00072   EXPECT_EQ("1", ConvertIdentifierNameToWords("_1"));
00073   EXPECT_EQ("2", ConvertIdentifierNameToWords("2_"));
00074   EXPECT_EQ("34", ConvertIdentifierNameToWords("_34_"));
00075   EXPECT_EQ("34 56", ConvertIdentifierNameToWords("_34_56"));
00076 }
00077 
00078 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContainsCamelCaseWords) {
00079   EXPECT_EQ("a big word", ConvertIdentifierNameToWords("ABigWord"));
00080   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("FooBar"));
00081   EXPECT_EQ("foo", ConvertIdentifierNameToWords("Foo_"));
00082   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_Foo_Bar_"));
00083   EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_Foo__And_Bar"));
00084 }
00085 
00086 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameContains_SeparatedWords) {
00087   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("foo_bar"));
00088   EXPECT_EQ("foo", ConvertIdentifierNameToWords("_foo_"));
00089   EXPECT_EQ("foo bar", ConvertIdentifierNameToWords("_foo_bar_"));
00090   EXPECT_EQ("foo and bar", ConvertIdentifierNameToWords("_foo__and_bar"));
00091 }
00092 
00093 TEST(ConvertIdentifierNameToWordsTest, WorksWhenNameIsMixture) {
00094   EXPECT_EQ("foo bar 123", ConvertIdentifierNameToWords("Foo_bar123"));
00095   EXPECT_EQ("chapter 11 section 1",
00096             ConvertIdentifierNameToWords("_Chapter11Section_1_"));
00097 }
00098 
00099 TEST(PointeeOfTest, WorksForSmartPointers) {
00100   CompileAssertTypesEqual<const char,
00101       PointeeOf<internal::linked_ptr<const char> >::type>();
00102 }
00103 
00104 TEST(PointeeOfTest, WorksForRawPointers) {
00105   CompileAssertTypesEqual<int, PointeeOf<int*>::type>();
00106   CompileAssertTypesEqual<const char, PointeeOf<const char*>::type>();
00107   CompileAssertTypesEqual<void, PointeeOf<void*>::type>();
00108 }
00109 
00110 TEST(GetRawPointerTest, WorksForSmartPointers) {
00111   const char* const raw_p4 = new const char('a');  // NOLINT
00112   const internal::linked_ptr<const char> p4(raw_p4);
00113   EXPECT_EQ(raw_p4, GetRawPointer(p4));
00114 }
00115 
00116 TEST(GetRawPointerTest, WorksForRawPointers) {
00117   int* p = NULL;
00118   // Don't use EXPECT_EQ as no NULL-testing magic on Symbian.
00119   EXPECT_TRUE(NULL == GetRawPointer(p));
00120   int n = 1;
00121   EXPECT_EQ(&n, GetRawPointer(&n));
00122 }
00123 
00124 // Tests KindOf<T>.
00125 
00126 class Base {};
00127 class Derived : public Base {};
00128 
00129 TEST(KindOfTest, Bool) {
00130   EXPECT_EQ(kBool, GMOCK_KIND_OF_(bool));  // NOLINT
00131 }
00132 
00133 TEST(KindOfTest, Integer) {
00134   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(char));  // NOLINT
00135   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(signed char));  // NOLINT
00136   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned char));  // NOLINT
00137   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(short));  // NOLINT
00138   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned short));  // NOLINT
00139   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(int));  // NOLINT
00140   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned int));  // NOLINT
00141   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(long));  // NOLINT
00142   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(unsigned long));  // NOLINT
00143   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(wchar_t));  // NOLINT
00144   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(Int64));  // NOLINT
00145   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(UInt64));  // NOLINT
00146   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(size_t));  // NOLINT
00147 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN
00148   // ssize_t is not defined on Windows and possibly some other OSes.
00149   EXPECT_EQ(kInteger, GMOCK_KIND_OF_(ssize_t));  // NOLINT
00150 #endif
00151 }
00152 
00153 TEST(KindOfTest, FloatingPoint) {
00154   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(float));  // NOLINT
00155   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(double));  // NOLINT
00156   EXPECT_EQ(kFloatingPoint, GMOCK_KIND_OF_(long double));  // NOLINT
00157 }
00158 
00159 TEST(KindOfTest, Other) {
00160   EXPECT_EQ(kOther, GMOCK_KIND_OF_(void*));  // NOLINT
00161   EXPECT_EQ(kOther, GMOCK_KIND_OF_(char**));  // NOLINT
00162   EXPECT_EQ(kOther, GMOCK_KIND_OF_(Base));  // NOLINT
00163 }
00164 
00165 // Tests LosslessArithmeticConvertible<T, U>.
00166 
00167 TEST(LosslessArithmeticConvertibleTest, BoolToBool) {
00168   EXPECT_TRUE((LosslessArithmeticConvertible<bool, bool>::value));
00169 }
00170 
00171 TEST(LosslessArithmeticConvertibleTest, BoolToInteger) {
00172   EXPECT_TRUE((LosslessArithmeticConvertible<bool, char>::value));
00173   EXPECT_TRUE((LosslessArithmeticConvertible<bool, int>::value));
00174   EXPECT_TRUE(
00175       (LosslessArithmeticConvertible<bool, unsigned long>::value));  // NOLINT
00176 }
00177 
00178 TEST(LosslessArithmeticConvertibleTest, BoolToFloatingPoint) {
00179   EXPECT_TRUE((LosslessArithmeticConvertible<bool, float>::value));
00180   EXPECT_TRUE((LosslessArithmeticConvertible<bool, double>::value));
00181 }
00182 
00183 TEST(LosslessArithmeticConvertibleTest, IntegerToBool) {
00184   EXPECT_FALSE((LosslessArithmeticConvertible<unsigned char, bool>::value));
00185   EXPECT_FALSE((LosslessArithmeticConvertible<int, bool>::value));
00186 }
00187 
00188 TEST(LosslessArithmeticConvertibleTest, IntegerToInteger) {
00189   // Unsigned => larger signed is fine.
00190   EXPECT_TRUE((LosslessArithmeticConvertible<unsigned char, int>::value));
00191 
00192   // Unsigned => larger unsigned is fine.
00193   EXPECT_TRUE(
00194       (LosslessArithmeticConvertible<unsigned short, UInt64>::value)); // NOLINT
00195 
00196   // Signed => unsigned is not fine.
00197   EXPECT_FALSE((LosslessArithmeticConvertible<short, UInt64>::value)); // NOLINT
00198   EXPECT_FALSE((LosslessArithmeticConvertible<
00199       signed char, unsigned int>::value));  // NOLINT
00200 
00201   // Same size and same signedness: fine too.
00202   EXPECT_TRUE((LosslessArithmeticConvertible<
00203                unsigned char, unsigned char>::value));
00204   EXPECT_TRUE((LosslessArithmeticConvertible<int, int>::value));
00205   EXPECT_TRUE((LosslessArithmeticConvertible<wchar_t, wchar_t>::value));
00206   EXPECT_TRUE((LosslessArithmeticConvertible<
00207                unsigned long, unsigned long>::value));  // NOLINT
00208 
00209   // Same size, different signedness: not fine.
00210   EXPECT_FALSE((LosslessArithmeticConvertible<
00211                 unsigned char, signed char>::value));
00212   EXPECT_FALSE((LosslessArithmeticConvertible<int, unsigned int>::value));
00213   EXPECT_FALSE((LosslessArithmeticConvertible<UInt64, Int64>::value));
00214 
00215   // Larger size => smaller size is not fine.
00216   EXPECT_FALSE((LosslessArithmeticConvertible<long, char>::value));  // NOLINT
00217   EXPECT_FALSE((LosslessArithmeticConvertible<int, signed char>::value));
00218   EXPECT_FALSE((LosslessArithmeticConvertible<Int64, unsigned int>::value));
00219 }
00220 
00221 TEST(LosslessArithmeticConvertibleTest, IntegerToFloatingPoint) {
00222   // Integers cannot be losslessly converted to floating-points, as
00223   // the format of the latter is implementation-defined.
00224   EXPECT_FALSE((LosslessArithmeticConvertible<char, float>::value));
00225   EXPECT_FALSE((LosslessArithmeticConvertible<int, double>::value));
00226   EXPECT_FALSE((LosslessArithmeticConvertible<
00227                 short, long double>::value));  // NOLINT
00228 }
00229 
00230 TEST(LosslessArithmeticConvertibleTest, FloatingPointToBool) {
00231   EXPECT_FALSE((LosslessArithmeticConvertible<float, bool>::value));
00232   EXPECT_FALSE((LosslessArithmeticConvertible<double, bool>::value));
00233 }
00234 
00235 TEST(LosslessArithmeticConvertibleTest, FloatingPointToInteger) {
00236   EXPECT_FALSE((LosslessArithmeticConvertible<float, long>::value));  // NOLINT
00237   EXPECT_FALSE((LosslessArithmeticConvertible<double, Int64>::value));
00238   EXPECT_FALSE((LosslessArithmeticConvertible<long double, int>::value));
00239 }
00240 
00241 TEST(LosslessArithmeticConvertibleTest, FloatingPointToFloatingPoint) {
00242   // Smaller size => larger size is fine.
00243   EXPECT_TRUE((LosslessArithmeticConvertible<float, double>::value));
00244   EXPECT_TRUE((LosslessArithmeticConvertible<float, long double>::value));
00245   EXPECT_TRUE((LosslessArithmeticConvertible<double, long double>::value));
00246 
00247   // Same size: fine.
00248   EXPECT_TRUE((LosslessArithmeticConvertible<float, float>::value));
00249   EXPECT_TRUE((LosslessArithmeticConvertible<double, double>::value));
00250 
00251   // Larger size => smaller size is not fine.
00252   EXPECT_FALSE((LosslessArithmeticConvertible<double, float>::value));
00253   if (sizeof(double) == sizeof(long double)) {  // NOLINT
00254     // In some implementations (e.g. MSVC), double and long double
00255     // have the same size.
00256     EXPECT_TRUE((LosslessArithmeticConvertible<long double, double>::value));
00257   } else {
00258     EXPECT_FALSE((LosslessArithmeticConvertible<long double, double>::value));
00259   }
00260 }
00261 
00262 // Tests the TupleMatches() template function.
00263 
00264 TEST(TupleMatchesTest, WorksForSize0) {
00265   tuple<> matchers;
00266   tuple<> values;
00267 
00268   EXPECT_TRUE(TupleMatches(matchers, values));
00269 }
00270 
00271 TEST(TupleMatchesTest, WorksForSize1) {
00272   tuple<Matcher<int> > matchers(Eq(1));
00273   tuple<int> values1(1),
00274       values2(2);
00275 
00276   EXPECT_TRUE(TupleMatches(matchers, values1));
00277   EXPECT_FALSE(TupleMatches(matchers, values2));
00278 }
00279 
00280 TEST(TupleMatchesTest, WorksForSize2) {
00281   tuple<Matcher<int>, Matcher<char> > matchers(Eq(1), Eq('a'));
00282   tuple<int, char> values1(1, 'a'),
00283       values2(1, 'b'),
00284       values3(2, 'a'),
00285       values4(2, 'b');
00286 
00287   EXPECT_TRUE(TupleMatches(matchers, values1));
00288   EXPECT_FALSE(TupleMatches(matchers, values2));
00289   EXPECT_FALSE(TupleMatches(matchers, values3));
00290   EXPECT_FALSE(TupleMatches(matchers, values4));
00291 }
00292 
00293 TEST(TupleMatchesTest, WorksForSize5) {
00294   tuple<Matcher<int>, Matcher<char>, Matcher<bool>, Matcher<long>,  // NOLINT
00295       Matcher<string> >
00296       matchers(Eq(1), Eq('a'), Eq(true), Eq(2L), Eq("hi"));
00297   tuple<int, char, bool, long, string>  // NOLINT
00298       values1(1, 'a', true, 2L, "hi"),
00299       values2(1, 'a', true, 2L, "hello"),
00300       values3(2, 'a', true, 2L, "hi");
00301 
00302   EXPECT_TRUE(TupleMatches(matchers, values1));
00303   EXPECT_FALSE(TupleMatches(matchers, values2));
00304   EXPECT_FALSE(TupleMatches(matchers, values3));
00305 }
00306 
00307 // Tests that Assert(true, ...) succeeds.
00308 TEST(AssertTest, SucceedsOnTrue) {
00309   Assert(true, __FILE__, __LINE__, "This should succeed.");
00310   Assert(true, __FILE__, __LINE__);  // This should succeed too.
00311 }
00312 
00313 // Tests that Assert(false, ...) generates a fatal failure.
00314 TEST(AssertTest, FailsFatallyOnFalse) {
00315   EXPECT_DEATH_IF_SUPPORTED({
00316     Assert(false, __FILE__, __LINE__, "This should fail.");
00317   }, "");
00318 
00319   EXPECT_DEATH_IF_SUPPORTED({
00320     Assert(false, __FILE__, __LINE__);
00321   }, "");
00322 }
00323 
00324 // Tests that Expect(true, ...) succeeds.
00325 TEST(ExpectTest, SucceedsOnTrue) {
00326   Expect(true, __FILE__, __LINE__, "This should succeed.");
00327   Expect(true, __FILE__, __LINE__);  // This should succeed too.
00328 }
00329 
00330 // Tests that Expect(false, ...) generates a non-fatal failure.
00331 TEST(ExpectTest, FailsNonfatallyOnFalse) {
00332   EXPECT_NONFATAL_FAILURE({  // NOLINT
00333     Expect(false, __FILE__, __LINE__, "This should fail.");
00334   }, "This should fail");
00335 
00336   EXPECT_NONFATAL_FAILURE({  // NOLINT
00337     Expect(false, __FILE__, __LINE__);
00338   }, "Expectation failed");
00339 }
00340 
00341 // Tests LogIsVisible().
00342 
00343 class LogIsVisibleTest : public ::testing::Test {
00344  protected:
00345   virtual void SetUp() {
00346     original_verbose_ = GMOCK_FLAG(verbose);
00347   }
00348 
00349   virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; }
00350 
00351   string original_verbose_;
00352 };
00353 
00354 TEST_F(LogIsVisibleTest, AlwaysReturnsTrueIfVerbosityIsInfo) {
00355   GMOCK_FLAG(verbose) = kInfoVerbosity;
00356   EXPECT_TRUE(LogIsVisible(kInfo));
00357   EXPECT_TRUE(LogIsVisible(kWarning));
00358 }
00359 
00360 TEST_F(LogIsVisibleTest, AlwaysReturnsFalseIfVerbosityIsError) {
00361   GMOCK_FLAG(verbose) = kErrorVerbosity;
00362   EXPECT_FALSE(LogIsVisible(kInfo));
00363   EXPECT_FALSE(LogIsVisible(kWarning));
00364 }
00365 
00366 TEST_F(LogIsVisibleTest, WorksWhenVerbosityIsWarning) {
00367   GMOCK_FLAG(verbose) = kWarningVerbosity;
00368   EXPECT_FALSE(LogIsVisible(kInfo));
00369   EXPECT_TRUE(LogIsVisible(kWarning));
00370 }
00371 
00372 #if GTEST_HAS_STREAM_REDIRECTION
00373 
00374 // Tests the Log() function.
00375 
00376 // Verifies that Log() behaves correctly for the given verbosity level
00377 // and log severity.
00378 void TestLogWithSeverity(const string& verbosity, LogSeverity severity,
00379                          bool should_print) {
00380   const string old_flag = GMOCK_FLAG(verbose);
00381   GMOCK_FLAG(verbose) = verbosity;
00382   CaptureStdout();
00383   Log(severity, "Test log.\n", 0);
00384   if (should_print) {
00385     EXPECT_THAT(GetCapturedStdout().c_str(),
00386                 ContainsRegex(
00387                     severity == kWarning ?
00388                     "^\nGMOCK WARNING:\nTest log\\.\nStack trace:\n" :
00389                     "^\nTest log\\.\nStack trace:\n"));
00390   } else {
00391     EXPECT_STREQ("", GetCapturedStdout().c_str());
00392   }
00393   GMOCK_FLAG(verbose) = old_flag;
00394 }
00395 
00396 // Tests that when the stack_frames_to_skip parameter is negative,
00397 // Log() doesn't include the stack trace in the output.
00398 TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
00399   const string saved_flag = GMOCK_FLAG(verbose);
00400   GMOCK_FLAG(verbose) = kInfoVerbosity;
00401   CaptureStdout();
00402   Log(kInfo, "Test log.\n", -1);
00403   EXPECT_STREQ("\nTest log.\n", GetCapturedStdout().c_str());
00404   GMOCK_FLAG(verbose) = saved_flag;
00405 }
00406 
00407 // Tests that in opt mode, a positive stack_frames_to_skip argument is
00408 // treated as 0.
00409 TEST(LogTest, NoSkippingStackFrameInOptMode) {
00410   CaptureStdout();
00411   Log(kWarning, "Test log.\n", 100);
00412   const string log = GetCapturedStdout();
00413 
00414 # if defined(NDEBUG) && GTEST_GOOGLE3_MODE_
00415 
00416   // In opt mode, no stack frame should be skipped.
00417   EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
00418                                  "Test log\\.\n"
00419                                  "Stack trace:\n"
00420                                  ".+"));
00421 # else
00422 
00423   // In dbg mode, the stack frames should be skipped.
00424   EXPECT_STREQ("\nGMOCK WARNING:\n"
00425                "Test log.\n"
00426                "Stack trace:\n", log.c_str());
00427 # endif
00428 }
00429 
00430 // Tests that all logs are printed when the value of the
00431 // --gmock_verbose flag is "info".
00432 TEST(LogTest, AllLogsArePrintedWhenVerbosityIsInfo) {
00433   TestLogWithSeverity(kInfoVerbosity, kInfo, true);
00434   TestLogWithSeverity(kInfoVerbosity, kWarning, true);
00435 }
00436 
00437 // Tests that only warnings are printed when the value of the
00438 // --gmock_verbose flag is "warning".
00439 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsWarning) {
00440   TestLogWithSeverity(kWarningVerbosity, kInfo, false);
00441   TestLogWithSeverity(kWarningVerbosity, kWarning, true);
00442 }
00443 
00444 // Tests that no logs are printed when the value of the
00445 // --gmock_verbose flag is "error".
00446 TEST(LogTest, NoLogsArePrintedWhenVerbosityIsError) {
00447   TestLogWithSeverity(kErrorVerbosity, kInfo, false);
00448   TestLogWithSeverity(kErrorVerbosity, kWarning, false);
00449 }
00450 
00451 // Tests that only warnings are printed when the value of the
00452 // --gmock_verbose flag is invalid.
00453 TEST(LogTest, OnlyWarningsArePrintedWhenVerbosityIsInvalid) {
00454   TestLogWithSeverity("invalid", kInfo, false);
00455   TestLogWithSeverity("invalid", kWarning, true);
00456 }
00457 
00458 #endif  // GTEST_HAS_STREAM_REDIRECTION
00459 
00460 TEST(TypeTraitsTest, true_type) {
00461   EXPECT_TRUE(true_type::value);
00462 }
00463 
00464 TEST(TypeTraitsTest, false_type) {
00465   EXPECT_FALSE(false_type::value);
00466 }
00467 
00468 TEST(TypeTraitsTest, is_reference) {
00469   EXPECT_FALSE(is_reference<int>::value);
00470   EXPECT_FALSE(is_reference<char*>::value);
00471   EXPECT_TRUE(is_reference<const int&>::value);
00472 }
00473 
00474 TEST(TypeTraitsTest, is_pointer) {
00475   EXPECT_FALSE(is_pointer<int>::value);
00476   EXPECT_FALSE(is_pointer<char&>::value);
00477   EXPECT_TRUE(is_pointer<const int*>::value);
00478 }
00479 
00480 TEST(TypeTraitsTest, type_equals) {
00481   EXPECT_FALSE((type_equals<int, const int>::value));
00482   EXPECT_FALSE((type_equals<int, int&>::value));
00483   EXPECT_FALSE((type_equals<int, double>::value));
00484   EXPECT_TRUE((type_equals<char, char>::value));
00485 }
00486 
00487 TEST(TypeTraitsTest, remove_reference) {
00488   EXPECT_TRUE((type_equals<char, remove_reference<char&>::type>::value));
00489   EXPECT_TRUE((type_equals<const int,
00490                remove_reference<const int&>::type>::value));
00491   EXPECT_TRUE((type_equals<int, remove_reference<int>::type>::value));
00492   EXPECT_TRUE((type_equals<double*, remove_reference<double*>::type>::value));
00493 }
00494 
00495 #if GTEST_HAS_STREAM_REDIRECTION
00496 
00497 // Verifies that Log() behaves correctly for the given verbosity level
00498 // and log severity.
00499 std::string GrabOutput(void(*logger)(), const char* verbosity) {
00500   const string saved_flag = GMOCK_FLAG(verbose);
00501   GMOCK_FLAG(verbose) = verbosity;
00502   CaptureStdout();
00503   logger();
00504   GMOCK_FLAG(verbose) = saved_flag;
00505   return GetCapturedStdout();
00506 }
00507 
00508 class DummyMock {
00509  public:
00510   MOCK_METHOD0(TestMethod, void());
00511   MOCK_METHOD1(TestMethodArg, void(int dummy));
00512 };
00513 
00514 void ExpectCallLogger() {
00515   DummyMock mock;
00516   EXPECT_CALL(mock, TestMethod());
00517   mock.TestMethod();
00518 };
00519 
00520 // Verifies that EXPECT_CALL logs if the --gmock_verbose flag is set to "info".
00521 TEST(ExpectCallTest, LogsWhenVerbosityIsInfo) {
00522   EXPECT_THAT(std::string(GrabOutput(ExpectCallLogger, kInfoVerbosity)),
00523               HasSubstr("EXPECT_CALL(mock, TestMethod())"));
00524 }
00525 
00526 // Verifies that EXPECT_CALL doesn't log
00527 // if the --gmock_verbose flag is set to "warning".
00528 TEST(ExpectCallTest, DoesNotLogWhenVerbosityIsWarning) {
00529   EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kWarningVerbosity).c_str());
00530 }
00531 
00532 // Verifies that EXPECT_CALL doesn't log
00533 // if the --gmock_verbose flag is set to "error".
00534 TEST(ExpectCallTest,  DoesNotLogWhenVerbosityIsError) {
00535   EXPECT_STREQ("", GrabOutput(ExpectCallLogger, kErrorVerbosity).c_str());
00536 }
00537 
00538 void OnCallLogger() {
00539   DummyMock mock;
00540   ON_CALL(mock, TestMethod());
00541 };
00542 
00543 // Verifies that ON_CALL logs if the --gmock_verbose flag is set to "info".
00544 TEST(OnCallTest, LogsWhenVerbosityIsInfo) {
00545   EXPECT_THAT(std::string(GrabOutput(OnCallLogger, kInfoVerbosity)),
00546               HasSubstr("ON_CALL(mock, TestMethod())"));
00547 }
00548 
00549 // Verifies that ON_CALL doesn't log
00550 // if the --gmock_verbose flag is set to "warning".
00551 TEST(OnCallTest, DoesNotLogWhenVerbosityIsWarning) {
00552   EXPECT_STREQ("", GrabOutput(OnCallLogger, kWarningVerbosity).c_str());
00553 }
00554 
00555 // Verifies that ON_CALL doesn't log if
00556 // the --gmock_verbose flag is set to "error".
00557 TEST(OnCallTest, DoesNotLogWhenVerbosityIsError) {
00558   EXPECT_STREQ("", GrabOutput(OnCallLogger, kErrorVerbosity).c_str());
00559 }
00560 
00561 void OnCallAnyArgumentLogger() {
00562   DummyMock mock;
00563   ON_CALL(mock, TestMethodArg(_));
00564 }
00565 
00566 // Verifies that ON_CALL prints provided _ argument.
00567 TEST(OnCallTest, LogsAnythingArgument) {
00568   EXPECT_THAT(std::string(GrabOutput(OnCallAnyArgumentLogger, kInfoVerbosity)),
00569               HasSubstr("ON_CALL(mock, TestMethodArg(_)"));
00570 }
00571 
00572 #endif  // GTEST_HAS_STREAM_REDIRECTION
00573 
00574 // Tests StlContainerView.
00575 
00576 TEST(StlContainerViewTest, WorksForStlContainer) {
00577   StaticAssertTypeEq<std::vector<int>,
00578       StlContainerView<std::vector<int> >::type>();
00579   StaticAssertTypeEq<const std::vector<double>&,
00580       StlContainerView<std::vector<double> >::const_reference>();
00581 
00582   typedef std::vector<char> Chars;
00583   Chars v1;
00584   const Chars& v2(StlContainerView<Chars>::ConstReference(v1));
00585   EXPECT_EQ(&v1, &v2);
00586 
00587   v1.push_back('a');
00588   Chars v3 = StlContainerView<Chars>::Copy(v1);
00589   EXPECT_THAT(v3, Eq(v3));
00590 }
00591 
00592 TEST(StlContainerViewTest, WorksForStaticNativeArray) {
00593   StaticAssertTypeEq<NativeArray<int>,
00594       StlContainerView<int[3]>::type>();
00595   StaticAssertTypeEq<NativeArray<double>,
00596       StlContainerView<const double[4]>::type>();
00597   StaticAssertTypeEq<NativeArray<char[3]>,
00598       StlContainerView<const char[2][3]>::type>();
00599 
00600   StaticAssertTypeEq<const NativeArray<int>,
00601       StlContainerView<int[2]>::const_reference>();
00602 
00603   int a1[3] = { 0, 1, 2 };
00604   NativeArray<int> a2 = StlContainerView<int[3]>::ConstReference(a1);
00605   EXPECT_EQ(3U, a2.size());
00606   EXPECT_EQ(a1, a2.begin());
00607 
00608   const NativeArray<int> a3 = StlContainerView<int[3]>::Copy(a1);
00609   ASSERT_EQ(3U, a3.size());
00610   EXPECT_EQ(0, a3.begin()[0]);
00611   EXPECT_EQ(1, a3.begin()[1]);
00612   EXPECT_EQ(2, a3.begin()[2]);
00613 
00614   // Makes sure a1 and a3 aren't aliases.
00615   a1[0] = 3;
00616   EXPECT_EQ(0, a3.begin()[0]);
00617 }
00618 
00619 TEST(StlContainerViewTest, WorksForDynamicNativeArray) {
00620   StaticAssertTypeEq<NativeArray<int>,
00621       StlContainerView<tuple<const int*, size_t> >::type>();
00622   StaticAssertTypeEq<NativeArray<double>,
00623       StlContainerView<tuple<linked_ptr<double>, int> >::type>();
00624 
00625   StaticAssertTypeEq<const NativeArray<int>,
00626       StlContainerView<tuple<const int*, int> >::const_reference>();
00627 
00628   int a1[3] = { 0, 1, 2 };
00629   const int* const p1 = a1;
00630   NativeArray<int> a2 = StlContainerView<tuple<const int*, int> >::
00631       ConstReference(make_tuple(p1, 3));
00632   EXPECT_EQ(3U, a2.size());
00633   EXPECT_EQ(a1, a2.begin());
00634 
00635   const NativeArray<int> a3 = StlContainerView<tuple<int*, size_t> >::
00636       Copy(make_tuple(static_cast<int*>(a1), 3));
00637   ASSERT_EQ(3U, a3.size());
00638   EXPECT_EQ(0, a3.begin()[0]);
00639   EXPECT_EQ(1, a3.begin()[1]);
00640   EXPECT_EQ(2, a3.begin()[2]);
00641 
00642   // Makes sure a1 and a3 aren't aliases.
00643   a1[0] = 3;
00644   EXPECT_EQ(0, a3.begin()[0]);
00645 }
00646 
00647 }  // namespace
00648 }  // namespace internal
00649 }  // namespace testing


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:41