瀏覽代碼

test/fix: write test cases for regex engines, make StdRegexEngine noexcept

Sam Jaffe 2 周之前
父節點
當前提交
0460975795
共有 3 個文件被更改,包括 62 次插入10 次删除
  1. 6 9
      include/jvalidate/regex.h
  2. 1 1
      tests/CMakeLists.txt
  3. 55 0
      tests/regex_test.cxx

+ 6 - 9
include/jvalidate/regex.h

@@ -37,17 +37,14 @@ private:
 public:
   static std::string_view engine_name() { return "std::regex[ECMAScript]"; }
 
-  static bool is_regex(std::string_view regex) {
-    try {
-      [[maybe_unused]] std::regex _{std::string(regex)};
-      return true;
-    } catch (std::exception const &) { return false; }
-  }
+  static bool is_regex(std::string_view regex) try {
+    return (std::regex(std::string(regex)), true);
+  } catch (std::exception const &) { return false; }
 
-  bool search(std::string const & regex, std::string const & text) {
-    auto const & re = cache_.try_emplace(regex, regex).first->second;
+  bool search(std::string const & regex, std::string const & text) try {
+    std::regex const & re = cache_.try_emplace(regex, regex).first->second;
     return std::regex_search(text, re);
-  }
+  } catch (std::exception const &) { return false; }
 };
 }
 

+ 1 - 1
tests/CMakeLists.txt

@@ -22,7 +22,7 @@ include(GoogleTest)
 
 set(JVALIDATE_UNIT_TESTS
     annotation_test extension_test validation_visitor_test enum_test detail_test
-    adapter_test jsoncpp_adapter_test)
+    regex_test adapter_test jsoncpp_adapter_test)
 
 set(JVALIDATE_TESTS selfvalidate_test ${JVALIDATE_UNIT_TESTS})
 

+ 55 - 0
tests/regex_test.cxx

@@ -0,0 +1,55 @@
+#include "jvalidate/regex.h"
+
+#include "gtest/gtest.h"
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using testing::IsEmpty;
+using testing::Not;
+
+template <typename T> class RegexEngineTest : public testing::Test {};
+
+using RegexEngines = testing::Types<jvalidate::StdRegexEngine
+#if JVALIDATE_HAS_ICU
+                                    ,
+                                    jvalidate::ICURegexEngine
+#endif
+                                    >;
+
+TYPED_TEST_SUITE(RegexEngineTest, RegexEngines);
+
+TYPED_TEST(RegexEngineTest, HasEngineName) {
+  EXPECT_THAT(TypeParam::engine_name(), Not(IsEmpty()));
+}
+
+TYPED_TEST(RegexEngineTest, IsRegexIsNoexceptOnBadRegex) {
+  EXPECT_NO_THROW(TypeParam::is_regex("(ABC){1,2"));
+  EXPECT_FALSE(TypeParam::is_regex("(ABC){1,2"));
+}
+
+TYPED_TEST(RegexEngineTest, IsRegexIsNoexceptOnGoodRegex) {
+  EXPECT_NO_THROW(TypeParam::is_regex("(ABC){1,2}"));
+  EXPECT_TRUE(TypeParam::is_regex("(ABC){1,2}"));
+}
+
+TYPED_TEST(RegexEngineTest, SearchCanMatchSubstring) {
+  EXPECT_NO_THROW(TypeParam().search("\\d", "10 dollars"));
+  EXPECT_TRUE(TypeParam().search("\\d", "10 dollars"));
+}
+
+TYPED_TEST(RegexEngineTest, SearchCanSetBoundaries) {
+  EXPECT_NO_THROW(TypeParam().search("^\\d$", "10 dollars"));
+  EXPECT_FALSE(TypeParam().search("^\\d$", "10 dollars"));
+}
+
+TYPED_TEST(RegexEngineTest, SearchIsNoexceptOnBadRegex) {
+  EXPECT_NO_THROW(TypeParam().search("(ABC){1,2", "ABC"));
+  EXPECT_FALSE(TypeParam().search("(ABC){1,2", "ABC"));
+}
+
+#if !defined(JVALIDATE_MONOTEST)
+int main(int argc, char ** argv) {
+  testing::InitGoogleMock(&argc, argv);
+  return RUN_ALL_TESTS();
+}
+#endif