michael@0: // Copyright 2005, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: // michael@0: // Tests for the Message class. michael@0: michael@0: #include "gtest/gtest-message.h" michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: namespace { michael@0: michael@0: using ::testing::Message; michael@0: michael@0: // A helper function that turns a Message into a C string. michael@0: const char* ToCString(const Message& msg) { michael@0: static testing::internal::String result; michael@0: result = msg.GetString(); michael@0: return result.c_str(); michael@0: } michael@0: michael@0: // Tests the testing::Message class michael@0: michael@0: // Tests the default constructor. michael@0: TEST(MessageTest, DefaultConstructor) { michael@0: const Message msg; michael@0: EXPECT_STREQ("", ToCString(msg)); michael@0: } michael@0: michael@0: // Tests the copy constructor. michael@0: TEST(MessageTest, CopyConstructor) { michael@0: const Message msg1("Hello"); michael@0: const Message msg2(msg1); michael@0: EXPECT_STREQ("Hello", ToCString(msg2)); michael@0: } michael@0: michael@0: // Tests constructing a Message from a C-string. michael@0: TEST(MessageTest, ConstructsFromCString) { michael@0: Message msg("Hello"); michael@0: EXPECT_STREQ("Hello", ToCString(msg)); michael@0: } michael@0: michael@0: // Tests streaming a float. michael@0: TEST(MessageTest, StreamsFloat) { michael@0: const char* const s = ToCString(Message() << 1.23456F << " " << 2.34567F); michael@0: // Both numbers should be printed with enough precision. michael@0: EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s); michael@0: EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s); michael@0: } michael@0: michael@0: // Tests streaming a double. michael@0: TEST(MessageTest, StreamsDouble) { michael@0: const char* const s = ToCString(Message() << 1260570880.4555497 << " " michael@0: << 1260572265.1954534); michael@0: // Both numbers should be printed with enough precision. michael@0: EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s); michael@0: EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s); michael@0: } michael@0: michael@0: // Tests streaming a non-char pointer. michael@0: TEST(MessageTest, StreamsPointer) { michael@0: int n = 0; michael@0: int* p = &n; michael@0: EXPECT_STRNE("(null)", ToCString(Message() << p)); michael@0: } michael@0: michael@0: // Tests streaming a NULL non-char pointer. michael@0: TEST(MessageTest, StreamsNullPointer) { michael@0: int* p = NULL; michael@0: EXPECT_STREQ("(null)", ToCString(Message() << p)); michael@0: } michael@0: michael@0: // Tests streaming a C string. michael@0: TEST(MessageTest, StreamsCString) { michael@0: EXPECT_STREQ("Foo", ToCString(Message() << "Foo")); michael@0: } michael@0: michael@0: // Tests streaming a NULL C string. michael@0: TEST(MessageTest, StreamsNullCString) { michael@0: char* p = NULL; michael@0: EXPECT_STREQ("(null)", ToCString(Message() << p)); michael@0: } michael@0: michael@0: // Tests streaming std::string. michael@0: TEST(MessageTest, StreamsString) { michael@0: const ::std::string str("Hello"); michael@0: EXPECT_STREQ("Hello", ToCString(Message() << str)); michael@0: } michael@0: michael@0: // Tests that we can output strings containing embedded NULs. michael@0: TEST(MessageTest, StreamsStringWithEmbeddedNUL) { michael@0: const char char_array_with_nul[] = michael@0: "Here's a NUL\0 and some more string"; michael@0: const ::std::string string_with_nul(char_array_with_nul, michael@0: sizeof(char_array_with_nul) - 1); michael@0: EXPECT_STREQ("Here's a NUL\\0 and some more string", michael@0: ToCString(Message() << string_with_nul)); michael@0: } michael@0: michael@0: // Tests streaming a NUL char. michael@0: TEST(MessageTest, StreamsNULChar) { michael@0: EXPECT_STREQ("\\0", ToCString(Message() << '\0')); michael@0: } michael@0: michael@0: // Tests streaming int. michael@0: TEST(MessageTest, StreamsInt) { michael@0: EXPECT_STREQ("123", ToCString(Message() << 123)); michael@0: } michael@0: michael@0: // Tests that basic IO manipulators (endl, ends, and flush) can be michael@0: // streamed to Message. michael@0: TEST(MessageTest, StreamsBasicIoManip) { michael@0: EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.", michael@0: ToCString(Message() << "Line 1." << std::endl michael@0: << "A NUL char " << std::ends << std::flush michael@0: << " in line 2.")); michael@0: } michael@0: michael@0: // Tests Message::GetString() michael@0: TEST(MessageTest, GetString) { michael@0: Message msg; michael@0: msg << 1 << " lamb"; michael@0: EXPECT_STREQ("1 lamb", msg.GetString().c_str()); michael@0: } michael@0: michael@0: // Tests streaming a Message object to an ostream. michael@0: TEST(MessageTest, StreamsToOStream) { michael@0: Message msg("Hello"); michael@0: ::std::stringstream ss; michael@0: ss << msg; michael@0: EXPECT_STREQ("Hello", testing::internal::StringStreamToString(&ss).c_str()); michael@0: } michael@0: michael@0: // Tests that a Message object doesn't take up too much stack space. michael@0: TEST(MessageTest, DoesNotTakeUpMuchStackSpace) { michael@0: EXPECT_LE(sizeof(Message), 16U); michael@0: } michael@0: michael@0: } // namespace