googletest-message-test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2005, 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. // Tests for the Message class.
  31. #include <sstream>
  32. #include <string>
  33. #include "gtest/gtest-message.h"
  34. #include "gtest/gtest.h"
  35. #ifdef GTEST_HAS_ABSL
  36. #include "absl/strings/str_format.h"
  37. #endif // GTEST_HAS_ABSL
  38. namespace {
  39. using ::testing::Message;
  40. #ifdef GTEST_HAS_ABSL
  41. struct AbslStringifiablePoint {
  42. template <typename Sink>
  43. friend void AbslStringify(Sink& sink, const AbslStringifiablePoint& p) {
  44. absl::Format(&sink, "(%d, %d)", p.x, p.y);
  45. }
  46. int x;
  47. int y;
  48. };
  49. #endif // GTEST_HAS_ABSL
  50. // Tests the testing::Message class
  51. // Tests the default constructor.
  52. TEST(MessageTest, DefaultConstructor) {
  53. const Message msg;
  54. EXPECT_EQ("", msg.GetString());
  55. }
  56. // Tests the copy constructor.
  57. TEST(MessageTest, CopyConstructor) {
  58. const Message msg1("Hello");
  59. const Message msg2(msg1);
  60. EXPECT_EQ("Hello", msg2.GetString());
  61. }
  62. // Tests constructing a Message from a C-string.
  63. TEST(MessageTest, ConstructsFromCString) {
  64. Message msg("Hello");
  65. EXPECT_EQ("Hello", msg.GetString());
  66. }
  67. // Tests streaming a float.
  68. TEST(MessageTest, StreamsFloat) {
  69. const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString();
  70. // Both numbers should be printed with enough precision.
  71. EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str());
  72. EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str());
  73. }
  74. // Tests streaming a double.
  75. TEST(MessageTest, StreamsDouble) {
  76. const std::string s =
  77. (Message() << 1260570880.4555497 << " " << 1260572265.1954534)
  78. .GetString();
  79. // Both numbers should be printed with enough precision.
  80. EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
  81. EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
  82. }
  83. // Tests streaming a non-char pointer.
  84. TEST(MessageTest, StreamsPointer) {
  85. int n = 0;
  86. int* p = &n;
  87. EXPECT_NE("(null)", (Message() << p).GetString());
  88. }
  89. // Tests streaming a NULL non-char pointer.
  90. TEST(MessageTest, StreamsNullPointer) {
  91. int* p = nullptr;
  92. EXPECT_EQ("(null)", (Message() << p).GetString());
  93. }
  94. // Tests streaming a C string.
  95. TEST(MessageTest, StreamsCString) {
  96. EXPECT_EQ("Foo", (Message() << "Foo").GetString());
  97. }
  98. // Tests streaming a NULL C string.
  99. TEST(MessageTest, StreamsNullCString) {
  100. char* p = nullptr;
  101. EXPECT_EQ("(null)", (Message() << p).GetString());
  102. }
  103. // Tests streaming std::string.
  104. TEST(MessageTest, StreamsString) {
  105. const ::std::string str("Hello");
  106. EXPECT_EQ("Hello", (Message() << str).GetString());
  107. }
  108. // Tests that we can output strings containing embedded NULs.
  109. TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
  110. const char char_array_with_nul[] = "Here's a NUL\0 and some more string";
  111. const ::std::string string_with_nul(char_array_with_nul,
  112. sizeof(char_array_with_nul) - 1);
  113. EXPECT_EQ("Here's a NUL\\0 and some more string",
  114. (Message() << string_with_nul).GetString());
  115. }
  116. // Tests streaming a NUL char.
  117. TEST(MessageTest, StreamsNULChar) {
  118. EXPECT_EQ("\\0", (Message() << '\0').GetString());
  119. }
  120. // Tests streaming int.
  121. TEST(MessageTest, StreamsInt) {
  122. EXPECT_EQ("123", (Message() << 123).GetString());
  123. }
  124. #ifdef GTEST_HAS_ABSL
  125. // Tests streaming a type with an AbslStringify definition.
  126. TEST(MessageTest, StreamsAbslStringify) {
  127. EXPECT_EQ("(1, 2)", (Message() << AbslStringifiablePoint{1, 2}).GetString());
  128. }
  129. #endif // GTEST_HAS_ABSL
  130. // Tests that basic IO manipulators (endl, ends, and flush) can be
  131. // streamed to Message.
  132. TEST(MessageTest, StreamsBasicIoManip) {
  133. EXPECT_EQ(
  134. "Line 1.\nA NUL char \\0 in line 2.",
  135. (Message() << "Line 1." << std::endl
  136. << "A NUL char " << std::ends << std::flush << " in line 2.")
  137. .GetString());
  138. }
  139. // Tests Message::GetString()
  140. TEST(MessageTest, GetString) {
  141. Message msg;
  142. msg << 1 << " lamb";
  143. EXPECT_EQ("1 lamb", msg.GetString());
  144. }
  145. // Tests streaming a Message object to an ostream.
  146. TEST(MessageTest, StreamsToOStream) {
  147. Message msg("Hello");
  148. ::std::stringstream ss;
  149. ss << msg;
  150. EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss));
  151. }
  152. // Tests that a Message object doesn't take up too much stack space.
  153. TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
  154. EXPECT_LE(sizeof(Message), 16U);
  155. }
  156. } // namespace