From: Alexey Spiridonov Date: Thu, 26 Mar 2015 23:16:15 +0000 (-0700) Subject: Add EXPECT_{NO_,}_PCRE_MATCH macros X-Git-Tag: v0.33.0~22 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=133fc50c97e7d3d81855fb9fedf034b1ad056feb 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 --- diff --git a/folly/experimental/TestUtil.cpp b/folly/experimental/TestUtil.cpp index 788e3628..ebeb7aec 100644 --- a/folly/experimental/TestUtil.cpp +++ b/folly/experimental/TestUtil.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -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 diff --git a/folly/experimental/TestUtil.h b/folly/experimental/TestUtil.h index 30687bf7..a949162c 100644 --- a/folly/experimental/TestUtil.h +++ b/folly/experimental/TestUtil.h @@ -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 diff --git a/folly/experimental/test/TestUtilTest.cpp b/folly/experimental/test/TestUtilTest.cpp index 4239c94c..c479e76f 100644 --- a/folly/experimental/test/TestUtilTest.cpp +++ b/folly/experimental/test/TestUtilTest.cpp @@ -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);