Path: blob/master/dep/googletest/include/gtest/gtest-message.h
4806 views
// Copyright 2005, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// The Google C++ Testing and Mocking Framework (Google Test)30//31// This header file defines the Message class.32//33// IMPORTANT NOTE: Due to limitation of the C++ language, we have to34// leave some internal implementation details in this header file.35// They are clearly marked by comments like this:36//37// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.38//39// Such code is NOT meant to be used by a user directly, and is subject40// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user41// program!4243// IWYU pragma: private, include "gtest/gtest.h"44// IWYU pragma: friend gtest/.*45// IWYU pragma: friend gmock/.*4647#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_48#define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_4950#include <limits>51#include <memory>52#include <ostream>53#include <sstream>54#include <string>5556#include "gtest/internal/gtest-port.h"5758#ifdef GTEST_HAS_ABSL59#include <type_traits>6061#include "absl/strings/has_absl_stringify.h"62#include "absl/strings/str_cat.h"63#endif // GTEST_HAS_ABSL6465GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \66/* class A needs to have dll-interface to be used by clients of class B */)6768// Ensures that there is at least one operator<< in the global namespace.69// See Message& operator<<(...) below for why.70void operator<<(const testing::internal::Secret&, int);7172namespace testing {7374// The Message class works like an ostream repeater.75//76// Typical usage:77//78// 1. You stream a bunch of values to a Message object.79// It will remember the text in a stringstream.80// 2. Then you stream the Message object to an ostream.81// This causes the text in the Message to be streamed82// to the ostream.83//84// For example;85//86// testing::Message foo;87// foo << 1 << " != " << 2;88// std::cout << foo;89//90// will print "1 != 2".91//92// Message is not intended to be inherited from. In particular, its93// destructor is not virtual.94//95// Note that stringstream behaves differently in gcc and in MSVC. You96// can stream a NULL char pointer to it in the former, but not in the97// latter (it causes an access violation if you do). The Message98// class hides this difference by treating a NULL char pointer as99// "(null)".100class GTEST_API_ Message {101private:102// The type of basic IO manipulators (endl, ends, and flush) for103// narrow streams.104typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);105106public:107// Constructs an empty Message.108Message();109110// Copy constructor.111Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT112*ss_ << msg.GetString();113}114115// Constructs a Message from a C-string.116explicit Message(const char* str) : ss_(new ::std::stringstream) {117*ss_ << str;118}119120// Streams a non-pointer value to this object. If building a version of121// GoogleTest with ABSL, this overload is only enabled if the value does not122// have an AbslStringify definition.123template <124typename T125#ifdef GTEST_HAS_ABSL126,127typename std::enable_if<!absl::HasAbslStringify<T>::value, // NOLINT128int>::type = 0129#endif // GTEST_HAS_ABSL130>131inline Message& operator<<(const T& val) {132// Some libraries overload << for STL containers. These133// overloads are defined in the global namespace instead of ::std.134//135// C++'s symbol lookup rule (i.e. Koenig lookup) says that these136// overloads are visible in either the std namespace or the global137// namespace, but not other namespaces, including the testing138// namespace which Google Test's Message class is in.139//140// To allow STL containers (and other types that has a << operator141// defined in the global namespace) to be used in Google Test142// assertions, testing::Message must access the custom << operator143// from the global namespace. With this using declaration,144// overloads of << defined in the global namespace and those145// visible via Koenig lookup are both exposed in this function.146using ::operator<<;147*ss_ << val;148return *this;149}150151#ifdef GTEST_HAS_ABSL152// Streams a non-pointer value with an AbslStringify definition to this153// object.154template <typename T,155typename std::enable_if<absl::HasAbslStringify<T>::value, // NOLINT156int>::type = 0>157inline Message& operator<<(const T& val) {158// ::operator<< is needed here for a similar reason as with the non-Abseil159// version above160using ::operator<<;161*ss_ << absl::StrCat(val);162return *this;163}164#endif // GTEST_HAS_ABSL165166// Streams a pointer value to this object.167//168// This function is an overload of the previous one. When you169// stream a pointer to a Message, this definition will be used as it170// is more specialized. (The C++ Standard, section171// [temp.func.order].) If you stream a non-pointer, then the172// previous definition will be used.173//174// The reason for this overload is that streaming a NULL pointer to175// ostream is undefined behavior. Depending on the compiler, you176// may get "0", "(nil)", "(null)", or an access violation. To177// ensure consistent result across compilers, we always treat NULL178// as "(null)".179template <typename T>180inline Message& operator<<(T* const& pointer) { // NOLINT181if (pointer == nullptr) {182*ss_ << "(null)";183} else {184*ss_ << pointer;185}186return *this;187}188189// Since the basic IO manipulators are overloaded for both narrow190// and wide streams, we have to provide this specialized definition191// of operator <<, even though its body is the same as the192// templatized version above. Without this definition, streaming193// endl or other basic IO manipulators to Message will confuse the194// compiler.195Message& operator<<(BasicNarrowIoManip val) {196*ss_ << val;197return *this;198}199200// Instead of 1/0, we want to see true/false for bool values.201Message& operator<<(bool b) { return *this << (b ? "true" : "false"); }202203// These two overloads allow streaming a wide C string to a Message204// using the UTF-8 encoding.205Message& operator<<(const wchar_t* wide_c_str);206Message& operator<<(wchar_t* wide_c_str);207208#if GTEST_HAS_STD_WSTRING209// Converts the given wide string to a narrow string using the UTF-8210// encoding, and streams the result to this Message object.211Message& operator<<(const ::std::wstring& wstr);212#endif // GTEST_HAS_STD_WSTRING213214// Gets the text streamed to this object so far as an std::string.215// Each '\0' character in the buffer is replaced with "\\0".216//217// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.218std::string GetString() const;219220private:221// We'll hold the text streamed to this object here.222const std::unique_ptr< ::std::stringstream> ss_;223224// We declare (but don't implement) this to prevent the compiler225// from implementing the assignment operator.226void operator=(const Message&);227};228229// Streams a Message to an ostream.230inline std::ostream& operator<<(std::ostream& os, const Message& sb) {231return os << sb.GetString();232}233234namespace internal {235236// Converts a streamable value to an std::string. A NULL pointer is237// converted to "(null)". When the input value is a ::string,238// ::std::string, ::wstring, or ::std::wstring object, each NUL239// character in it is replaced with "\\0".240template <typename T>241std::string StreamableToString(const T& streamable) {242return (Message() << streamable).GetString();243}244245} // namespace internal246} // namespace testing247248GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251249250#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_251252253