Skip to content

Commit

Permalink
update EXPECT_THROW_RE() to accept the pattern as a string_view
Browse files Browse the repository at this point in the history
Summary:
Previously the `EXPECT_THROW_RE()` macro required the regular expression be
passed in as a `const char*`.  This updates the code to accept a
`std::string_view` instead.

This allows the API to accept either `const char*` arguments, `std::string`,
or `std::string_view` objects.

Reviewed By: yfeldblum

Differential Revision: D25837542

fbshipit-source-id: e6fb00034046ad7f2810367f946f0135858ed065
  • Loading branch information
simpkins authored and facebook-github-bot committed Jan 21, 2021
1 parent 59b4747 commit 9c9f1d2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
12 changes: 7 additions & 5 deletions folly/test/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <chrono>
#include <regex>
#include <string_view>
#include <system_error>
#include <type_traits>

Expand Down Expand Up @@ -211,7 +212,8 @@ class CheckResult {
* Helper function for implementing EXPECT_THROW
*/
template <typename Fn>
CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) {
CheckResult
checkThrowErrno(Fn&& fn, int errnoValue, std::string_view statementStr) {
try {
fn();
} catch (const std::system_error& ex) {
Expand Down Expand Up @@ -259,9 +261,9 @@ CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) {
template <typename ExType, typename Fn>
CheckResult checkThrowRegex(
Fn&& fn,
const char* pattern,
const char* statementStr,
const char* excTypeStr) {
std::string_view pattern,
std::string_view statementStr,
std::string_view excTypeStr) {
static_assert(
std::is_base_of<std::exception, ExType>::value,
"EXPECT_THROW_RE() exception type must derive from std::exception");
Expand All @@ -277,7 +279,7 @@ CheckResult checkThrowRegex(
<< exceptionStr(ex);
}

std::regex re(pattern);
std::regex re(pattern.data(), pattern.size());
if (!std::regex_search(derived->what(), re)) {
return CheckResult(false)
<< "Expected: " << statementStr << " throws a " << excTypeStr
Expand Down
48 changes: 48 additions & 0 deletions folly/test/TestUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

#include <folly/test/TestUtils.h>

#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>

using namespace folly;
using namespace std::string_literals;
using ::testing::MatchesRegex;
using ::testing::PrintToString;

TEST(TestUtilsFbStringTest, Ascii) {
Expand Down Expand Up @@ -93,3 +95,49 @@ TEST(TestUtilsRangeTest, Utf32) {
const auto kHelloString = U"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece));
}

TEST(ExpectThrowRegex, SuccessCases) {
EXPECT_THROW_RE(throw std::runtime_error("test"), std::runtime_error, "test");
EXPECT_THROW_RE(
throw std::invalid_argument("test 123"), std::invalid_argument, ".*123");
EXPECT_THROW_RE(
throw std::invalid_argument("test 123"),
std::invalid_argument,
std::string("t.*123"));
}

TEST(ExpectThrowRegex, FailureCases) {
std::string failureMsg;
auto recordFailure = [&](const char* msg) { failureMsg = msg; };

TEST_THROW_RE_(
throw std::runtime_error("test"),
std::invalid_argument,
"test",
recordFailure);
EXPECT_THAT(
failureMsg,
MatchesRegex(".*Actual: it throws a different exception type.*"));

failureMsg = "";
TEST_THROW_RE_(
throw std::runtime_error("test"),
std::runtime_error,
"xest",
recordFailure);
EXPECT_THAT(
failureMsg,
MatchesRegex("Expected:.* throws a std::runtime_error with message "
"matching \"xest\".*Actual: message is: test"));

failureMsg = "";
TEST_THROW_RE_(
throw std::runtime_error("abc"),
std::runtime_error,
std::string("xyz"),
recordFailure);
EXPECT_THAT(
failureMsg,
MatchesRegex("Expected:.* throws a std::runtime_error with message "
"matching \"xyz\".*Actual: message is: abc"));
}

0 comments on commit 9c9f1d2

Please sign in to comment.