gtest-message_test.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Tests for the Message class.
  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 = (Message() << 1260570880.4555497 << " "
  64. << 1260572265.1954534).GetString();
  65. // Both numbers should be printed with enough precision.
  66. EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
  67. EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
  68. }
  69. // Tests streaming a non-char pointer.
  70. TEST(MessageTest, StreamsPointer) {
  71. int n = 0;
  72. int* p = &n;
  73. EXPECT_NE("(null)", (Message() << p).GetString());
  74. }
  75. // Tests streaming a NULL non-char pointer.
  76. TEST(MessageTest, StreamsNullPointer) {
  77. int* p = NULL;
  78. EXPECT_EQ("(null)", (Message() << p).GetString());
  79. }
  80. // Tests streaming a C string.
  81. TEST(MessageTest, StreamsCString) {
  82. EXPECT_EQ("Foo", (Message() << "Foo").GetString());
  83. }
  84. // Tests streaming a NULL C string.
  85. TEST(MessageTest, StreamsNullCString) {
  86. char* p = NULL;
  87. EXPECT_EQ("(null)", (Message() << p).GetString());
  88. }
  89. // Tests streaming std::string.
  90. TEST(MessageTest, StreamsString) {
  91. const ::std::string str("Hello");
  92. EXPECT_EQ("Hello", (Message() << str).GetString());
  93. }
  94. // Tests that we can output strings containing embedded NULs.
  95. TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
  96. const char char_array_with_nul[] =
  97. "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("Line 1.\nA NUL char \\0 in line 2.",
  115. (Message() << "Line 1." << std::endl
  116. << "A NUL char " << std::ends << std::flush
  117. << " in line 2.").GetString());
  118. }
  119. // Tests Message::GetString()
  120. TEST(MessageTest, GetString) {
  121. Message msg;
  122. msg << 1 << " lamb";
  123. EXPECT_EQ("1 lamb", msg.GetString());
  124. }
  125. // Tests streaming a Message object to an ostream.
  126. TEST(MessageTest, StreamsToOStream) {
  127. Message msg("Hello");
  128. ::std::stringstream ss;
  129. ss << msg;
  130. EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss));
  131. }
  132. // Tests that a Message object doesn't take up too much stack space.
  133. TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
  134. EXPECT_LE(sizeof(Message), 16U);
  135. }
  136. } // namespace