You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.1 KiB

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