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