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