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; 41using ::testing::internal::StrStream; 42 43// A helper function that turns a Message into a C string. 44const char* ToCString(const Message& msg) { 45 static testing::internal::String result; 46 result = msg.GetString(); 47 return result.c_str(); 48} 49 50// Tests the testing::Message class 51 52// Tests the default constructor. 53TEST(MessageTest, DefaultConstructor) { 54 const Message msg; 55 EXPECT_STREQ("", ToCString(msg)); 56} 57 58// Tests the copy constructor. 59TEST(MessageTest, CopyConstructor) { 60 const Message msg1("Hello"); 61 const Message msg2(msg1); 62 EXPECT_STREQ("Hello", ToCString(msg2)); 63} 64 65// Tests constructing a Message from a C-string. 66TEST(MessageTest, ConstructsFromCString) { 67 Message msg("Hello"); 68 EXPECT_STREQ("Hello", ToCString(msg)); 69} 70 71// Tests streaming a float. 72TEST(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); 77} 78 79// Tests streaming a double. 80TEST(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); 86} 87 88// Tests streaming a non-char pointer. 89TEST(MessageTest, StreamsPointer) { 90 int n = 0; 91 int* p = &n; 92 EXPECT_STRNE("(null)", ToCString(Message() << p)); 93} 94 95// Tests streaming a NULL non-char pointer. 96TEST(MessageTest, StreamsNullPointer) { 97 int* p = NULL; 98 EXPECT_STREQ("(null)", ToCString(Message() << p)); 99} 100 101// Tests streaming a C string. 102TEST(MessageTest, StreamsCString) { 103 EXPECT_STREQ("Foo", ToCString(Message() << "Foo")); 104} 105 106// Tests streaming a NULL C string. 107TEST(MessageTest, StreamsNullCString) { 108 char* p = NULL; 109 EXPECT_STREQ("(null)", ToCString(Message() << p)); 110} 111 112// Tests streaming std::string. 113TEST(MessageTest, StreamsString) { 114 const ::std::string str("Hello"); 115 EXPECT_STREQ("Hello", ToCString(Message() << str)); 116} 117 118// Tests that we can output strings containing embedded NULs. 119TEST(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)); 126} 127 128// Tests streaming a NUL char. 129TEST(MessageTest, StreamsNULChar) { 130 EXPECT_STREQ("\\0", ToCString(Message() << '\0')); 131} 132 133// Tests streaming int. 134TEST(MessageTest, StreamsInt) { 135 EXPECT_STREQ("123", ToCString(Message() << 123)); 136} 137 138// Tests that basic IO manipulators (endl, ends, and flush) can be 139// streamed to Message. 140TEST(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 144 << " in line 2.")); 145} 146 147// Tests Message::GetString() 148TEST(MessageTest, GetString) { 149 Message msg; 150 msg << 1 << " lamb"; 151 EXPECT_STREQ("1 lamb", msg.GetString().c_str()); 152} 153 154// Tests streaming a Message object to an ostream. 155TEST(MessageTest, StreamsToOStream) { 156 Message msg("Hello"); 157 StrStream ss; 158 ss << msg; 159 EXPECT_STREQ("Hello", testing::internal::StrStreamToString(&ss).c_str()); 160} 161 162// Tests that a Message object doesn't take up too much stack space. 163TEST(MessageTest, DoesNotTakeUpMuchStackSpace) { 164 EXPECT_LE(sizeof(Message), 16U); 165} 166 167} // namespace 168