From 1028ddaf13739c396a2926c73998c7f53fc89d20 Mon Sep 17 00:00:00 2001 From: yangwentong <425822674@qq.com> Date: Thu, 29 Sep 2022 17:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAset=5Ftest.cc=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=89=E4=B8=AA=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2/set.cc | 11 ++++------- re2/testing/set_test.cc | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/re2/set.cc b/re2/set.cc index 920f146..d5d1079 100644 --- a/re2/set.cc +++ b/re2/set.cc @@ -75,10 +75,10 @@ namespace re2 if (re == NULL) { const char *msg = rure_error_message(err); - if(error) + if(error != NULL) { error->assign(msg); - LOG(ERROR) << "Error Compile '" << pattern.data() << "':" << msg << "'"; + LOG(ERROR) << "Regexp Error '" << pattern.data() << "':" << msg << "'"; } // rure_free(re); // rure_error_free(err); @@ -95,7 +95,7 @@ namespace re2 bool RE2::Set::Compile() { if (compiled_) { - LOG(DFATAL) << "RE2::Set::Compile() called more than once"; + LOG(ERROR) << "RE2::Set::Compile() called more than once"; return false; } compiled_ = true; @@ -113,13 +113,10 @@ namespace re2 patterns_lengths, PAT_COUNT, 0, NULL, err); if(re == NULL){ - const char *msg = rure_error_message(err); - std::cout << msg << std::endl; compiled_ = false; return false; } prog_.reset((Prog *)re); - // rure_set_free(re); compiled_ = true; return true; } @@ -140,7 +137,7 @@ namespace re2 // 2. v != NULL if (!compiled_) { - LOG(DFATAL) << "RE2::Set::Match() called before compiling"; + LOG(ERROR) << "RE2::Set::Match() called before compiling"; if (error_info != NULL) error_info->kind = kNotCompiled; return false; diff --git a/re2/testing/set_test.cc b/re2/testing/set_test.cc index 720603d..b03c967 100644 --- a/re2/testing/set_test.cc +++ b/re2/testing/set_test.cc @@ -254,4 +254,33 @@ TEST(Set, MoveSemantics) { ASSERT_EQ(s1.Match("abc bar2 xyz", NULL), false); } +TEST(Set, FailCompile) { + RE2::Set s(RE2::DefaultOptions, RE2::ANCHOR_START); + ASSERT_EQ(s.Add("foo", NULL), 0); + ASSERT_EQ(s.Add("(", NULL), -1); + ASSERT_EQ(s.Add("bar", NULL), 1); + ASSERT_EQ(s.Compile(), true); + ASSERT_EQ(s.Compile(), false); // RE2::Set::Compile() called more than once +} + +TEST(Set, FailMatch) { + RE2::Set s(RE2::DefaultOptions, RE2::ANCHOR_START); + ASSERT_EQ(s.Add("foo", NULL), 0); + ASSERT_EQ(s.Add("(", NULL), -1); + ASSERT_EQ(s.Add("bar", NULL), 1); + std::vector v; + RE2::Set::ErrorInfo error; + ASSERT_EQ(s.Match("foobar", &v, &error), false); // RE2::Set::Match() called before compiling +} + +TEST(Set, FailAdd) { + RE2::Set s(RE2::DefaultOptions, RE2::ANCHOR_START); + ASSERT_EQ(s.Add("foo", NULL), 0); + std::string error; + ASSERT_EQ(s.Add("(", &error), -1); // Regexp Error + ASSERT_EQ(s.Add("bar", NULL), 1); +} + + + } // namespace re2 -- Gitee