Commit 133fc50c authored by Alexey Spiridonov's avatar Alexey Spiridonov Committed by afrind

Add EXPECT_{NO_,}_PCRE_MATCH macros

Summary: There are about 40 callsites to these across a couple of projects, and they seem generally useful.

Test Plan: ```fbconfig folly/experimental/test && fbmake runtests```

Reviewed By: yfeldblum@fb.com

Subscribers: folly-diffs@, yfeldblum

FB internal diff: D1933415

Signature: t1:1933415:1427345307:6587eea3dac74c5d841950ace3c3501d6e3dbe4a
parent bf0d2143
......@@ -20,6 +20,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <boost/regex.hpp>
#include <folly/Conv.h>
#include <folly/Exception.h>
......@@ -41,7 +42,7 @@ fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
return path;
}
} // namespce
} // namespace
TemporaryFile::TemporaryFile(StringPiece namePrefix,
fs::path dir,
......@@ -117,5 +118,21 @@ ChangeToTempDir::~ChangeToTempDir() {
::chdir(p.c_str());
}
namespace detail {
bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
return boost::regex_match(
target.begin(),
target.end(),
boost::regex(pattern.begin(), pattern.end())
);
}
bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
return !hasPCREPatternMatch(pattern, target);
}
} // namespace detail
} // namespace test
} // namespace folly
......@@ -116,6 +116,29 @@ private:
TemporaryDirectory dir_;
};
/**
* Easy PCRE regex matching. Note that pattern must match the ENTIRE target,
* so use .* at the start and end of the pattern, as appropriate. See
* http://regex101.com/ for a PCRE simulator.
*/
#define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
EXPECT_PRED2( \
::folly::test::detail::hasPCREPatternMatch, \
pattern_stringpiece, \
target_stringpiece \
)
#define EXPECT_NO_PCRE_MATCH(pattern_stringpiece, target_stringpiece) \
EXPECT_PRED2( \
::folly::test::detail::hasNoPCREPatternMatch, \
pattern_stringpiece, \
target_stringpiece \
)
namespace detail {
bool hasPCREPatternMatch(StringPiece pattern, StringPiece target);
bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target);
} // namespace detail
} // namespace test
} // namespace folly
......
......@@ -109,6 +109,12 @@ TEST(ChangeToTempDir, ChangeDir) {
EXPECT_EQ(pwd1, fs::current_path());
}
TEST(PCREPatternMatch, Simple) {
EXPECT_PCRE_MATCH(".*a.c.*", "gabca");
EXPECT_NO_PCRE_MATCH("a.c", "gabca");
EXPECT_NO_PCRE_MATCH(".*ac.*", "gabca");
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
......
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