Adds the UniversalPrinter for absl::variant.

This commit is contained in:
Derek Mauro 2018-07-12 13:46:50 -04:00
parent 41f0e24335
commit 6c7878a151
2 changed files with 42 additions and 17 deletions

View File

@ -51,7 +51,6 @@ config_setting(
values = {"define": "absl=1"}, values = {"define": "absl=1"},
) )
# Google Test including Google Mock # Google Test including Google Mock
cc_library( cc_library(
name = "gtest", name = "gtest",
@ -81,6 +80,14 @@ cc_library(
"//conditions:default": ["-pthread"], "//conditions:default": ["-pthread"],
}, },
), ),
defines = select(
{
":has_absl": [
"GTEST_HAS_ABSL=1",
],
"//conditions:default": [],
},
),
includes = [ includes = [
"googlemock", "googlemock",
"googlemock/include", "googlemock/include",
@ -94,21 +101,16 @@ cc_library(
"-pthread", "-pthread",
], ],
}), }),
defines = select ({ deps = select(
":has_absl": [ {
"GTEST_HAS_ABSL=1",
],
"//conditions:default": [],
}
),
deps = select ({
":has_absl": [ ":has_absl": [
"@com_google_absl//absl/types:optional", "@com_google_absl//absl/types:optional",
"@com_google_absl//absl/strings" "@com_google_absl//absl/types:variant",
"@com_google_absl//absl/strings",
], ],
"//conditions:default": [], "//conditions:default": [],
} },
) ),
) )
cc_library( cc_library(

View File

@ -114,6 +114,7 @@
#if GTEST_HAS_ABSL #if GTEST_HAS_ABSL
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "absl/types/variant.h"
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
namespace testing { namespace testing {
@ -787,6 +788,28 @@ class UniversalPrinter<::absl::optional<T>> {
} }
}; };
// Printer for absl::variant
template <typename... T>
class UniversalPrinter<::absl::variant<T...>> {
public:
static void Print(const ::absl::variant<T...>& value, ::std::ostream* os) {
*os << '(';
absl::visit(Visitor{os}, value);
*os << ')';
}
private:
struct Visitor {
template <typename U>
void operator()(const U& u) const {
*os << "'" << GetTypeName<U>() << "' with value ";
UniversalPrint(u, os);
}
::std::ostream* os;
};
};
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
// UniversalPrintArray(begin, len, os) prints an array of 'len' // UniversalPrintArray(begin, len, os) prints an array of 'len'