2
0

googletest-message-test.cc 5.1 KB

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