1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2024-11-25 00:53:43 +00:00

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.
This commit is contained in:
King_DuckZ 2016-05-11 22:24:30 +02:00
parent 05aaaebe0d
commit 217b41ace2
2 changed files with 47 additions and 4 deletions

View file

@ -31,6 +31,12 @@
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/phoenix/operator.hpp>
//#define AST_DEBUG_OUTPUT
#if defined(AST_DEBUG_OUTPUT)
# include <iostream>
#endif
namespace qi = boost::spirit::qi;
namespace g2r {
@ -41,10 +47,10 @@ namespace g2r {
~GlobGrammar ( void ) = default;
qi::rule<Iterator, AstType()> start;
qi::rule<Iterator, AstType()> alternation_list;
qi::rule<Iterator, GlobAlternation()> alternation;
qi::rule<Iterator, GlobGroup()> group;
qi::rule<Iterator, std::string()> literal;
qi::rule<Iterator, std::vector<std::vector<GlobNode>>()> comma_list;
qi::rule<Iterator, std::string()> single_char_comma_list;
qi::rule<Iterator, char()> escaped_glob;
qi::rule<Iterator, GlobJolly()> 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;
}

View file

@ -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);
}
}