googletest
googletest copied to clipboard
Improve support for printing std::basic_string_view
Extend printing capabilities for std::basic_string_view
What is std::basic_string_view?
std::basic_string_view (spec) is a relatively new (since C++17) form of manipulating string objects in C++.
The language also provides a suffix for it, so the following line will create and initiate a new std::string_view (synonym for std::basic_string_view<char>) object:
const auto str_view = "hello world"sv;
Does GTest support it?
Currently, GTest has some means of outputting these objects. It will be handled by the ContainerPrinter with one exception.
It will look a bit ugly though. Printing L"w"sv will produce { L'w' (119, 0x77) }.
Exception
GTest declares its own StringView which depending on the build parameters will be either absl::string_view or std::string_view.
Besides that GTest implements printing capability for that StringView type. Unfortunately, in the best case scenario it covers only one of the subtypes of the std::basic_string_view family. That is why in the #4295 author mentioned that the issue appears only for non-char basic_string_view's.
Solution
To improve the way std::basic_string_view is printed, PrintTo function was overloaded with all the available subtypes of the std::basic_string_view (just the way it's done for the std::basic_string):
std::string_viewstd::u8string_viewstd::u16string_viewstd::u32string_viewstd::wstring_view
This should fix #4295.
Notes
std::string_viewwill be added only in case the language level supports it andGTestis built withabsl::string_view. Otherwise it skips the implementation, because it should be already done forinternal::StringViewtype.std::u8string_viewis implemented only when the flag__cpp_lib_char8_tis defined.std::wstring_viewis implemented if the flagGTEST_HAS_STD_WSTRINGis defined.
I followed the way it's done for similar std::basic_string<T> types, but I'd like to ask to double check it.
Tests
I added a few tests too.
If more needed, please let me know.