Add EXPECT_{NO_,}_PCRE_MATCH macros
authorAlexey Spiridonov <lesha@fb.com>
Thu, 26 Mar 2015 23:16:15 +0000 (16:16 -0700)
committerafrind <afrind@fb.com>
Thu, 2 Apr 2015 18:58:17 +0000 (11:58 -0700)
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

folly/experimental/TestUtil.cpp
folly/experimental/TestUtil.h
folly/experimental/test/TestUtilTest.cpp

index 788e36287fea2d759999c9fac365324bd7cb8ac7..ebeb7aec854df6eea4e21d49a3d1b3ec2ff01e85 100644 (file)
@@ -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
index 30687bf72a994f9948d2fa70c7fb741c7455e323..a949162c5aec9637685d8a46839832d13a64d3cb 100644 (file)
@@ -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
 
index 4239c94c394a5b152a3970b253f118c32a5c0bfe..c479e76feb2aac5d790fc7238edb691f50bd32f7 100644 (file)
@@ -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);