From 217b41ace20d9cc74c7b3d8d43729a6ead17db4b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 11 May 2016 22:24:30 +0200 Subject: [PATCH] Add new tests for glob2regex and try to fix the failing one glob2regex is not 100% accurate yet, but it works at least for the cases in the accompanying unit test. --- lib/glob2regex/src/glob_ast.cpp | 16 +++++++++++---- test/unit/test_glob2regex.cpp | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/glob2regex/src/glob_ast.cpp b/lib/glob2regex/src/glob_ast.cpp index d2b2205..913f411 100644 --- a/lib/glob2regex/src/glob_ast.cpp +++ b/lib/glob2regex/src/glob_ast.cpp @@ -31,6 +31,12 @@ #include #include +//#define AST_DEBUG_OUTPUT + +#if defined(AST_DEBUG_OUTPUT) +# include +#endif + namespace qi = boost::spirit::qi; namespace g2r { @@ -41,10 +47,10 @@ namespace g2r { ~GlobGrammar ( void ) = default; qi::rule start; + qi::rule alternation_list; qi::rule alternation; qi::rule group; qi::rule literal; - qi::rule>()> comma_list; qi::rule single_char_comma_list; qi::rule escaped_glob; qi::rule jolly; @@ -69,9 +75,9 @@ namespace g2r { const uint16_t uint16_one = 1; start = *(group | alternation | literal | jolly); - comma_list = start % ","; + alternation_list = *(group | alternation | as_string[+(~char_("{}[]*\\+? ,") | escaped_glob)] | jolly); single_char_comma_list = ~char_(special_char_list) % ","; - alternation = eps >> lit("{") >> comma_list >> "}"; + alternation = eps >> lit("{") >> (alternation_list % ",") >> "}"; group = (lit("[") >> matches[lit("!")] >> as_string[-string("]") >> *(~char_(']') | escaped_glob)] >> "]") | (attr(false) >> lit("{") >> single_char_comma_list >> lit("}")); @@ -96,12 +102,14 @@ namespace g2r { glob_ast ); - std::cout << "make_ast() - parse_ret = "; +#if defined(AST_DEBUG_OUTPUT) + std::cout << "make_ast(\"" << parGlob << "\") - parse_ret = "; if (parse_ret) std::cout << "true"; else std::cout << "false"; std::cout << ", glob_ast.size() = " << glob_ast.size() << std::endl; +#endif return glob_ast; } diff --git a/test/unit/test_glob2regex.cpp b/test/unit/test_glob2regex.cpp index 45a420c..5e617d1 100644 --- a/test/unit/test_glob2regex.cpp +++ b/test/unit/test_glob2regex.cpp @@ -26,4 +26,39 @@ TEST(glob2regex, convert) { const auto auto_regex = g2r::convert(glob); EXPECT_EQ(expected_regex, auto_regex); } + + { + const std::string glob = "*.jp*g"; + const std::string expected_regex = "[^/]*\\.jp[^/]*g$"; + const auto auto_regex = g2r::convert(glob); + EXPECT_EQ(expected_regex, auto_regex); + } + + { + const std::string glob = "/home/duckz/sample_file.bin"; + const std::string expected_regex = "/home/duckz/sample_file\\.bin$"; + const auto auto_regex = g2r::convert(glob); + EXPECT_EQ(expected_regex, auto_regex); + } + + { + const std::string glob = "**/main.{c,h}pp"; + const std::string expected_regex = ".*/main\\.[ch]pp$"; + const auto auto_regex = g2r::convert(glob); + EXPECT_EQ(expected_regex, auto_regex); + } + + { + const std::string glob = "photo_???.JPG"; + const std::string expected_regex = "photo_[^/][^/][^/]\\.JPG$"; + const auto auto_regex = g2r::convert(glob); + EXPECT_EQ(expected_regex, auto_regex); + } + + { + const std::string glob = "nested_{example,*sample,test}"; + const std::string expected_regex = "nested_(?:example|[^/]*sample|test)$"; + const auto auto_regex = g2r::convert(glob); + EXPECT_EQ(expected_regex, auto_regex); + } }