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.

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