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