proxygen
ExceptionTest.cpp File Reference
#include <folly/lang/Exception.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <folly/Portability.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  MyException
 
class  ExceptionTest
 

Functions

template<typename Ex >
static std::string type_pretty_name ()
 
template<typename Ex >
static std::string message_for_terminate_with (std::string const &what)
 
static std::string message_for_terminate ()
 
 TEST_F (ExceptionTest, throw_exception_direct)
 
 TEST_F (ExceptionTest, throw_exception_variadic)
 
 TEST_F (ExceptionTest, terminate_with_direct)
 
 TEST_F (ExceptionTest, terminate_with_variadic)
 
 TEST_F (ExceptionTest, invoke_noreturn_cold)
 

Function Documentation

static std::string message_for_terminate ( )
static

Definition at line 51 of file ExceptionTest.cpp.

References folly::kIsGlibcxx, and folly::kIsLibcpp.

Referenced by TEST_F().

51  {
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 }
constexpr auto kIsLibcpp
Definition: Portability.h:379
constexpr auto kIsGlibcxx
Definition: Portability.h:373
template<typename Ex >
static std::string message_for_terminate_with ( std::string const &  what)
static

Definition at line 39 of file ExceptionTest.cpp.

References folly::kIsGlibcxx, folly::kIsLibcpp, name, prefix(), and string.

39  {
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 }
bool prefix(Cursor &c, uint32_t expected)
constexpr auto kIsLibcpp
Definition: Portability.h:379
const char * name
Definition: http_parser.c:437
constexpr auto kIsGlibcxx
Definition: Portability.h:373
const char * string
Definition: Conv.cpp:212
TEST_F ( ExceptionTest  ,
throw_exception_direct   
)

Definition at line 76 of file ExceptionTest.cpp.

References ADD_FAILURE, EXPECT_STREQ, and MyException::what().

76  {
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 }
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
char const * what() const noexceptoverride
#define ADD_FAILURE()
Definition: gtest.h:1808
TEST_F ( ExceptionTest  ,
throw_exception_variadic   
)

Definition at line 85 of file ExceptionTest.cpp.

References ADD_FAILURE, EXPECT_STREQ, and MyException::what().

85  {
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 }
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
char const * what() const noexceptoverride
#define ADD_FAILURE()
Definition: gtest.h:1808
TEST_F ( ExceptionTest  ,
terminate_with_direct   
)

Definition at line 94 of file ExceptionTest.cpp.

94  {
95  EXPECT_DEATH(
96  folly::terminate_with<MyException>("hello world"),
97  message_for_terminate_with<MyException>("hello world"));
98 }
TEST_F ( ExceptionTest  ,
terminate_with_variadic   
)

Definition at line 100 of file ExceptionTest.cpp.

100  {
101  EXPECT_DEATH(
102  folly::terminate_with<MyException>("hello world", 6),
103  message_for_terminate_with<MyException>("world"));
104 }
TEST_F ( ExceptionTest  ,
invoke_noreturn_cold   
)

Definition at line 106 of file ExceptionTest.cpp.

References EXPECT_THROW, folly::invoke_noreturn_cold(), and message_for_terminate().

106  {
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
FOLLY_NOINLINE FOLLY_COLD void invoke_noreturn_cold(F &&f, A &&...a)
Definition: Exception.h:122
static std::string message_for_terminate()
template<typename Ex >
static std::string type_pretty_name ( )
static

Definition at line 27 of file ExceptionTest.cpp.

References min, name, folly::size(), and string.

27  {
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 }
const char * name
Definition: http_parser.c:437
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
LogLevel min
Definition: LogLevel.cpp:30
const char * string
Definition: Conv.cpp:212