1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef THRIFT_FATAL_INTERNAL_TEST_HELPERS_H
18 #define THRIFT_FATAL_INTERNAL_TEST_HELPERS_H
19 
20 #include <string>
21 #include <tuple>
22 #include <typeinfo>
23 
24 #include <folly/portability/GTest.h>
25 
26 #include <folly/Demangle.h>
27 
28 namespace apache {
29 namespace thrift {
30 namespace detail {
31 
32 struct expect_same {
expect_sameexpect_same33   expect_same(char const* filename, std::size_t line)
34       : filename_(filename), line_(line) {}
35 
36   template <typename LHS, typename RHS>
checkexpect_same37   void check() const {
38     using type =
39         std::tuple<std::string, char const*, std::size_t, char const*, bool>;
40     auto const lhs_name = folly::demangle(typeid(LHS));
41     auto const rhs_name = folly::demangle(typeid(RHS));
42     auto const line_caption = "line: ";
43     type const lhs(filename_, line_caption, line_, lhs_name.c_str(), true);
44     type const rhs(
45         filename_,
46         line_caption,
47         line_,
48         lhs_name == rhs_name ? lhs_name.c_str() : rhs_name.c_str(),
49         std::is_same<LHS, RHS>::value);
50     EXPECT_EQ(lhs, rhs);
51   }
52 
53  private:
54   std::string const filename_;
55   std::size_t const line_;
56 };
57 
58 #define EXPECT_SAME \
59   ::apache::thrift::detail::expect_same(__FILE__, __LINE__).check
60 
61 } // namespace detail
62 } // namespace thrift
63 } // namespace apache
64 
65 #endif // THRIFT_FATAL_INTERNAL_TEST_HELPERS_H
66