Commit d42a8052 authored by Phil Willoughby's avatar Phil Willoughby Committed by Facebook Github Bot

googletest's PrintTo for folly strings

Summary:
Prints StringPiece/FixedString/fbstring like a string and not like a vector of char when implicitly printed by googletest/googlemock.

The order of printing precedence within googletest is:
* ADL `PrintTo(T const&, ostream*)`
* Gtest's container-printer for anything that looks like a container
* ADL `ostream& operator<<(ostream&, T const&)`

Unfortunately you need to include the <folly/test/TestUtils.h> header in every test to have this functionality. The googletest mechanism for overloading this means we can't do it in the main headers without introducing a dependency from them to googletest.

Reviewed By: yfeldblum, luciang

Differential Revision: D13306040

fbshipit-source-id: 4e8252d377835dad04625d84a0cae7c0713ce8af
parent 62080229
......@@ -24,9 +24,9 @@
* - EXPECT_THROW_ERRNO(), ASSERT_THROW_ERRNO()
* - AreWithinSecs()
*
* Additionally, it includes a PrintTo() function for StringPiece.
* Including this file in your tests will ensure that StringPiece is printed
* nicely when used in EXPECT_EQ() or EXPECT_NE() checks.
* It also imports PrintTo() functions for StringPiece, FixedString and
* FBString. Including this file in your tests will ensure that they are printed
* as strings by googletest - for example in failing EXPECT_EQ() checks.
*/
#include <chrono>
......@@ -36,6 +36,8 @@
#include <folly/Conv.h>
#include <folly/ExceptionString.h>
#include <folly/FBString.h>
#include <folly/FixedString.h>
#include <folly/Range.h>
#include <folly/portability/GTest.h>
......@@ -264,16 +266,41 @@ CheckResult checkThrowRegex(
} // namespace detail
} // namespace test
// Define a PrintTo() function for StringPiece, so that gtest checks
// will print it as a string. Without this gtest identifies StringPiece as a
// container type, and therefore tries printing its elements individually,
// despite the fact that there is an ostream operator<<() defined for
// StringPiece.
inline void PrintTo(StringPiece sp, ::std::ostream* os) {
// gtest's PrintToString() function will quote the string and escape internal
// quotes and non-printable characters, the same way gtest does for the
// standard string types.
*os << ::testing::PrintToString(sp.str());
// Define PrintTo() functions for StringPiece/FixedString/fbstring, so that
// gtest checks will print them as strings. Without these gtest identifies them
// as container types, and therefore tries printing the elements individually,
// despite the fact that there is an ostream operator<<() defined for each of
// them.
//
// gtest's PrintToString() function is used to quote the string and escape
// internal quotes and non-printable characters, the same way gtest does for the
// string types it directly supports.
inline void PrintTo(StringPiece const& stringPiece, std::ostream* out) {
*out << ::testing::PrintToString(stringPiece.str());
}
inline void PrintTo(
Range<wchar_t const*> const& stringPiece,
std::ostream* out) {
*out << ::testing::PrintToString(
std::wstring(stringPiece.begin(), stringPiece.size()));
}
template <typename CharT, size_t N>
void PrintTo(
BasicFixedString<CharT, N> const& someFixedString,
std::ostream* out) {
*out << ::testing::PrintToString(someFixedString.toStdString());
}
template <typename CharT, class Storage>
void PrintTo(
basic_fbstring<
CharT,
std::char_traits<CharT>,
std::allocator<CharT>,
Storage> const& someFbString,
std::ostream* out) {
*out << ::testing::PrintToString(someFbString.toStdString());
}
} // namespace folly
/*
* Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/test/TestUtils.h>
#include <folly/portability/GTest.h>
using namespace folly;
using namespace std::string_literals;
using ::testing::PrintToString;
TEST(TestUtilsFbStringTest, Ascii) {
const auto kHelloFbString = fbstring("hello");
const auto kHelloString = "hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFbString));
}
TEST(TestUtilsFbStringTest, Wide) {
const auto kHelloFbString = basic_fbstring<wchar_t>(L"hello");
const auto kHelloString = L"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFbString));
}
TEST(TestUtilsFbStringTest, Utf16) {
const auto kHelloFbString = basic_fbstring<char16_t>(u"hello");
const auto kHelloString = u"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFbString));
}
TEST(TestUtilsFbStringTest, Utf32) {
const auto kHelloFbString = basic_fbstring<char32_t>(U"hello");
const auto kHelloString = U"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFbString));
}
TEST(TestUtilsFixedStringTest, Ascii) {
constexpr auto kHelloFixedString = makeFixedString("hello");
const auto kHelloString = "hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFixedString));
}
TEST(TestUtilsFixedStringTest, Wide) {
constexpr auto kHelloFixedString = makeFixedString(L"hello");
const auto kHelloString = L"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFixedString));
}
TEST(TestUtilsFixedStringTest, Utf16) {
constexpr auto kHelloFixedString = makeFixedString(u"hello");
const auto kHelloString = u"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFixedString));
}
TEST(TestUtilsFixedStringTest, Utf32) {
constexpr auto kHelloFixedString = makeFixedString(U"hello");
const auto kHelloString = U"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloFixedString));
}
TEST(TestUtilsRangeTest, Ascii) {
constexpr auto kHelloStringPiece = "hello"_sp;
const auto kHelloString = "hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece));
}
TEST(TestUtilsRangeTest, Wide) {
constexpr auto kHelloStringPiece = L"hello"_sp;
const auto kHelloString = L"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece));
}
TEST(TestUtilsRangeTest, Utf16) {
constexpr auto kHelloStringPiece = u"hello"_sp;
const auto kHelloString = u"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece));
}
TEST(TestUtilsRangeTest, Utf32) {
constexpr auto kHelloStringPiece = U"hello"_sp;
const auto kHelloString = U"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment