OLD | NEW |
(Empty) | |
| 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 // |
| 30 // Author: wan@google.com (Zhanyong Wan) |
| 31 |
| 32 // Google Mock - a framework for writing C++ mock classes. |
| 33 // |
| 34 // This file tests the universal value printer. |
| 35 |
| 36 #include <gmock/gmock-printers.h> |
| 37 |
| 38 #include <ctype.h> |
| 39 #include <limits.h> |
| 40 #include <string.h> |
| 41 #include <algorithm> |
| 42 #include <deque> |
| 43 #include <list> |
| 44 #include <map> |
| 45 #include <set> |
| 46 #include <sstream> |
| 47 #include <string> |
| 48 #include <utility> |
| 49 #include <vector> |
| 50 #include <gmock/gmock-generated-matchers.h> |
| 51 #include <gmock/gmock-matchers.h> |
| 52 #include <gmock/internal/gmock-port.h> |
| 53 #include <gtest/gtest.h> |
| 54 |
| 55 // hash_map and hash_set are available on Windows. |
| 56 #if GTEST_OS_WINDOWS |
| 57 #define GMOCK_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. |
| 58 #include <hash_map> // NOLINT |
| 59 #define GMOCK_HAS_HASH_SET_ 1 // Indicates that hash_set is available. |
| 60 #include <hash_set> // NOLINT |
| 61 #endif // GTEST_OS_WINDOWS |
| 62 |
| 63 // Some user-defined types for testing the universal value printer. |
| 64 |
| 65 // A user-defined unprintable class template in the global namespace. |
| 66 template <typename T> |
| 67 class UnprintableTemplateInGlobal { |
| 68 public: |
| 69 UnprintableTemplateInGlobal() : value_() {} |
| 70 private: |
| 71 T value_; |
| 72 }; |
| 73 |
| 74 // A user-defined streamable type in the global namespace. |
| 75 class StreamableInGlobal { |
| 76 public: |
| 77 virtual ~StreamableInGlobal() {} |
| 78 }; |
| 79 |
| 80 inline void operator<<(::std::ostream& os, const StreamableInGlobal& x) { |
| 81 os << "StreamableInGlobal"; |
| 82 } |
| 83 |
| 84 namespace foo { |
| 85 |
| 86 // A user-defined unprintable type in a user namespace. |
| 87 class UnprintableInFoo { |
| 88 public: |
| 89 UnprintableInFoo() : x_(0x12EF), y_(0xAB34), z_(0) {} |
| 90 private: |
| 91 testing::internal::Int32 x_; |
| 92 testing::internal::Int32 y_; |
| 93 double z_; |
| 94 }; |
| 95 |
| 96 // A user-defined printable type in a user-chosen namespace. |
| 97 struct PrintableViaPrintTo { |
| 98 PrintableViaPrintTo() : value() {} |
| 99 int value; |
| 100 }; |
| 101 |
| 102 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { |
| 103 *os << "PrintableViaPrintTo: " << x.value; |
| 104 } |
| 105 |
| 106 // A user-defined printable class template in a user-chosen namespace. |
| 107 template <typename T> |
| 108 class PrintableViaPrintToTemplate { |
| 109 public: |
| 110 explicit PrintableViaPrintToTemplate(const T& value) : value_(value) {} |
| 111 |
| 112 const T& value() const { return value_; } |
| 113 private: |
| 114 T value_; |
| 115 }; |
| 116 |
| 117 template <typename T> |
| 118 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { |
| 119 *os << "PrintableViaPrintToTemplate: " << x.value(); |
| 120 } |
| 121 |
| 122 // A user-defined streamable class template in a user namespace. |
| 123 template <typename T> |
| 124 class StreamableTemplateInFoo { |
| 125 public: |
| 126 StreamableTemplateInFoo() : value_() {} |
| 127 |
| 128 const T& value() const { return value_; } |
| 129 private: |
| 130 T value_; |
| 131 }; |
| 132 |
| 133 template <typename T> |
| 134 inline ::std::ostream& operator<<(::std::ostream& os, |
| 135 const StreamableTemplateInFoo<T>& x) { |
| 136 return os << "StreamableTemplateInFoo: " << x.value(); |
| 137 } |
| 138 |
| 139 } // namespace foo |
| 140 |
| 141 namespace testing { |
| 142 namespace gmock_printers_test { |
| 143 |
| 144 using ::std::deque; |
| 145 using ::std::list; |
| 146 using ::std::make_pair; |
| 147 using ::std::map; |
| 148 using ::std::multimap; |
| 149 using ::std::multiset; |
| 150 using ::std::pair; |
| 151 using ::std::set; |
| 152 using ::std::tr1::make_tuple; |
| 153 using ::std::tr1::tuple; |
| 154 using ::std::vector; |
| 155 using ::testing::ElementsAre; |
| 156 using ::testing::StartsWith; |
| 157 using ::testing::internal::Strings; |
| 158 using ::testing::internal::UniversalTersePrint; |
| 159 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; |
| 160 using ::testing::internal::UniversalPrinter; |
| 161 using ::testing::internal::string; |
| 162 |
| 163 #if GTEST_OS_WINDOWS |
| 164 // MSVC defines the following classes in the ::stdext namespace while |
| 165 // gcc defines them in the :: namespace. Note that they are not part |
| 166 // of the C++ standard. |
| 167 |
| 168 using ::stdext::hash_map; |
| 169 using ::stdext::hash_set; |
| 170 using ::stdext::hash_multimap; |
| 171 using ::stdext::hash_multiset; |
| 172 |
| 173 #endif // GTEST_OS_WINDOWS |
| 174 |
| 175 // Prints a value to a string using the universal value printer. This |
| 176 // is a helper for testing UniversalPrinter<T>::Print() for various types. |
| 177 template <typename T> |
| 178 string Print(const T& value) { |
| 179 ::std::stringstream ss; |
| 180 UniversalPrinter<T>::Print(value, &ss); |
| 181 return ss.str(); |
| 182 } |
| 183 |
| 184 // Prints a value passed by reference to a string, using the universal |
| 185 // value printer. This is a helper for testing |
| 186 // UniversalPrinter<T&>::Print() for various types. |
| 187 template <typename T> |
| 188 string PrintByRef(const T& value) { |
| 189 ::std::stringstream ss; |
| 190 UniversalPrinter<T&>::Print(value, &ss); |
| 191 return ss.str(); |
| 192 } |
| 193 |
| 194 // Tests printing various char types. |
| 195 |
| 196 // char. |
| 197 TEST(PrintCharTest, PlainChar) { |
| 198 EXPECT_EQ("'\\0'", Print('\0')); |
| 199 EXPECT_EQ("'\\'' (39)", Print('\'')); |
| 200 EXPECT_EQ("'\"' (34)", Print('"')); |
| 201 EXPECT_EQ("'\\?' (63)", Print('\?')); |
| 202 EXPECT_EQ("'\\\\' (92)", Print('\\')); |
| 203 EXPECT_EQ("'\\a' (7)", Print('\a')); |
| 204 EXPECT_EQ("'\\b' (8)", Print('\b')); |
| 205 EXPECT_EQ("'\\f' (12)", Print('\f')); |
| 206 EXPECT_EQ("'\\n' (10)", Print('\n')); |
| 207 EXPECT_EQ("'\\r' (13)", Print('\r')); |
| 208 EXPECT_EQ("'\\t' (9)", Print('\t')); |
| 209 EXPECT_EQ("'\\v' (11)", Print('\v')); |
| 210 EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); |
| 211 EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); |
| 212 EXPECT_EQ("' ' (32)", Print(' ')); |
| 213 EXPECT_EQ("'a' (97)", Print('a')); |
| 214 } |
| 215 |
| 216 // signed char. |
| 217 TEST(PrintCharTest, SignedChar) { |
| 218 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); |
| 219 EXPECT_EQ("'\\xCE' (-50)", |
| 220 Print(static_cast<signed char>(-50))); |
| 221 } |
| 222 |
| 223 // unsigned char. |
| 224 TEST(PrintCharTest, UnsignedChar) { |
| 225 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); |
| 226 EXPECT_EQ("'b' (98)", |
| 227 Print(static_cast<unsigned char>('b'))); |
| 228 } |
| 229 |
| 230 // Tests printing other simple, built-in types. |
| 231 |
| 232 // bool. |
| 233 TEST(PrintBuiltInTypeTest, Bool) { |
| 234 EXPECT_EQ("false", Print(false)); |
| 235 EXPECT_EQ("true", Print(true)); |
| 236 } |
| 237 |
| 238 // wchar_t. |
| 239 TEST(PrintBuiltInTypeTest, Wchar_t) { |
| 240 EXPECT_EQ("L'\\0'", Print(L'\0')); |
| 241 EXPECT_EQ("L'\\'' (39)", Print(L'\'')); |
| 242 EXPECT_EQ("L'\"' (34)", Print(L'"')); |
| 243 EXPECT_EQ("L'\\?' (63)", Print(L'\?')); |
| 244 EXPECT_EQ("L'\\\\' (92)", Print(L'\\')); |
| 245 EXPECT_EQ("L'\\a' (7)", Print(L'\a')); |
| 246 EXPECT_EQ("L'\\b' (8)", Print(L'\b')); |
| 247 EXPECT_EQ("L'\\f' (12)", Print(L'\f')); |
| 248 EXPECT_EQ("L'\\n' (10)", Print(L'\n')); |
| 249 EXPECT_EQ("L'\\r' (13)", Print(L'\r')); |
| 250 EXPECT_EQ("L'\\t' (9)", Print(L'\t')); |
| 251 EXPECT_EQ("L'\\v' (11)", Print(L'\v')); |
| 252 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); |
| 253 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); |
| 254 EXPECT_EQ("L' ' (32)", Print(L' ')); |
| 255 EXPECT_EQ("L'a' (97)", Print(L'a')); |
| 256 EXPECT_EQ("L'\\x576' (1398)", Print(L'\x576')); |
| 257 EXPECT_EQ("L'\\xC74D' (51021)", Print(L'\xC74D')); |
| 258 } |
| 259 |
| 260 // Test that Int64 provides more storage than wchar_t. |
| 261 TEST(PrintTypeSizeTest, Wchar_t) { |
| 262 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); |
| 263 } |
| 264 |
| 265 // Various integer types. |
| 266 TEST(PrintBuiltInTypeTest, Integer) { |
| 267 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 |
| 268 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 |
| 269 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 |
| 270 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 |
| 271 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 |
| 272 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 |
| 273 EXPECT_EQ("18446744073709551615", |
| 274 Print(static_cast<testing::internal::UInt64>(-1))); // uint64 |
| 275 EXPECT_EQ("-9223372036854775808", |
| 276 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64 |
| 277 } |
| 278 |
| 279 // Size types. |
| 280 TEST(PrintBuiltInTypeTest, Size_t) { |
| 281 EXPECT_EQ("1", Print(sizeof('a'))); // size_t. |
| 282 #if !GTEST_OS_WINDOWS |
| 283 // Windows has no ssize_t type. |
| 284 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. |
| 285 #endif // !GTEST_OS_WINDOWS |
| 286 } |
| 287 |
| 288 // Floating-points. |
| 289 TEST(PrintBuiltInTypeTest, FloatingPoints) { |
| 290 EXPECT_EQ("1.5", Print(1.5f)); // float |
| 291 EXPECT_EQ("-2.5", Print(-2.5)); // double |
| 292 } |
| 293 |
| 294 // Since ::std::stringstream::operator<<(const void *) formats the pointer |
| 295 // output differently with different compilers, we have to create the expected |
| 296 // output first and use it as our expectation. |
| 297 static string PrintPointer(const void *p) { |
| 298 ::std::stringstream expected_result_stream; |
| 299 expected_result_stream << p; |
| 300 return expected_result_stream.str(); |
| 301 } |
| 302 |
| 303 // Tests printing C strings. |
| 304 |
| 305 // const char*. |
| 306 TEST(PrintCStringTest, Const) { |
| 307 const char* p = "World"; |
| 308 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); |
| 309 } |
| 310 |
| 311 // char*. |
| 312 TEST(PrintCStringTest, NonConst) { |
| 313 char p[] = "Hi"; |
| 314 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", |
| 315 Print(static_cast<char*>(p))); |
| 316 } |
| 317 |
| 318 // NULL C string. |
| 319 TEST(PrintCStringTest, Null) { |
| 320 const char* p = NULL; |
| 321 EXPECT_EQ("NULL", Print(p)); |
| 322 } |
| 323 |
| 324 // Tests that C strings are escaped properly. |
| 325 TEST(PrintCStringTest, EscapesProperly) { |
| 326 const char* p = "'\"\?\\\a\b\f\n\r\t\v\x7F\xFF a"; |
| 327 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"\\?\\\\\\a\\b\\f" |
| 328 "\\n\\r\\t\\v\\x7F\\xFF a\"", |
| 329 Print(p)); |
| 330 } |
| 331 |
| 332 |
| 333 |
| 334 // MSVC compiler can be configured to define whar_t as a typedef |
| 335 // of unsigned short. Defining an overload for const wchar_t* in that case |
| 336 // would cause pointers to unsigned shorts be printed as wide strings, |
| 337 // possibly accessing more memory than intended and causing invalid |
| 338 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when |
| 339 // wchar_t is implemented as a native type. |
| 340 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) |
| 341 |
| 342 // const wchar_t*. |
| 343 TEST(PrintWideCStringTest, Const) { |
| 344 const wchar_t* p = L"World"; |
| 345 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); |
| 346 } |
| 347 |
| 348 // wchar_t*. |
| 349 TEST(PrintWideCStringTest, NonConst) { |
| 350 wchar_t p[] = L"Hi"; |
| 351 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", |
| 352 Print(static_cast<wchar_t*>(p))); |
| 353 } |
| 354 |
| 355 // NULL wide C string. |
| 356 TEST(PrintWideCStringTest, Null) { |
| 357 const wchar_t* p = NULL; |
| 358 EXPECT_EQ("NULL", Print(p)); |
| 359 } |
| 360 |
| 361 // Tests that wide C strings are escaped properly. |
| 362 TEST(PrintWideCStringTest, EscapesProperly) { |
| 363 const wchar_t* p = L"'\"\?\\\a\b\f\n\r\t\v\xD3\x576\x8D3\xC74D a"; |
| 364 EXPECT_EQ(PrintPointer(p) + " pointing to L\"'\\\"\\?\\\\\\a\\b\\f" |
| 365 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", |
| 366 Print(p)); |
| 367 } |
| 368 #endif // native wchar_t |
| 369 |
| 370 // Tests printing pointers to other char types. |
| 371 |
| 372 // signed char*. |
| 373 TEST(PrintCharPointerTest, SignedChar) { |
| 374 signed char* p = reinterpret_cast<signed char*>(0x1234); |
| 375 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 376 p = NULL; |
| 377 EXPECT_EQ("NULL", Print(p)); |
| 378 } |
| 379 |
| 380 // const signed char*. |
| 381 TEST(PrintCharPointerTest, ConstSignedChar) { |
| 382 signed char* p = reinterpret_cast<signed char*>(0x1234); |
| 383 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 384 p = NULL; |
| 385 EXPECT_EQ("NULL", Print(p)); |
| 386 } |
| 387 |
| 388 // unsigned char*. |
| 389 TEST(PrintCharPointerTest, UnsignedChar) { |
| 390 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234); |
| 391 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 392 p = NULL; |
| 393 EXPECT_EQ("NULL", Print(p)); |
| 394 } |
| 395 |
| 396 // const unsigned char*. |
| 397 TEST(PrintCharPointerTest, ConstUnsignedChar) { |
| 398 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234); |
| 399 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 400 p = NULL; |
| 401 EXPECT_EQ("NULL", Print(p)); |
| 402 } |
| 403 |
| 404 // Tests printing pointers to simple, built-in types. |
| 405 |
| 406 // bool*. |
| 407 TEST(PrintPointerToBuiltInTypeTest, Bool) { |
| 408 bool* p = reinterpret_cast<bool*>(0xABCD); |
| 409 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 410 p = NULL; |
| 411 EXPECT_EQ("NULL", Print(p)); |
| 412 } |
| 413 |
| 414 // void*. |
| 415 TEST(PrintPointerToBuiltInTypeTest, Void) { |
| 416 void* p = reinterpret_cast<void*>(0xABCD); |
| 417 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 418 p = NULL; |
| 419 EXPECT_EQ("NULL", Print(p)); |
| 420 } |
| 421 |
| 422 // const void*. |
| 423 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { |
| 424 const void* p = reinterpret_cast<const void*>(0xABCD); |
| 425 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 426 p = NULL; |
| 427 EXPECT_EQ("NULL", Print(p)); |
| 428 } |
| 429 |
| 430 // Tests printing pointers to pointers. |
| 431 TEST(PrintPointerToPointerTest, IntPointerPointer) { |
| 432 int** p = reinterpret_cast<int**>(0xABCD); |
| 433 EXPECT_EQ(PrintPointer(p), Print(p)); |
| 434 p = NULL; |
| 435 EXPECT_EQ("NULL", Print(p)); |
| 436 } |
| 437 |
| 438 // Tests printing (non-member) function pointers. |
| 439 |
| 440 void MyFunction(int n) {} |
| 441 |
| 442 TEST(PrintPointerTest, NonMemberFunctionPointer) { |
| 443 EXPECT_EQ(PrintPointer(reinterpret_cast<const void*>(&MyFunction)), |
| 444 Print(&MyFunction)); |
| 445 int (*p)(bool) = NULL; // NOLINT |
| 446 EXPECT_EQ("NULL", Print(p)); |
| 447 } |
| 448 |
| 449 // Tests printing member variable pointers. Although they are called |
| 450 // pointers, they don't point to a location in the address space. |
| 451 // Their representation is implementation-defined. Thus they will be |
| 452 // printed as raw bytes. |
| 453 |
| 454 struct Foo { |
| 455 public: |
| 456 virtual ~Foo() {} |
| 457 int MyMethod(char x) { return x + 1; } |
| 458 virtual char MyVirtualMethod(int n) { return 'a'; } |
| 459 |
| 460 int value; |
| 461 }; |
| 462 |
| 463 TEST(PrintPointerTest, MemberVariablePointer) { |
| 464 EXPECT_THAT(Print(&Foo::value), |
| 465 StartsWith(Print(sizeof(&Foo::value)) + "-byte object ")); |
| 466 int (Foo::*p) = NULL; // NOLINT |
| 467 EXPECT_THAT(Print(p), |
| 468 StartsWith(Print(sizeof(p)) + "-byte object ")); |
| 469 } |
| 470 |
| 471 // Tests printing member function pointers. Although they are called |
| 472 // pointers, they don't point to a location in the address space. |
| 473 // Their representation is implementation-defined. Thus they will be |
| 474 // printed as raw bytes. |
| 475 TEST(PrintPointerTest, MemberFunctionPointer) { |
| 476 EXPECT_THAT(Print(&Foo::MyMethod), |
| 477 StartsWith(Print(sizeof(&Foo::MyMethod)) + "-byte object ")); |
| 478 EXPECT_THAT(Print(&Foo::MyVirtualMethod), |
| 479 StartsWith(Print(sizeof((&Foo::MyVirtualMethod))) |
| 480 + "-byte object ")); |
| 481 int (Foo::*p)(char) = NULL; // NOLINT |
| 482 EXPECT_THAT(Print(p), |
| 483 StartsWith(Print(sizeof(p)) + "-byte object ")); |
| 484 } |
| 485 |
| 486 // Tests printing C arrays. |
| 487 |
| 488 // One-dimensional array. |
| 489 |
| 490 void ArrayHelper1(int (&a)[5]) { // NOLINT |
| 491 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", Print(a)); |
| 492 } |
| 493 |
| 494 TEST(PrintArrayTest, OneDimensionalArray) { |
| 495 int a[5] = { 1, 2, 3, 4, 5 }; |
| 496 ArrayHelper1(a); |
| 497 } |
| 498 |
| 499 // Two-dimensional array. |
| 500 |
| 501 void ArrayHelper2(int (&a)[2][5]) { // NOLINT |
| 502 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", Print(a)); |
| 503 } |
| 504 |
| 505 TEST(PrintArrayTest, TwoDimensionalArray) { |
| 506 int a[2][5] = { |
| 507 { 1, 2, 3, 4, 5 }, |
| 508 { 6, 7, 8, 9, 0 } |
| 509 }; |
| 510 ArrayHelper2(a); |
| 511 } |
| 512 |
| 513 // Array of const elements. |
| 514 |
| 515 void ArrayHelper3(const bool (&a)[1]) { // NOLINT |
| 516 EXPECT_EQ("{ false }", Print(a)); |
| 517 } |
| 518 |
| 519 TEST(PrintArrayTest, ConstArray) { |
| 520 const bool a[1] = { false }; |
| 521 ArrayHelper3(a); |
| 522 } |
| 523 |
| 524 // Char array. |
| 525 |
| 526 void ArrayHelper4(char (&a)[3]) { // NOLINT |
| 527 EXPECT_EQ(PrintPointer(a) + " pointing to \"Hi\"", Print(a)); |
| 528 } |
| 529 |
| 530 TEST(PrintArrayTest, CharArray) { |
| 531 char a[3] = "Hi"; |
| 532 ArrayHelper4(a); |
| 533 } |
| 534 |
| 535 // Const char array. |
| 536 |
| 537 void ArrayHelper5(const char (&a)[3]) { // NOLINT |
| 538 EXPECT_EQ(Print(a), PrintPointer(a) + " pointing to \"Hi\""); |
| 539 } |
| 540 |
| 541 TEST(PrintArrayTest, ConstCharArray) { |
| 542 const char a[3] = "Hi"; |
| 543 ArrayHelper5(a); |
| 544 } |
| 545 |
| 546 // Array of objects. |
| 547 TEST(PrintArrayTest, ObjectArray) { |
| 548 string a[3] = { "Hi", "Hello", "Ni hao" }; |
| 549 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", Print(a)); |
| 550 } |
| 551 |
| 552 // Array with many elements. |
| 553 TEST(PrintArrayTest, BigArray) { |
| 554 int a[100] = { 1, 2, 3 }; |
| 555 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", |
| 556 Print(a)); |
| 557 } |
| 558 |
| 559 // Tests printing ::string and ::std::string. |
| 560 |
| 561 #if GTEST_HAS_GLOBAL_STRING |
| 562 // ::string. |
| 563 TEST(PrintStringTest, StringInGlobalNamespace) { |
| 564 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; |
| 565 const ::string str(s, sizeof(s)); |
| 566 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", |
| 567 Print(str)); |
| 568 } |
| 569 #endif // GTEST_HAS_GLOBAL_STRING |
| 570 |
| 571 #if GTEST_HAS_STD_STRING |
| 572 // ::std::string. |
| 573 TEST(PrintStringTest, StringInStdNamespace) { |
| 574 const char s[] = "'\"\?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; |
| 575 const ::std::string str(s, sizeof(s)); |
| 576 EXPECT_EQ("\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", |
| 577 Print(str)); |
| 578 } |
| 579 #endif // GTEST_HAS_STD_STRING |
| 580 |
| 581 // Tests printing ::wstring and ::std::wstring. |
| 582 |
| 583 #if GTEST_HAS_GLOBAL_WSTRING |
| 584 // ::wstring. |
| 585 TEST(PrintWideStringTest, StringInGlobalNamespace) { |
| 586 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; |
| 587 const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); |
| 588 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" |
| 589 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", |
| 590 Print(str)); |
| 591 } |
| 592 #endif // GTEST_HAS_GLOBAL_WSTRING |
| 593 |
| 594 #if GTEST_HAS_STD_WSTRING |
| 595 // ::std::wstring. |
| 596 TEST(PrintWideStringTest, StringInStdNamespace) { |
| 597 const wchar_t s[] = L"'\"\?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; |
| 598 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); |
| 599 EXPECT_EQ("L\"'\\\"\\?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" |
| 600 "\\xD3\\x576\\x8D3\\xC74D a\\0\"", |
| 601 Print(str)); |
| 602 } |
| 603 #endif // GTEST_HAS_STD_WSTRING |
| 604 |
| 605 // Tests printing types that support generic streaming (i.e. streaming |
| 606 // to std::basic_ostream<Char, CharTraits> for any valid Char and |
| 607 // CharTraits types). |
| 608 |
| 609 // Tests printing a non-template type that supports generic streaming. |
| 610 |
| 611 class AllowsGenericStreaming {}; |
| 612 |
| 613 template <typename Char, typename CharTraits> |
| 614 std::basic_ostream<Char, CharTraits>& operator<<( |
| 615 std::basic_ostream<Char, CharTraits>& os, |
| 616 const AllowsGenericStreaming& a) { |
| 617 return os << "AllowsGenericStreaming"; |
| 618 } |
| 619 |
| 620 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { |
| 621 AllowsGenericStreaming a; |
| 622 EXPECT_EQ("AllowsGenericStreaming", Print(a)); |
| 623 } |
| 624 |
| 625 // Tests printing a template type that supports generic streaming. |
| 626 |
| 627 template <typename T> |
| 628 class AllowsGenericStreamingTemplate {}; |
| 629 |
| 630 template <typename Char, typename CharTraits, typename T> |
| 631 std::basic_ostream<Char, CharTraits>& operator<<( |
| 632 std::basic_ostream<Char, CharTraits>& os, |
| 633 const AllowsGenericStreamingTemplate<T>& a) { |
| 634 return os << "AllowsGenericStreamingTemplate"; |
| 635 } |
| 636 |
| 637 TEST(PrintTypeWithGenericStreamingTest, TemplateType) { |
| 638 AllowsGenericStreamingTemplate<int> a; |
| 639 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); |
| 640 } |
| 641 |
| 642 // Tests printing a type that supports generic streaming and can be |
| 643 // implicitly converted to another printable type. |
| 644 |
| 645 template <typename T> |
| 646 class AllowsGenericStreamingAndImplicitConversionTemplate { |
| 647 public: |
| 648 operator bool() const { return false; } |
| 649 }; |
| 650 |
| 651 template <typename Char, typename CharTraits, typename T> |
| 652 std::basic_ostream<Char, CharTraits>& operator<<( |
| 653 std::basic_ostream<Char, CharTraits>& os, |
| 654 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& a) { |
| 655 return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; |
| 656 } |
| 657 |
| 658 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { |
| 659 AllowsGenericStreamingAndImplicitConversionTemplate<int> a; |
| 660 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); |
| 661 } |
| 662 |
| 663 // Tests printing STL containers. |
| 664 |
| 665 TEST(PrintStlContainerTest, EmptyDeque) { |
| 666 deque<char> empty; |
| 667 EXPECT_EQ("{}", Print(empty)); |
| 668 } |
| 669 |
| 670 TEST(PrintStlContainerTest, NonEmptyDeque) { |
| 671 deque<int> non_empty; |
| 672 non_empty.push_back(1); |
| 673 non_empty.push_back(3); |
| 674 EXPECT_EQ("{ 1, 3 }", Print(non_empty)); |
| 675 } |
| 676 |
| 677 #if GMOCK_HAS_HASH_MAP_ |
| 678 |
| 679 TEST(PrintStlContainerTest, OneElementHashMap) { |
| 680 hash_map<int, char> map1; |
| 681 map1[1] = 'a'; |
| 682 EXPECT_EQ("{ (1, 'a' (97)) }", Print(map1)); |
| 683 } |
| 684 |
| 685 TEST(PrintStlContainerTest, HashMultiMap) { |
| 686 hash_multimap<int, bool> map1; |
| 687 map1.insert(make_pair(5, true)); |
| 688 map1.insert(make_pair(5, false)); |
| 689 |
| 690 // Elements of hash_multimap can be printed in any order. |
| 691 const string result = Print(map1); |
| 692 EXPECT_TRUE(result == "{ (5, true), (5, false) }" || |
| 693 result == "{ (5, false), (5, true) }") |
| 694 << " where Print(map1) returns \"" << result << "\"."; |
| 695 } |
| 696 |
| 697 #endif // GMOCK_HAS_HASH_MAP_ |
| 698 |
| 699 #if GMOCK_HAS_HASH_SET_ |
| 700 |
| 701 TEST(PrintStlContainerTest, HashSet) { |
| 702 hash_set<string> set1; |
| 703 set1.insert("hello"); |
| 704 EXPECT_EQ("{ \"hello\" }", Print(set1)); |
| 705 } |
| 706 |
| 707 TEST(PrintStlContainerTest, HashMultiSet) { |
| 708 const int kSize = 5; |
| 709 int a[kSize] = { 1, 1, 2, 5, 1 }; |
| 710 hash_multiset<int> set1(a, a + kSize); |
| 711 |
| 712 // Elements of hash_multiset can be printed in any order. |
| 713 const string result = Print(set1); |
| 714 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. |
| 715 |
| 716 // Verifies the result matches the expected pattern; also extracts |
| 717 // the numbers in the result. |
| 718 ASSERT_EQ(expected_pattern.length(), result.length()); |
| 719 std::vector<int> numbers; |
| 720 for (size_t i = 0; i != result.length(); i++) { |
| 721 if (expected_pattern[i] == 'd') { |
| 722 ASSERT_TRUE(isdigit(result[i])); |
| 723 numbers.push_back(result[i] - '0'); |
| 724 } else { |
| 725 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " |
| 726 << result; |
| 727 } |
| 728 } |
| 729 |
| 730 // Makes sure the result contains the right numbers. |
| 731 std::sort(numbers.begin(), numbers.end()); |
| 732 std::sort(a, a + kSize); |
| 733 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); |
| 734 } |
| 735 |
| 736 #endif // GMOCK_HAS_HASH_SET_ |
| 737 |
| 738 TEST(PrintStlContainerTest, List) { |
| 739 const char* a[] = { |
| 740 "hello", |
| 741 "world" |
| 742 }; |
| 743 const list<string> strings(a, a + 2); |
| 744 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); |
| 745 } |
| 746 |
| 747 TEST(PrintStlContainerTest, Map) { |
| 748 map<int, bool> map1; |
| 749 map1[1] = true; |
| 750 map1[5] = false; |
| 751 map1[3] = true; |
| 752 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); |
| 753 } |
| 754 |
| 755 TEST(PrintStlContainerTest, MultiMap) { |
| 756 multimap<bool, int> map1; |
| 757 map1.insert(make_pair(true, 0)); |
| 758 map1.insert(make_pair(true, 1)); |
| 759 map1.insert(make_pair(false, 2)); |
| 760 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); |
| 761 } |
| 762 |
| 763 TEST(PrintStlContainerTest, Set) { |
| 764 const unsigned int a[] = { 3, 0, 5 }; |
| 765 set<unsigned int> set1(a, a + 3); |
| 766 EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); |
| 767 } |
| 768 |
| 769 TEST(PrintStlContainerTest, MultiSet) { |
| 770 const int a[] = { 1, 1, 2, 5, 1 }; |
| 771 multiset<int> set1(a, a + 5); |
| 772 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); |
| 773 } |
| 774 |
| 775 TEST(PrintStlContainerTest, Pair) { |
| 776 pair<const bool, int> p(true, 5); |
| 777 EXPECT_EQ("(true, 5)", Print(p)); |
| 778 } |
| 779 |
| 780 TEST(PrintStlContainerTest, Vector) { |
| 781 vector<int> v; |
| 782 v.push_back(1); |
| 783 v.push_back(2); |
| 784 EXPECT_EQ("{ 1, 2 }", Print(v)); |
| 785 } |
| 786 |
| 787 TEST(PrintStlContainerTest, LongSequence) { |
| 788 const int a[100] = { 1, 2, 3 }; |
| 789 const vector<int> v(a, a + 100); |
| 790 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " |
| 791 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); |
| 792 } |
| 793 |
| 794 TEST(PrintStlContainerTest, NestedContainer) { |
| 795 const int a1[] = { 1, 2 }; |
| 796 const int a2[] = { 3, 4, 5 }; |
| 797 const list<int> l1(a1, a1 + 2); |
| 798 const list<int> l2(a2, a2 + 3); |
| 799 |
| 800 vector<list<int> > v; |
| 801 v.push_back(l1); |
| 802 v.push_back(l2); |
| 803 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); |
| 804 } |
| 805 |
| 806 |
| 807 // Tests printing tuples. |
| 808 |
| 809 // Tuples of various arities. |
| 810 TEST(PrintTupleTest, VariousSizes) { |
| 811 tuple<> t0; |
| 812 EXPECT_EQ("()", Print(t0)); |
| 813 |
| 814 tuple<int> t1(5); |
| 815 EXPECT_EQ("(5)", Print(t1)); |
| 816 |
| 817 tuple<char, bool> t2('a', true); |
| 818 EXPECT_EQ("('a' (97), true)", Print(t2)); |
| 819 |
| 820 tuple<bool, int, int> t3(false, 2, 3); |
| 821 EXPECT_EQ("(false, 2, 3)", Print(t3)); |
| 822 |
| 823 tuple<bool, int, int, int> t4(false, 2, 3, 4); |
| 824 EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); |
| 825 |
| 826 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true); |
| 827 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); |
| 828 |
| 829 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6); |
| 830 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); |
| 831 |
| 832 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7); |
| 833 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); |
| 834 |
| 835 tuple<bool, int, int, int, bool, int, int, bool> t8( |
| 836 false, 2, 3, 4, true, 6, 7, true); |
| 837 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); |
| 838 |
| 839 tuple<bool, int, int, int, bool, int, int, bool, int> t9( |
| 840 false, 2, 3, 4, true, 6, 7, true, 9); |
| 841 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); |
| 842 |
| 843 const char* const str = "8"; |
| 844 tuple<bool, char, short, testing::internal::Int32, // NOLINT |
| 845 testing::internal::Int64, float, double, const char*, void*, string> |
| 846 t10(false, 'a', 3, 4, 5, 6.5F, 7.5, str, NULL, "10"); |
| 847 EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 6.5, 7.5, " + PrintPointer(str) + |
| 848 " pointing to \"8\", NULL, \"10\")", |
| 849 Print(t10)); |
| 850 } |
| 851 |
| 852 // Nested tuples. |
| 853 TEST(PrintTupleTest, NestedTuple) { |
| 854 tuple<tuple<int, double>, char> nested(make_tuple(5, 9.5), 'a'); |
| 855 EXPECT_EQ("((5, 9.5), 'a' (97))", Print(nested)); |
| 856 } |
| 857 |
| 858 // Tests printing user-defined unprintable types. |
| 859 |
| 860 // Unprintable types in the global namespace. |
| 861 TEST(PrintUnprintableTypeTest, InGlobalNamespace) { |
| 862 EXPECT_EQ("1-byte object <00>", |
| 863 Print(UnprintableTemplateInGlobal<bool>())); |
| 864 } |
| 865 |
| 866 // Unprintable types in a user namespace. |
| 867 TEST(PrintUnprintableTypeTest, InUserNamespace) { |
| 868 EXPECT_EQ("16-byte object <EF12 0000 34AB 0000 0000 0000 0000 0000>", |
| 869 Print(::foo::UnprintableInFoo())); |
| 870 } |
| 871 |
| 872 // Unprintable types are that too big to be printed completely. |
| 873 |
| 874 struct Big { |
| 875 Big() { memset(array, 0, sizeof(array)); } |
| 876 char array[257]; |
| 877 }; |
| 878 |
| 879 TEST(PrintUnpritableTypeTest, BigObject) { |
| 880 EXPECT_EQ("257-byte object <0000 0000 0000 0000 0000 0000 " |
| 881 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " |
| 882 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " |
| 883 "0000 0000 0000 0000 0000 0000 ... 0000 0000 0000 " |
| 884 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " |
| 885 "0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 " |
| 886 "0000 0000 0000 0000 0000 0000 0000 0000 00>", |
| 887 Print(Big())); |
| 888 } |
| 889 |
| 890 // Tests printing user-defined streamable types. |
| 891 |
| 892 // Streamable types in the global namespace. |
| 893 TEST(PrintStreamableTypeTest, InGlobalNamespace) { |
| 894 EXPECT_EQ("StreamableInGlobal", |
| 895 Print(StreamableInGlobal())); |
| 896 } |
| 897 |
| 898 // Printable template types in a user namespace. |
| 899 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { |
| 900 EXPECT_EQ("StreamableTemplateInFoo: 0", |
| 901 Print(::foo::StreamableTemplateInFoo<int>())); |
| 902 } |
| 903 |
| 904 // Tests printing user-defined types that have a PrintTo() function. |
| 905 TEST(PrintPrintableTypeTest, InUserNamespace) { |
| 906 EXPECT_EQ("PrintableViaPrintTo: 0", |
| 907 Print(::foo::PrintableViaPrintTo())); |
| 908 } |
| 909 |
| 910 // Tests printing user-defined class template that have a PrintTo() function. |
| 911 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { |
| 912 EXPECT_EQ("PrintableViaPrintToTemplate: 5", |
| 913 Print(::foo::PrintableViaPrintToTemplate<int>(5))); |
| 914 } |
| 915 |
| 916 #if GMOCK_HAS_PROTOBUF_ |
| 917 |
| 918 // Tests printing a protocol message. |
| 919 TEST(PrintProtocolMessageTest, PrintsShortDebugString) { |
| 920 testing::internal::TestMessage msg; |
| 921 msg.set_member("yes"); |
| 922 EXPECT_EQ("<member:\"yes\">", Print(msg)); |
| 923 } |
| 924 |
| 925 // Tests printing a proto2 message. |
| 926 TEST(PrintProto2MessageTest, PrintsShortDebugString) { |
| 927 testing::internal::FooMessage msg; |
| 928 msg.set_int_field(2); |
| 929 EXPECT_PRED2(RE::FullMatch, Print(msg), |
| 930 "<int_field:\\s*2\\s*>"); |
| 931 } |
| 932 |
| 933 #endif // GMOCK_HAS_PROTOBUF_ |
| 934 |
| 935 // Tests that the universal printer prints both the address and the |
| 936 // value of a reference. |
| 937 TEST(PrintReferenceTest, PrintsAddressAndValue) { |
| 938 int n = 5; |
| 939 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); |
| 940 |
| 941 int a[2][3] = { |
| 942 { 0, 1, 2 }, |
| 943 { 3, 4, 5 } |
| 944 }; |
| 945 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", |
| 946 PrintByRef(a)); |
| 947 |
| 948 const ::foo::UnprintableInFoo x; |
| 949 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " |
| 950 "<EF12 0000 34AB 0000 0000 0000 0000 0000>", |
| 951 PrintByRef(x)); |
| 952 } |
| 953 |
| 954 // Tests that the universal printer prints a function pointer passed by |
| 955 // reference. |
| 956 TEST(PrintReferenceTest, HandlesFunctionPointer) { |
| 957 void (*fp)(int n) = &MyFunction; |
| 958 const string fp_pointer_string = |
| 959 PrintPointer(reinterpret_cast<const void*>(&fp)); |
| 960 const string fp_string = PrintPointer(reinterpret_cast<const void*>(fp)); |
| 961 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, |
| 962 PrintByRef(fp)); |
| 963 } |
| 964 |
| 965 // Tests that the universal printer prints a member function pointer |
| 966 // passed by reference. |
| 967 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { |
| 968 int (Foo::*p)(char ch) = &Foo::MyMethod; |
| 969 EXPECT_THAT(PrintByRef(p), |
| 970 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p)) |
| 971 + " " + Print(sizeof(p)) + "-byte object ")); |
| 972 |
| 973 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; |
| 974 EXPECT_THAT(PrintByRef(p2), |
| 975 StartsWith("@" + PrintPointer(reinterpret_cast<const void*>(&p2)) |
| 976 + " " + Print(sizeof(p2)) + "-byte object ")); |
| 977 } |
| 978 |
| 979 // Tests that the universal printer prints a member variable pointer |
| 980 // passed by reference. |
| 981 TEST(PrintReferenceTest, HandlesMemberVariablePointer) { |
| 982 int (Foo::*p) = &Foo::value; // NOLINT |
| 983 EXPECT_THAT(PrintByRef(p), |
| 984 StartsWith("@" + PrintPointer(&p) |
| 985 + " " + Print(sizeof(p)) + "-byte object ")); |
| 986 } |
| 987 |
| 988 TEST(PrintToStringTest, WorksForNonReference) { |
| 989 EXPECT_EQ("123", UniversalPrinter<int>::PrintToString(123)); |
| 990 } |
| 991 |
| 992 TEST(PrintToStringTest, WorksForReference) { |
| 993 int n = 123; |
| 994 EXPECT_EQ("@" + PrintPointer(&n) + " 123", |
| 995 UniversalPrinter<const int&>::PrintToString(n)); |
| 996 } |
| 997 |
| 998 TEST(UniversalTersePrintTest, WorksForNonReference) { |
| 999 ::std::stringstream ss; |
| 1000 UniversalTersePrint(123, &ss); |
| 1001 EXPECT_EQ("123", ss.str()); |
| 1002 } |
| 1003 |
| 1004 TEST(UniversalTersePrintTest, WorksForReference) { |
| 1005 const int& n = 123; |
| 1006 ::std::stringstream ss; |
| 1007 UniversalTersePrint(n, &ss); |
| 1008 EXPECT_EQ("123", ss.str()); |
| 1009 } |
| 1010 |
| 1011 TEST(UniversalTersePrintTest, WorksForCString) { |
| 1012 const char* s1 = "abc"; |
| 1013 ::std::stringstream ss1; |
| 1014 UniversalTersePrint(s1, &ss1); |
| 1015 EXPECT_EQ("\"abc\"", ss1.str()); |
| 1016 |
| 1017 char* s2 = const_cast<char*>(s1); |
| 1018 ::std::stringstream ss2; |
| 1019 UniversalTersePrint(s2, &ss2); |
| 1020 EXPECT_EQ("\"abc\"", ss2.str()); |
| 1021 |
| 1022 const char* s3 = NULL; |
| 1023 ::std::stringstream ss3; |
| 1024 UniversalTersePrint(s3, &ss3); |
| 1025 EXPECT_EQ("NULL", ss3.str()); |
| 1026 } |
| 1027 |
| 1028 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { |
| 1029 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple()), |
| 1030 ElementsAre()); |
| 1031 } |
| 1032 |
| 1033 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { |
| 1034 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1)), |
| 1035 ElementsAre("1")); |
| 1036 } |
| 1037 |
| 1038 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { |
| 1039 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')), |
| 1040 ElementsAre("1", "'a' (97)")); |
| 1041 } |
| 1042 |
| 1043 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { |
| 1044 const int n = 1; |
| 1045 EXPECT_THAT(UniversalTersePrintTupleFieldsToStrings( |
| 1046 tuple<const int&, const char*>(n, "a")), |
| 1047 ElementsAre("1", "\"a\"")); |
| 1048 } |
| 1049 |
| 1050 } // namespace gmock_printers_test |
| 1051 } // namespace testing |
OLD | NEW |