write unit tests for resplit, fix some issues with resplit

This commit is contained in:
ado 2021-01-30 21:52:32 +01:00
parent a5c9216824
commit 1bf6b9d595
3 changed files with 129 additions and 12 deletions

View File

@ -135,11 +135,10 @@ public:
split_input& resplit(line_ptr_type new_line, ssize_t new_size, split_input& resplit(line_ptr_type new_line, ssize_t new_size,
const std::string& delimiter = default_delimiter) { const std::string& delimiter = default_delimiter) {
clear_error();
line_ = new_line; line_ = new_line;
// resplitting, continue from last slice // resplitting, continue from last slice
if (!output_.empty()) { if (!output_.empty() && unterminated_quote()) {
const auto& last = std::prev(output_.end()); const auto& last = std::prev(output_.end());
const auto [old_line, old_begin] = *last; const auto [old_line, old_begin] = *last;
size_t begin = old_begin - old_line - 1; size_t begin = old_begin - old_line - 1;
@ -152,7 +151,7 @@ public:
return output_; return output_;
} }
line_ += begin; begin_ = line_ + begin;
} }
return split_impl_select_delim(delimiter); return split_impl_select_delim(delimiter);
@ -287,6 +286,7 @@ private:
split_input& split_impl_select_delim( split_input& split_impl_select_delim(
const std::string& delimiter = default_delimiter) { const std::string& delimiter = default_delimiter) {
clear_error();
switch (delimiter.size()) { switch (delimiter.size()) {
case 0: case 0:
set_error_empty_delimiter(); set_error_empty_delimiter();
@ -301,7 +301,10 @@ private:
template <typename Delim> template <typename Delim>
split_input& split_impl(const Delim& delim) { split_input& split_impl(const Delim& delim) {
state_ = state::begin; state_ = state::begin;
if (output_.empty()) {
begin_ = line_; begin_ = line_;
}
trim_if_enabled(begin_); trim_if_enabled(begin_);

View File

@ -17,6 +17,11 @@ public:
strcpy(data_, data); strcpy(data_, data);
return data_; return data_;
} }
char* append(const char* data) {
strcat(data_, data);
return data_;
}
}; };
[[maybe_unused]] inline buffer buff; [[maybe_unused]] inline buffer buff;

View File

@ -525,7 +525,7 @@ TEST_CASE("testing unterminated quote") {
auto vec = expect_unterminated_quote(s, "\"just"); auto vec = expect_unterminated_quote(s, "\"just");
CHECK(vec.size() == 1); CHECK(vec.size() == 1);
char new_line[] = R"("just",strings)"; auto new_line = buff.append(R"(",strings)");
vec = s.resplit(new_line, strlen(new_line)); vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid()); CHECK(s.valid());
CHECK(!s.unterminated_quote()); CHECK(!s.unterminated_quote());
@ -539,7 +539,7 @@ TEST_CASE("testing unterminated quote") {
std::vector<std::string> expected{"just", "some", "just,some,\""}; std::vector<std::string> expected{"just", "some", "just,some,\""};
CHECK(words(vec) == expected); CHECK(words(vec) == expected);
char new_line[] = R"(just,some,"random",strings)"; auto new_line = buff.append(R"(",strings)");
vec = s.resplit(new_line, strlen(new_line)); vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid()); CHECK(s.valid());
CHECK(!s.unterminated_quote()); CHECK(!s.unterminated_quote());
@ -547,17 +547,126 @@ TEST_CASE("testing unterminated quote") {
CHECK(words(vec) == expected); CHECK(words(vec) == expected);
} }
{
ss::splitter<ss::quote<'"'>> s;
auto vec = expect_unterminated_quote(s, R"("just","some","ran"")");
std::vector<std::string> expected{"just", "some", R"("just","some",")"};
CHECK(words(vec) == expected);
auto new_line = buff.append(R"(,dom","strings")");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"just", "some", "ran\",dom", "strings"};
CHECK(words(vec) == expected);
}
{ {
ss::splitter<ss::quote<'"'>> s; ss::splitter<ss::quote<'"'>> s;
auto vec = expect_unterminated_quote(s, R"("just","some","ran)"); auto vec = expect_unterminated_quote(s, R"("just","some","ran)");
std::vector<std::string> expected{"just", "some", R"("just","some",")"}; std::vector<std::string> expected{"just", "some", R"("just","some",")"};
CHECK(words(vec) == expected); CHECK(words(vec) == expected);
char new_line[] = R"("just","some","ran,dom","strings")"; {
auto new_line = buff.append(R"(,dom)");
vec = s.resplit(new_line, strlen(new_line));
CHECK(!s.valid());
CHECK(s.unterminated_quote());
CHECK(words(vec) == expected);
}
{
auto new_line = buff.append(R"(",strings)");
vec = s.resplit(new_line, strlen(new_line)); vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid()); CHECK(s.valid());
CHECK(!s.unterminated_quote()); CHECK(!s.unterminated_quote());
expected = {"just", "some", "ran,dom", "strings"}; expected = {"just", "some", "ran,dom", "strings"};
CHECK(words(vec) == expected); CHECK(words(vec) == expected);
} }
}
{
ss::splitter<ss::quote<'"'>, ss::escape<'\\'>> s;
auto vec = expect_unterminated_quote(s, R"("just\"some","ra)");
std::vector<std::string> expected{"just\"some"};
auto w = words(vec);
w.pop_back();
CHECK(w == expected);
{
auto new_line = buff.append(R"(n,dom",str\"ings)");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"just\"some", "ran,dom", "str\"ings"};
CHECK(words(vec) == expected);
}
}
{
ss::splitter<ss::quote<'"'>, ss::escape<'\\'>> s;
auto vec = expect_unterminated_quote(s, R"("just\"some","ra"")");
std::vector<std::string> expected{"just\"some"};
auto w = words(vec);
w.pop_back();
CHECK(w == expected);
{
auto new_line = buff.append(R"(n,dom",str\"ings)");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"just\"some", "ra\"n,dom", "str\"ings"};
CHECK(words(vec) == expected);
}
}
{
ss::splitter<ss::quote<'"'>, ss::escape<'\\'>> s;
auto vec = expect_unterminated_quote(s, R"("just\"some","ra\")");
std::vector<std::string> expected{"just\"some"};
auto w = words(vec);
w.pop_back();
CHECK(w == expected);
{
auto new_line = buff.append(R"(n,dom",str\"ings)");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"just\"some", "ra\"n,dom", "str\"ings"};
CHECK(words(vec) == expected);
}
}
{
ss::splitter<ss::quote<'"'>, ss::trim<' '>> s;
auto vec = expect_unterminated_quote(s, R"( "just" ,some, "ra )");
std::vector<std::string> expected{"just", "some"};
auto w = words(vec);
w.pop_back();
CHECK(w == expected);
{
auto new_line = buff.append(R"( n,dom" , strings )");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"just", "some", "ra n,dom", "strings"};
CHECK(words(vec) == expected);
}
}
{
ss::splitter<ss::quote<'"'>, ss::trim<' '>, ss::escape<'\\'>> s;
auto vec = expect_unterminated_quote(s, R"( "ju\"st" ,some, "ra \")");
std::vector<std::string> expected{"ju\"st", "some"};
auto w = words(vec);
w.pop_back();
CHECK(w == expected);
{
auto new_line = buff.append(R"( n,dom" , strings )");
vec = s.resplit(new_line, strlen(new_line));
CHECK(s.valid());
CHECK(!s.unterminated_quote());
expected = {"ju\"st", "some", "ra \" n,dom", "strings"};
CHECK(words(vec) == expected);
}
}
} }