Home | History | Annotate | Download | only in test
      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 (at) 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 
     38 namespace {
     39 
     40 using ::testing::Message;
     41 using ::testing::internal::StrStream;
     42 
     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();
     48 }
     49 
     50 // Tests the testing::Message class
     51 
     52 // Tests the default constructor.
     53 TEST(MessageTest, DefaultConstructor) {
     54   const Message msg;
     55   EXPECT_STREQ("", ToCString(msg));
     56 }
     57 
     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));
     63 }
     64 
     65 // Tests constructing a Message from a C-string.
     66 TEST(MessageTest, ConstructsFromCString) {
     67   Message msg("Hello");
     68   EXPECT_STREQ("Hello", ToCString(msg));
     69 }
     70 
     71 // Tests streaming a non-char pointer.
     72 TEST(MessageTest, StreamsPointer) {
     73   int n = 0;
     74   int* p = &n;
     75   EXPECT_STRNE("(null)", ToCString(Message() << p));
     76 }
     77 
     78 // Tests streaming a NULL non-char pointer.
     79 TEST(MessageTest, StreamsNullPointer) {
     80   int* p = NULL;
     81   EXPECT_STREQ("(null)", ToCString(Message() << p));
     82 }
     83 
     84 // Tests streaming a C string.
     85 TEST(MessageTest, StreamsCString) {
     86   EXPECT_STREQ("Foo", ToCString(Message() << "Foo"));
     87 }
     88 
     89 // Tests streaming a NULL C string.
     90 TEST(MessageTest, StreamsNullCString) {
     91   char* p = NULL;
     92   EXPECT_STREQ("(null)", ToCString(Message() << p));
     93 }
     94 
     95 #if GTEST_HAS_STD_STRING
     96 
     97 // Tests streaming std::string.
     98 //
     99 // As std::string has problem in MSVC when exception is disabled, we only
    100 // test this where std::string can be used.
    101 TEST(MessageTest, StreamsString) {
    102   const ::std::string str("Hello");
    103   EXPECT_STREQ("Hello", ToCString(Message() << str));
    104 }
    105 
    106 // Tests that we can output strings containing embedded NULs.
    107 TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
    108   const char char_array_with_nul[] =
    109       "Here's a NUL\0 and some more string";
    110   const ::std::string string_with_nul(char_array_with_nul,
    111                                       sizeof(char_array_with_nul) - 1);
    112   EXPECT_STREQ("Here's a NUL\\0 and some more string",
    113                ToCString(Message() << string_with_nul));
    114 }
    115 
    116 #endif  // GTEST_HAS_STD_STRING
    117 
    118 // Tests streaming a NUL char.
    119 TEST(MessageTest, StreamsNULChar) {
    120   EXPECT_STREQ("\\0", ToCString(Message() << '\0'));
    121 }
    122 
    123 // Tests streaming int.
    124 TEST(MessageTest, StreamsInt) {
    125   EXPECT_STREQ("123", ToCString(Message() << 123));
    126 }
    127 
    128 // Tests that basic IO manipulators (endl, ends, and flush) can be
    129 // streamed to Message.
    130 TEST(MessageTest, StreamsBasicIoManip) {
    131   EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.",
    132                ToCString(Message() << "Line 1." << std::endl
    133                          << "A NUL char " << std::ends << std::flush
    134                          << " in line 2."));
    135 }
    136 
    137 // Tests Message::GetString()
    138 TEST(MessageTest, GetString) {
    139   Message msg;
    140   msg << 1 << " lamb";
    141   EXPECT_STREQ("1 lamb", msg.GetString().c_str());
    142 }
    143 
    144 // Tests streaming a Message object to an ostream.
    145 TEST(MessageTest, StreamsToOStream) {
    146   Message msg("Hello");
    147   StrStream ss;
    148   ss << msg;
    149   EXPECT_STREQ("Hello", testing::internal::StrStreamToString(&ss).c_str());
    150 }
    151 
    152 // Tests that a Message object doesn't take up too much stack space.
    153 TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
    154   EXPECT_LE(sizeof(Message), 16U);
    155 }
    156 
    157 }  // namespace
    158