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