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 3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-message.h" 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing ::testing::Message; 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the testing::Message class 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the default constructor. 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, DefaultConstructor) { 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg; 47fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("", msg.GetString()); 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the copy constructor. 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, CopyConstructor) { 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg1("Hello"); 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg2(msg1); 54fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Hello", msg2.GetString()); 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests constructing a Message from a C-string. 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, ConstructsFromCString) { 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg("Hello"); 60fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Hello", msg.GetString()); 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests streaming a float. 6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(MessageTest, StreamsFloat) { 65fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString(); 6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Both numbers should be printed with enough precision. 67fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str()); 68fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str()); 6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 7041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests streaming a double. 7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(MessageTest, StreamsDouble) { 73fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes const std::string s = (Message() << 1260570880.4555497 << " " 74fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << 1260572265.1954534).GetString(); 7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Both numbers should be printed with enough precision. 76fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str()); 77fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str()); 7841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 7941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a non-char pointer. 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsPointer) { 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int n = 0; 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int* p = &n; 84fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_NE("(null)", (Message() << p).GetString()); 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NULL non-char pointer. 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNullPointer) { 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int* p = NULL; 90fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("(null)", (Message() << p).GetString()); 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a C string. 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsCString) { 95fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Foo", (Message() << "Foo").GetString()); 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NULL C string. 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNullCString) { 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char* p = NULL; 101fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("(null)", (Message() << p).GetString()); 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming std::string. 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsString) { 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string str("Hello"); 107fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Hello", (Message() << str).GetString()); 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that we can output strings containing embedded NULs. 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsStringWithEmbeddedNUL) { 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char char_array_with_nul[] = 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Here's a NUL\0 and some more string"; 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string string_with_nul(char_array_with_nul, 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania sizeof(char_array_with_nul) - 1); 116fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Here's a NUL\\0 and some more string", 117fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes (Message() << string_with_nul).GetString()); 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a NUL char. 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsNULChar) { 122fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("\\0", (Message() << '\0').GetString()); 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming int. 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsInt) { 127fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("123", (Message() << 123).GetString()); 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that basic IO manipulators (endl, ends, and flush) can be 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// streamed to Message. 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsBasicIoManip) { 133fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Line 1.\nA NUL char \\0 in line 2.", 134fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes (Message() << "Line 1." << std::endl 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "A NUL char " << std::ends << std::flush 136fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << " in line 2.").GetString()); 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests Message::GetString() 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, GetString) { 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << 1 << " lamb"; 143fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("1 lamb", msg.GetString()); 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests streaming a Message object to an ostream. 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, StreamsToOStream) { 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg("Hello"); 14941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream ss; 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ss << msg; 151fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss)); 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a Message object doesn't take up too much stack space. 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(MessageTest, DoesNotTakeUpMuchStackSpace) { 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_LE(sizeof(Message), 16U); 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace 160