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