diff --git a/src/tools/regexTools.cpp b/src/tools/regexTools.cpp index 3f1d5b8ab..914404085 100644 --- a/src/tools/regexTools.cpp +++ b/src/tools/regexTools.cpp @@ -30,7 +30,7 @@ std::map> regexCache; static pthread_mutex_t regexLock = PTHREAD_MUTEX_INITIALIZER; -std::unique_ptr buildMatcher(const std::string& regex, const icu::UnicodeString& content) +std::unique_ptr buildMatcher(const std::string& regex, icu::UnicodeString& content) { std::shared_ptr pattern; /* Regex is in cache */ @@ -56,7 +56,8 @@ std::unique_ptr buildMatcher(const std::string& regex, const bool matchRegex(const std::string& content, const std::string& regex) { ucnv_setDefaultName("UTF-8"); - auto matcher = buildMatcher(regex, content.c_str()); + icu::UnicodeString ucontent(content.c_str()); + auto matcher = buildMatcher(regex, ucontent); return matcher->find(); } diff --git a/test/meson.build b/test/meson.build index b08cabd47..fbd814bba 100644 --- a/test/meson.build +++ b/test/meson.build @@ -2,7 +2,8 @@ tests = [ 'parseUrl', - 'library' + 'library', + 'regex' ] diff --git a/test/regex.cpp b/test/regex.cpp new file mode 100644 index 000000000..bdcd132ab --- /dev/null +++ b/test/regex.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2019 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "gtest/gtest.h" +#include + + +#include "../include/tools/regexTools.h" + +namespace +{ + +TEST(MatchRegex, match) +{ + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "f")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "ef")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "efg")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "mno")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "m*o")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "m*v")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "M")); + EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "STU")); + EXPECT_TRUE(matchRegex("Mythology & Folklore Stack Exchange", "folklore")); + EXPECT_TRUE(matchRegex("Mythology & Folklore Stack Exchange", "Folklore")); + EXPECT_TRUE(matchRegex("Mythology & Folklore Stack Exchange", "folklore")); +} + +TEST(MatchRegex, nomatch) +{ + EXPECT_FALSE(matchRegex("abcdefghijklmnopqrstuvwxyz", "vu")); +} + +TEST(ReplaceRegex, beginning) +{ + EXPECT_EQ(replaceRegex("abcdefghij", "----", "abcd"), "----efghij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "abcde"), "----fghij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "a.*i"), "----j"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "AbCd"), "----efghij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "A"), "----bcdefghij"); +} + +TEST(ReplaceRegex, end) +{ + EXPECT_EQ(replaceRegex("abcdefghij", "----", "ghij"), "abcdef----"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "fghij"), "abcde----"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "c.*j"), "ab----"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "GhIj"), "abcdef----"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "J"), "abcdefghi----"); +} + +TEST(ReplaceRegex, middle) +{ + EXPECT_EQ(replaceRegex("abcdefghij", "----", "cdef"), "ab----ghij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "cdefgh"), "ab----ij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "c.*f"), "ab----ghij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "DeFg"), "abc----hij"); + EXPECT_EQ(replaceRegex("abcdefghij", "----", "F"), "abcde----ghij"); +} + +TEST(append, beggining) +{ + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "abcd", "----"), "abcd----efghij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "abcde", "----"), "abcde----fghij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "a.*i", "----"), "abcdefghi----j"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "AbCd", "----"), "abcd----efghij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "A", "----"), "a----bcdefghij"); +} + +TEST(append, end) +{ + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "ghij", "----"), "abcdefghij----"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "fghij", "----"), "abcdefghij----"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "c.*j", "----"), "abcdefghij----"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "GhIj", "----"), "abcdefghij----"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "J", "----"), "abcdefghij----"); +} + +TEST(append, middle) +{ + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "cdef", "----"), "abcdef----ghij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "cdefgh", "----"), "abcdefgh----ij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "c.*f", "----"), "abcdef----ghij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "DeFg", "----"), "abcdefg----hij"); + EXPECT_EQ(appendToFirstOccurence("abcdefghij", "F", "----"), "abcdef----ghij"); +} + +}; + +int main(int argc, char** argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/travis/compile.sh b/travis/compile.sh index bf33f1c84..8720e5ca0 100755 --- a/travis/compile.sh +++ b/travis/compile.sh @@ -5,13 +5,15 @@ set -e BUILD_DIR=${HOME}/BUILD_${PLATFORM} INSTALL_DIR=${BUILD_DIR}/INSTALL - +TEST=0 case ${PLATFORM} in "native_static") MESON_OPTION="--default-library=static" + TEST=1 ;; "native_dyn") MESON_OPTION="--default-library=shared" + TEST=1 ;; "win32_static") MESON_OPTION="--default-library=static --cross-file ${BUILD_DIR}/meson_cross_file.txt" @@ -39,3 +41,9 @@ export CPPFLAGS="-I${INSTALL_DIR}/include" meson . build ${MESON_OPTION} cd build ninja +if [[ "$TEST" == "1" ]] +then + echo "Running test" + export LD_LIBRARY_PATH=${INSTALL_DIR}/lib:${INSTALL_DIR}/lib64:${INSTALL_DIR}/lib/x86_64-linux-gnu + ninja test +fi