gtest-message_test.cc revision 1be2c9def7187e4e643c00a31dd9986395795d7d
11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests for the Message class.
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <gtest/gtest-message.h>
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <gtest/gtest.h>
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing ::testing::Message;
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing ::testing::internal::StrStream;
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper function that turns a Message into a C string.
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* ToCString(const Message& msg) {
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static testing::internal::String result;
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result = msg.GetString();
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return result.c_str();
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the testing::Message class
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the default constructor.
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, DefaultConstructor) {
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Message msg;
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("", ToCString(msg));
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the copy constructor.
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, CopyConstructor) {
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Message msg1("Hello");
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Message msg2(msg1);
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Hello", ToCString(msg2));
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests constructing a Message from a C-string.
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, ConstructsFromCString) {
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg("Hello");
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Hello", ToCString(msg));
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a non-char pointer.
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsPointer) {
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int n = 0;
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int* p = &n;
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STRNE("(null)", ToCString(Message() << p));
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NULL non-char pointer.
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNullPointer) {
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int* p = NULL;
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("(null)", ToCString(Message() << p));
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a C string.
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsCString) {
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Foo", ToCString(Message() << "Foo"));
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NULL C string.
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNullCString) {
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  char* p = NULL;
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("(null)", ToCString(Message() << p));
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_STRING
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming std::string.
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// As std::string has problem in MSVC when exception is disabled, we only
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test this where std::string can be used.
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsString) {
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ::std::string str("Hello");
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Hello", ToCString(Message() << str));
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that we can output strings containing embedded NULs.
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsStringWithEmbeddedNUL) {
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char char_array_with_nul[] =
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      "Here's a NUL\0 and some more string";
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ::std::string string_with_nul(char_array_with_nul,
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                      sizeof(char_array_with_nul) - 1);
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Here's a NUL\\0 and some more string",
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania               ToCString(Message() << string_with_nul));
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_STD_STRING
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NUL char.
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNULChar) {
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("\\0", ToCString(Message() << '\0'));
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming int.
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsInt) {
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("123", ToCString(Message() << 123));
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that basic IO manipulators (endl, ends, and flush) can be
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// streamed to Message.
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsBasicIoManip) {
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.",
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania               ToCString(Message() << "Line 1." << std::endl
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                         << "A NUL char " << std::ends << std::flush
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                         << " in line 2."));
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests Message::GetString()
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, GetString) {
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg;
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg << 1 << " lamb";
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("1 lamb", msg.GetString().c_str());
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a Message object to an ostream.
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsToOStream) {
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg("Hello");
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  StrStream ss;
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ss << msg;
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_STREQ("Hello", testing::internal::StrStreamToString(&ss).c_str());
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a Message object doesn't take up too much stack space.
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_LE(sizeof(Message), 16U);
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace
158