Commit 9c9f1d2d authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

update EXPECT_THROW_RE() to accept the pattern as a string_view

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
parent 59b47473
......@@ -31,6 +31,7 @@
#include <chrono>
#include <regex>
#include <string_view>
#include <system_error>
#include <type_traits>
......@@ -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) {
......@@ -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");
......@@ -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
......
......@@ -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) {
......@@ -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"));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment