1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper
30b89a7cc2SEnji Cooper //
31b89a7cc2SEnji Cooper // Tests for the Message class.
32b89a7cc2SEnji Cooper
33*28f6c2f2SEnji Cooper #include <sstream>
34*28f6c2f2SEnji Cooper #include <string>
35b89a7cc2SEnji Cooper
36*28f6c2f2SEnji Cooper #include "gtest/gtest-message.h"
37b89a7cc2SEnji Cooper #include "gtest/gtest.h"
38b89a7cc2SEnji Cooper
39*28f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
40*28f6c2f2SEnji Cooper #include "absl/strings/str_format.h"
41*28f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL
42*28f6c2f2SEnji Cooper
43b89a7cc2SEnji Cooper namespace {
44b89a7cc2SEnji Cooper
45b89a7cc2SEnji Cooper using ::testing::Message;
46b89a7cc2SEnji Cooper
47*28f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
48*28f6c2f2SEnji Cooper struct AbslStringifiablePoint {
49*28f6c2f2SEnji Cooper template <typename Sink>
AbslStringify(Sink & sink,const AbslStringifiablePoint & p)50*28f6c2f2SEnji Cooper friend void AbslStringify(Sink& sink, const AbslStringifiablePoint& p) {
51*28f6c2f2SEnji Cooper absl::Format(&sink, "(%d, %d)", p.x, p.y);
52*28f6c2f2SEnji Cooper }
53*28f6c2f2SEnji Cooper
54*28f6c2f2SEnji Cooper int x;
55*28f6c2f2SEnji Cooper int y;
56*28f6c2f2SEnji Cooper };
57*28f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL
58*28f6c2f2SEnji Cooper
59b89a7cc2SEnji Cooper // Tests the testing::Message class
60b89a7cc2SEnji Cooper
61b89a7cc2SEnji Cooper // Tests the default constructor.
TEST(MessageTest,DefaultConstructor)62b89a7cc2SEnji Cooper TEST(MessageTest, DefaultConstructor) {
63b89a7cc2SEnji Cooper const Message msg;
64b89a7cc2SEnji Cooper EXPECT_EQ("", msg.GetString());
65b89a7cc2SEnji Cooper }
66b89a7cc2SEnji Cooper
67b89a7cc2SEnji Cooper // Tests the copy constructor.
TEST(MessageTest,CopyConstructor)68b89a7cc2SEnji Cooper TEST(MessageTest, CopyConstructor) {
69b89a7cc2SEnji Cooper const Message msg1("Hello");
70b89a7cc2SEnji Cooper const Message msg2(msg1);
71b89a7cc2SEnji Cooper EXPECT_EQ("Hello", msg2.GetString());
72b89a7cc2SEnji Cooper }
73b89a7cc2SEnji Cooper
74b89a7cc2SEnji Cooper // Tests constructing a Message from a C-string.
TEST(MessageTest,ConstructsFromCString)75b89a7cc2SEnji Cooper TEST(MessageTest, ConstructsFromCString) {
76b89a7cc2SEnji Cooper Message msg("Hello");
77b89a7cc2SEnji Cooper EXPECT_EQ("Hello", msg.GetString());
78b89a7cc2SEnji Cooper }
79b89a7cc2SEnji Cooper
80b89a7cc2SEnji Cooper // Tests streaming a float.
TEST(MessageTest,StreamsFloat)81b89a7cc2SEnji Cooper TEST(MessageTest, StreamsFloat) {
82b89a7cc2SEnji Cooper const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString();
83b89a7cc2SEnji Cooper // Both numbers should be printed with enough precision.
84b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str());
85b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str());
86b89a7cc2SEnji Cooper }
87b89a7cc2SEnji Cooper
88b89a7cc2SEnji Cooper // Tests streaming a double.
TEST(MessageTest,StreamsDouble)89b89a7cc2SEnji Cooper TEST(MessageTest, StreamsDouble) {
90*28f6c2f2SEnji Cooper const std::string s =
91*28f6c2f2SEnji Cooper (Message() << 1260570880.4555497 << " " << 1260572265.1954534)
92*28f6c2f2SEnji Cooper .GetString();
93b89a7cc2SEnji Cooper // Both numbers should be printed with enough precision.
94b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
95b89a7cc2SEnji Cooper EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
96b89a7cc2SEnji Cooper }
97b89a7cc2SEnji Cooper
98b89a7cc2SEnji Cooper // Tests streaming a non-char pointer.
TEST(MessageTest,StreamsPointer)99b89a7cc2SEnji Cooper TEST(MessageTest, StreamsPointer) {
100b89a7cc2SEnji Cooper int n = 0;
101b89a7cc2SEnji Cooper int* p = &n;
102b89a7cc2SEnji Cooper EXPECT_NE("(null)", (Message() << p).GetString());
103b89a7cc2SEnji Cooper }
104b89a7cc2SEnji Cooper
105b89a7cc2SEnji Cooper // Tests streaming a NULL non-char pointer.
TEST(MessageTest,StreamsNullPointer)106b89a7cc2SEnji Cooper TEST(MessageTest, StreamsNullPointer) {
107*28f6c2f2SEnji Cooper int* p = nullptr;
108b89a7cc2SEnji Cooper EXPECT_EQ("(null)", (Message() << p).GetString());
109b89a7cc2SEnji Cooper }
110b89a7cc2SEnji Cooper
111b89a7cc2SEnji Cooper // Tests streaming a C string.
TEST(MessageTest,StreamsCString)112b89a7cc2SEnji Cooper TEST(MessageTest, StreamsCString) {
113b89a7cc2SEnji Cooper EXPECT_EQ("Foo", (Message() << "Foo").GetString());
114b89a7cc2SEnji Cooper }
115b89a7cc2SEnji Cooper
116b89a7cc2SEnji Cooper // Tests streaming a NULL C string.
TEST(MessageTest,StreamsNullCString)117b89a7cc2SEnji Cooper TEST(MessageTest, StreamsNullCString) {
118*28f6c2f2SEnji Cooper char* p = nullptr;
119b89a7cc2SEnji Cooper EXPECT_EQ("(null)", (Message() << p).GetString());
120b89a7cc2SEnji Cooper }
121b89a7cc2SEnji Cooper
122b89a7cc2SEnji Cooper // Tests streaming std::string.
TEST(MessageTest,StreamsString)123b89a7cc2SEnji Cooper TEST(MessageTest, StreamsString) {
124b89a7cc2SEnji Cooper const ::std::string str("Hello");
125b89a7cc2SEnji Cooper EXPECT_EQ("Hello", (Message() << str).GetString());
126b89a7cc2SEnji Cooper }
127b89a7cc2SEnji Cooper
128b89a7cc2SEnji Cooper // Tests that we can output strings containing embedded NULs.
TEST(MessageTest,StreamsStringWithEmbeddedNUL)129b89a7cc2SEnji Cooper TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
130*28f6c2f2SEnji Cooper const char char_array_with_nul[] = "Here's a NUL\0 and some more string";
131b89a7cc2SEnji Cooper const ::std::string string_with_nul(char_array_with_nul,
132b89a7cc2SEnji Cooper sizeof(char_array_with_nul) - 1);
133b89a7cc2SEnji Cooper EXPECT_EQ("Here's a NUL\\0 and some more string",
134b89a7cc2SEnji Cooper (Message() << string_with_nul).GetString());
135b89a7cc2SEnji Cooper }
136b89a7cc2SEnji Cooper
137b89a7cc2SEnji Cooper // Tests streaming a NUL char.
TEST(MessageTest,StreamsNULChar)138b89a7cc2SEnji Cooper TEST(MessageTest, StreamsNULChar) {
139b89a7cc2SEnji Cooper EXPECT_EQ("\\0", (Message() << '\0').GetString());
140b89a7cc2SEnji Cooper }
141b89a7cc2SEnji Cooper
142b89a7cc2SEnji Cooper // Tests streaming int.
TEST(MessageTest,StreamsInt)143b89a7cc2SEnji Cooper TEST(MessageTest, StreamsInt) {
144b89a7cc2SEnji Cooper EXPECT_EQ("123", (Message() << 123).GetString());
145b89a7cc2SEnji Cooper }
146b89a7cc2SEnji Cooper
147*28f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
148*28f6c2f2SEnji Cooper // Tests streaming a type with an AbslStringify definition.
TEST(MessageTest,StreamsAbslStringify)149*28f6c2f2SEnji Cooper TEST(MessageTest, StreamsAbslStringify) {
150*28f6c2f2SEnji Cooper EXPECT_EQ("(1, 2)", (Message() << AbslStringifiablePoint{1, 2}).GetString());
151*28f6c2f2SEnji Cooper }
152*28f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL
153*28f6c2f2SEnji Cooper
154b89a7cc2SEnji Cooper // Tests that basic IO manipulators (endl, ends, and flush) can be
155b89a7cc2SEnji Cooper // streamed to Message.
TEST(MessageTest,StreamsBasicIoManip)156b89a7cc2SEnji Cooper TEST(MessageTest, StreamsBasicIoManip) {
157*28f6c2f2SEnji Cooper EXPECT_EQ(
158*28f6c2f2SEnji Cooper "Line 1.\nA NUL char \\0 in line 2.",
159b89a7cc2SEnji Cooper (Message() << "Line 1." << std::endl
160*28f6c2f2SEnji Cooper << "A NUL char " << std::ends << std::flush << " in line 2.")
161*28f6c2f2SEnji Cooper .GetString());
162b89a7cc2SEnji Cooper }
163b89a7cc2SEnji Cooper
164b89a7cc2SEnji Cooper // Tests Message::GetString()
TEST(MessageTest,GetString)165b89a7cc2SEnji Cooper TEST(MessageTest, GetString) {
166b89a7cc2SEnji Cooper Message msg;
167b89a7cc2SEnji Cooper msg << 1 << " lamb";
168b89a7cc2SEnji Cooper EXPECT_EQ("1 lamb", msg.GetString());
169b89a7cc2SEnji Cooper }
170b89a7cc2SEnji Cooper
171b89a7cc2SEnji Cooper // Tests streaming a Message object to an ostream.
TEST(MessageTest,StreamsToOStream)172b89a7cc2SEnji Cooper TEST(MessageTest, StreamsToOStream) {
173b89a7cc2SEnji Cooper Message msg("Hello");
174b89a7cc2SEnji Cooper ::std::stringstream ss;
175b89a7cc2SEnji Cooper ss << msg;
176b89a7cc2SEnji Cooper EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss));
177b89a7cc2SEnji Cooper }
178b89a7cc2SEnji Cooper
179b89a7cc2SEnji Cooper // Tests that a Message object doesn't take up too much stack space.
TEST(MessageTest,DoesNotTakeUpMuchStackSpace)180b89a7cc2SEnji Cooper TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
181b89a7cc2SEnji Cooper EXPECT_LE(sizeof(Message), 16U);
182b89a7cc2SEnji Cooper }
183b89a7cc2SEnji Cooper
184b89a7cc2SEnji Cooper } // namespace
185