proxygen
ExceptionTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
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 #include <folly/lang/Exception.h>
18 
19 #include <algorithm>
20 #include <cstring>
21 #include <string>
22 
23 #include <folly/Portability.h>
25 
26 template <typename Ex>
28  auto const name = __PRETTY_FUNCTION__;
29  auto const size = std::strlen(name);
30  auto const eq = std::find(name, name + size, '=');
31  auto const sc = std::find(name, name + size, ';');
32  auto const br = std::find(name, name + size, ']');
33  auto const bpos = name + size - eq >= 2 ? eq + 2 : name + size;
34  auto const epos = std::min(sc, br);
35  return epos < bpos ? "" : std::string(bpos, epos - bpos);
36 }
37 
38 template <typename Ex>
40  auto const name = type_pretty_name<Ex>();
41  auto const prefix =
42  std::string("terminate called after throwing an instance of ");
43  // clang-format off
44  return
45  folly::kIsGlibcxx ? prefix + "'" + name + "'\\s+what\\(\\):\\s+" + what :
46  folly::kIsLibcpp ? prefix + name + ": " + what :
47  "" /* empty regex matches anything */;
48  // clang-format on
49 }
50 
52  // clang-format off
53  return
54  folly::kIsGlibcxx ? "terminate called without an active exception" :
55  folly::kIsLibcpp ? "terminating" :
56  "" /* empty regex matches anything */;
57  // clang-format on
58 }
59 
60 class MyException : public std::exception {
61  private:
62  char const* what_;
63 
64  public:
65  explicit MyException(char const* const what) : MyException(what, 0) {}
66  MyException(char const* const what, std::size_t const strip)
67  : what_(what + strip) {}
68 
69  char const* what() const noexcept override {
70  return what_;
71  }
72 };
73 
74 class ExceptionTest : public testing::Test {};
75 
76 TEST_F(ExceptionTest, throw_exception_direct) {
77  try {
78  folly::throw_exception<MyException>("hello world");
79  ADD_FAILURE();
80  } catch (MyException const& ex) {
81  EXPECT_STREQ("hello world", ex.what());
82  }
83 }
84 
85 TEST_F(ExceptionTest, throw_exception_variadic) {
86  try {
87  folly::throw_exception<MyException>("hello world", 6);
88  ADD_FAILURE();
89  } catch (MyException const& ex) {
90  EXPECT_STREQ("world", ex.what());
91  }
92 }
93 
94 TEST_F(ExceptionTest, terminate_with_direct) {
95  EXPECT_DEATH(
96  folly::terminate_with<MyException>("hello world"),
97  message_for_terminate_with<MyException>("hello world"));
98 }
99 
100 TEST_F(ExceptionTest, terminate_with_variadic) {
101  EXPECT_DEATH(
102  folly::terminate_with<MyException>("hello world", 6),
103  message_for_terminate_with<MyException>("world"));
104 }
105 
107  EXPECT_THROW(
108  folly::invoke_noreturn_cold([] { throw std::runtime_error("bad"); }),
109  std::runtime_error);
110  EXPECT_DEATH(folly::invoke_noreturn_cold([] {}), message_for_terminate());
111 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
char const * what_
requires E e noexcept(noexcept(s.error(std::move(e))))
bool prefix(Cursor &c, uint32_t expected)
MyException(char const *const what, std::size_t const strip)
FOLLY_NOINLINE FOLLY_COLD void invoke_noreturn_cold(F &&f, A &&...a)
Definition: Exception.h:122
TEST_F(ExceptionTest, throw_exception_direct)
constexpr auto kIsLibcpp
Definition: Portability.h:379
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
const char * name
Definition: http_parser.c:437
std::uint64_t strip(std::chrono::nanoseconds t)
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
LogLevel min
Definition: LogLevel.cpp:30
MyException(char const *const what)
static std::string message_for_terminate_with(std::string const &what)
char const * what() const noexceptoverride
constexpr auto kIsGlibcxx
Definition: Portability.h:373
const char * string
Definition: Conv.cpp:212
const
Definition: upload.py:398
static std::string message_for_terminate()
#define ADD_FAILURE()
Definition: gtest.h:1808
static std::string type_pretty_name()