/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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. */ #pragma once #include #include // portability/GTest must be imported before any other gtest header #include #include #include #include namespace apache::thrift { template void PrintTo(field_ref obj, std::ostream* os) { *os << "field_ref holding " << testing::PrintToString(*obj); } template void PrintTo(optional_field_ref obj, std::ostream* os) { if (!obj) { *os << "empty optional_field_ref"; return; } *os << "optional_field_ref holding " << testing::PrintToString(*obj); } #if __cpp_concepts namespace detail { template constexpr bool is_type_complete() { return requires { sizeof(T); }; } template constexpr bool supports_custom_protocol() { // TccStructTraits is defied in the _custom_protocol.h header, so // we can check if that header has been included by checking if // the type is complete return is_type_complete>(); } } // namespace detail // WARNING: The behavior of this function depends on which headers // were included by **the first time it is invoked**. // The compiler caches the first template instantiation, so the detection // on whether the _types_custom_protocol.h was included or not // happens at that point. // If you include the header, but still see the fallback message, // make sure to include the header **above** whatever else you are // including that might be calling this function (normally some other testing // code). template requires( is_thrift_class_v> || is_thrift_exception_v< std::remove_cvref_t>) void PrintTo(const T& obj, std::ostream* os) { if constexpr (detail::supports_custom_protocol>()) { *os << debugString(obj); } else { *os << "a thrift object of type " << folly::pretty_name() << " (include the corresponding _types_custom_protocol.h header before thrift/lib/cpp2/test/Printer.h to see the contents)"; } } } // namespace apache::thrift #endif