mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-08-06 13:19:47 +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:
parent
05aaaebe0d
commit
217b41ace2
2 changed files with 47 additions and 4 deletions
|
@ -31,6 +31,12 @@
|
||||||
#include <boost/phoenix/bind/bind_member_function.hpp>
|
#include <boost/phoenix/bind/bind_member_function.hpp>
|
||||||
#include <boost/phoenix/operator.hpp>
|
#include <boost/phoenix/operator.hpp>
|
||||||
|
|
||||||
|
//#define AST_DEBUG_OUTPUT
|
||||||
|
|
||||||
|
#if defined(AST_DEBUG_OUTPUT)
|
||||||
|
# include <iostream>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace qi = boost::spirit::qi;
|
namespace qi = boost::spirit::qi;
|
||||||
|
|
||||||
namespace g2r {
|
namespace g2r {
|
||||||
|
@ -41,10 +47,10 @@ namespace g2r {
|
||||||
~GlobGrammar ( void ) = default;
|
~GlobGrammar ( void ) = default;
|
||||||
|
|
||||||
qi::rule<Iterator, AstType()> start;
|
qi::rule<Iterator, AstType()> start;
|
||||||
|
qi::rule<Iterator, AstType()> alternation_list;
|
||||||
qi::rule<Iterator, GlobAlternation()> alternation;
|
qi::rule<Iterator, GlobAlternation()> alternation;
|
||||||
qi::rule<Iterator, GlobGroup()> group;
|
qi::rule<Iterator, GlobGroup()> group;
|
||||||
qi::rule<Iterator, std::string()> literal;
|
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, std::string()> single_char_comma_list;
|
||||||
qi::rule<Iterator, char()> escaped_glob;
|
qi::rule<Iterator, char()> escaped_glob;
|
||||||
qi::rule<Iterator, GlobJolly()> jolly;
|
qi::rule<Iterator, GlobJolly()> jolly;
|
||||||
|
@ -69,9 +75,9 @@ namespace g2r {
|
||||||
const uint16_t uint16_one = 1;
|
const uint16_t uint16_one = 1;
|
||||||
|
|
||||||
start = *(group | alternation | literal | jolly);
|
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) % ",";
|
single_char_comma_list = ~char_(special_char_list) % ",";
|
||||||
alternation = eps >> lit("{") >> comma_list >> "}";
|
alternation = eps >> lit("{") >> (alternation_list % ",") >> "}";
|
||||||
group =
|
group =
|
||||||
(lit("[") >> matches[lit("!")] >> as_string[-string("]") >> *(~char_(']') | escaped_glob)] >> "]") |
|
(lit("[") >> matches[lit("!")] >> as_string[-string("]") >> *(~char_(']') | escaped_glob)] >> "]") |
|
||||||
(attr(false) >> lit("{") >> single_char_comma_list >> lit("}"));
|
(attr(false) >> lit("{") >> single_char_comma_list >> lit("}"));
|
||||||
|
@ -96,12 +102,14 @@ namespace g2r {
|
||||||
glob_ast
|
glob_ast
|
||||||
);
|
);
|
||||||
|
|
||||||
std::cout << "make_ast() - parse_ret = ";
|
#if defined(AST_DEBUG_OUTPUT)
|
||||||
|
std::cout << "make_ast(\"" << parGlob << "\") - parse_ret = ";
|
||||||
if (parse_ret)
|
if (parse_ret)
|
||||||
std::cout << "true";
|
std::cout << "true";
|
||||||
else
|
else
|
||||||
std::cout << "false";
|
std::cout << "false";
|
||||||
std::cout << ", glob_ast.size() = " << glob_ast.size() << std::endl;
|
std::cout << ", glob_ast.size() = " << glob_ast.size() << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
return glob_ast;
|
return glob_ast;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,39 @@ TEST(glob2regex, convert) {
|
||||||
const auto auto_regex = g2r::convert(glob);
|
const auto auto_regex = g2r::convert(glob);
|
||||||
EXPECT_EQ(expected_regex, auto_regex);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue