1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Tests for the Message class.
33 #include "gtest/gtest-message.h"
35 #include "gtest/gtest.h"
39 using ::testing::Message;
41 // Tests the testing::Message class
43 // Tests the default constructor.
44 TEST(MessageTest, DefaultConstructor) {
46 EXPECT_EQ("", msg.GetString());
49 // Tests the copy constructor.
50 TEST(MessageTest, CopyConstructor) {
51 const Message msg1("Hello");
52 const Message msg2(msg1);
53 EXPECT_EQ("Hello", msg2.GetString());
56 // Tests constructing a Message from a C-string.
57 TEST(MessageTest, ConstructsFromCString) {
59 EXPECT_EQ("Hello", msg.GetString());
62 // Tests streaming a float.
63 TEST(MessageTest, StreamsFloat) {
64 const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString();
65 // Both numbers should be printed with enough precision.
66 EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str());
67 EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str());
70 // Tests streaming a double.
71 TEST(MessageTest, StreamsDouble) {
72 const std::string s = (Message() << 1260570880.4555497 << " "
73 << 1260572265.1954534).GetString();
74 // Both numbers should be printed with enough precision.
75 EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
76 EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
79 // Tests streaming a non-char pointer.
80 TEST(MessageTest, StreamsPointer) {
83 EXPECT_NE("(null)", (Message() << p).GetString());
86 // Tests streaming a NULL non-char pointer.
87 TEST(MessageTest, StreamsNullPointer) {
89 EXPECT_EQ("(null)", (Message() << p).GetString());
92 // Tests streaming a C string.
93 TEST(MessageTest, StreamsCString) {
94 EXPECT_EQ("Foo", (Message() << "Foo").GetString());
97 // Tests streaming a NULL C string.
98 TEST(MessageTest, StreamsNullCString) {
100 EXPECT_EQ("(null)", (Message() << p).GetString());
103 // Tests streaming std::string.
104 TEST(MessageTest, StreamsString) {
105 const ::std::string str("Hello");
106 EXPECT_EQ("Hello", (Message() << str).GetString());
109 // Tests that we can output strings containing embedded NULs.
110 TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
111 const char char_array_with_nul[] =
112 "Here's a NUL\0 and some more string";
113 const ::std::string string_with_nul(char_array_with_nul,
114 sizeof(char_array_with_nul) - 1);
115 EXPECT_EQ("Here's a NUL\\0 and some more string",
116 (Message() << string_with_nul).GetString());
119 // Tests streaming a NUL char.
120 TEST(MessageTest, StreamsNULChar) {
121 EXPECT_EQ("\\0", (Message() << '\0').GetString());
124 // Tests streaming int.
125 TEST(MessageTest, StreamsInt) {
126 EXPECT_EQ("123", (Message() << 123).GetString());
129 // Tests that basic IO manipulators (endl, ends, and flush) can be
130 // streamed to Message.
131 TEST(MessageTest, StreamsBasicIoManip) {
132 EXPECT_EQ("Line 1.\nA NUL char \\0 in line 2.",
133 (Message() << "Line 1." << std::endl
134 << "A NUL char " << std::ends << std::flush
135 << " in line 2.").GetString());
138 // Tests Message::GetString()
139 TEST(MessageTest, GetString) {
142 EXPECT_EQ("1 lamb", msg.GetString());
145 // Tests streaming a Message object to an ostream.
146 TEST(MessageTest, StreamsToOStream) {
147 Message msg("Hello");
148 ::std::stringstream ss;
150 EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss));
153 // Tests that a Message object doesn't take up too much stack space.
154 TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
155 EXPECT_LE(sizeof(Message), 16U);