/* * 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 #include #include namespace apache::thrift::test { namespace detail { template class ThriftFieldMatcher; template class IsThriftUnionWithMatcher; } // namespace detail // A Thrift field matcher. // // Given a Thrift struct // // struct Person { // 1: string name; // 2: optional int id; // } // // the matcher can be used as follows: // // auto p = Person(); // p.name() = "Zaphod"; // p.id() = 42 // EXPECT_THAT(p, ThriftField(Eq("Zaphod"))); // EXPECT_THAT(p. ThriftField(Optional(Eq("Zaphod")))) // // The `name` and `id` types are declared inside the `apache::thrift::ident` // namespace in the `_types.h` generated file. // A useful pattern is to define `namespace field = apache::thrift::ident;`, and // refer to the fields as `field::field_name`, e.g. // `ThriftField(_)`. // // Alternatively, the following also works (but doesn't print the field name on // failure): // // EXPECT_THAT(p, ThriftField(&Person::name<>, Eq("Zaphod"))); // EXPECT_THAT(p. ThriftField(&Person::id<>, Optional(Eq("Zaphod")))) // template auto ThriftField( apache::thrift::field_ref (Struct::*ref)() const&, const Matcher& matcher) { return testing::ResultOf( [=](const Struct& s) { return *(s.*ref)(); }, matcher); } template auto ThriftField( apache::thrift::optional_field_ref (Struct::*ref)() const&, const Matcher& matcher) { return testing::ResultOf( [=](const Struct& s) { return (s.*ref)(); }, matcher); } template detail::ThriftFieldMatcher ThriftField(Matcher matcher) { return detail::ThriftFieldMatcher(matcher); } // A Thrift union(variant) matcher. // // Given a Thrift union // // union Result { // 1: int success; // 2: string error; // } // // the matcher can be used as follows: // // auto r = Result(); // r.set_success(1); // EXPECT_THAT(p, IsThriftUnionWith(Eq(1))); // EXPECT_THAT(p, Not(IsThriftUnionWith(_))); // // The `success` and `error` types are declared inside the // `apache::thrift::ident` namespace in the `_types.h` generated file. A useful // pattern is to define `namespace field = apache::thrift::ident;`, and refer to // the fields as `field::field_name`, e.g. // `IsThriftUnionWith(_)`. // template detail::IsThriftUnionWithMatcher IsThriftUnionWith( Matcher matcher) { return detail::IsThriftUnionWithMatcher(matcher); } } // namespace apache::thrift::test #include