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 @@ ...@@ -31,6 +31,7 @@
#include <chrono> #include <chrono>
#include <regex> #include <regex>
#include <string_view>
#include <system_error> #include <system_error>
#include <type_traits> #include <type_traits>
...@@ -211,7 +212,8 @@ class CheckResult { ...@@ -211,7 +212,8 @@ class CheckResult {
* Helper function for implementing EXPECT_THROW * Helper function for implementing EXPECT_THROW
*/ */
template <typename Fn> template <typename Fn>
CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) { CheckResult
checkThrowErrno(Fn&& fn, int errnoValue, std::string_view statementStr) {
try { try {
fn(); fn();
} catch (const std::system_error& ex) { } catch (const std::system_error& ex) {
...@@ -259,9 +261,9 @@ CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) { ...@@ -259,9 +261,9 @@ CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) {
template <typename ExType, typename Fn> template <typename ExType, typename Fn>
CheckResult checkThrowRegex( CheckResult checkThrowRegex(
Fn&& fn, Fn&& fn,
const char* pattern, std::string_view pattern,
const char* statementStr, std::string_view statementStr,
const char* excTypeStr) { std::string_view excTypeStr) {
static_assert( static_assert(
std::is_base_of<std::exception, ExType>::value, std::is_base_of<std::exception, ExType>::value,
"EXPECT_THROW_RE() exception type must derive from std::exception"); "EXPECT_THROW_RE() exception type must derive from std::exception");
...@@ -277,7 +279,7 @@ CheckResult checkThrowRegex( ...@@ -277,7 +279,7 @@ CheckResult checkThrowRegex(
<< exceptionStr(ex); << exceptionStr(ex);
} }
std::regex re(pattern); std::regex re(pattern.data(), pattern.size());
if (!std::regex_search(derived->what(), re)) { if (!std::regex_search(derived->what(), re)) {
return CheckResult(false) return CheckResult(false)
<< "Expected: " << statementStr << " throws a " << excTypeStr << "Expected: " << statementStr << " throws a " << excTypeStr
......
...@@ -16,10 +16,12 @@ ...@@ -16,10 +16,12 @@
#include <folly/test/TestUtils.h> #include <folly/test/TestUtils.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using namespace folly; using namespace folly;
using namespace std::string_literals; using namespace std::string_literals;
using ::testing::MatchesRegex;
using ::testing::PrintToString; using ::testing::PrintToString;
TEST(TestUtilsFbStringTest, Ascii) { TEST(TestUtilsFbStringTest, Ascii) {
...@@ -93,3 +95,49 @@ TEST(TestUtilsRangeTest, Utf32) { ...@@ -93,3 +95,49 @@ TEST(TestUtilsRangeTest, Utf32) {
const auto kHelloString = U"hello"s; const auto kHelloString = U"hello"s;
EXPECT_EQ(PrintToString(kHelloString), PrintToString(kHelloStringPiece)); 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