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