1
// Copyright 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Author: wan@google.com (Zhanyong Wan)
32
// Tests for the Message class.
34
#include <gtest/gtest-message.h>
36
#include <gtest/gtest.h>
40
using ::testing::Message;
41
using ::testing::internal::StrStream;
43
// A helper function that turns a Message into a C string.
44
const char* ToCString(const Message& msg) {
45
static testing::internal::String result;
46
result = msg.GetString();
47
return result.c_str();
50
// Tests the testing::Message class
52
// Tests the default constructor.
53
TEST(MessageTest, DefaultConstructor) {
55
EXPECT_STREQ("", ToCString(msg));
58
// Tests the copy constructor.
59
TEST(MessageTest, CopyConstructor) {
60
const Message msg1("Hello");
61
const Message msg2(msg1);
62
EXPECT_STREQ("Hello", ToCString(msg2));
65
// Tests constructing a Message from a C-string.
66
TEST(MessageTest, ConstructsFromCString) {
68
EXPECT_STREQ("Hello", ToCString(msg));
71
// Tests streaming a float.
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);
79
// Tests streaming a double.
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);
88
// Tests streaming a non-char pointer.
89
TEST(MessageTest, StreamsPointer) {
92
EXPECT_STRNE("(null)", ToCString(Message() << p));
95
// Tests streaming a NULL non-char pointer.
96
TEST(MessageTest, StreamsNullPointer) {
98
EXPECT_STREQ("(null)", ToCString(Message() << p));
101
// Tests streaming a C string.
102
TEST(MessageTest, StreamsCString) {
103
EXPECT_STREQ("Foo", ToCString(Message() << "Foo"));
106
// Tests streaming a NULL C string.
107
TEST(MessageTest, StreamsNullCString) {
109
EXPECT_STREQ("(null)", ToCString(Message() << p));
112
// Tests streaming std::string.
113
TEST(MessageTest, StreamsString) {
114
const ::std::string str("Hello");
115
EXPECT_STREQ("Hello", ToCString(Message() << str));
118
// Tests that we can output strings containing embedded NULs.
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));
128
// Tests streaming a NUL char.
129
TEST(MessageTest, StreamsNULChar) {
130
EXPECT_STREQ("\\0", ToCString(Message() << '\0'));
133
// Tests streaming int.
134
TEST(MessageTest, StreamsInt) {
135
EXPECT_STREQ("123", ToCString(Message() << 123));
138
// Tests that basic IO manipulators (endl, ends, and flush) can be
139
// streamed to Message.
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
147
// Tests Message::GetString()
148
TEST(MessageTest, GetString) {
151
EXPECT_STREQ("1 lamb", msg.GetString().c_str());
154
// Tests streaming a Message object to an ostream.
155
TEST(MessageTest, StreamsToOStream) {
156
Message msg("Hello");
159
EXPECT_STREQ("Hello", testing::internal::StrStreamToString(&ss).c_str());
162
// Tests that a Message object doesn't take up too much stack space.
163
TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
164
EXPECT_LE(sizeof(Message), 16U);