mirror of
https://github.com/red0124/ssp.git
synced 2025-12-14 21:59:55 +01:00
add mismatched quote error, update error handling for splitter, add unit tests, update test_helpers buffer
This commit is contained in:
@@ -115,6 +115,9 @@ TEST_CASE("testing invalid conversions") {
|
||||
c.convert<int>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int>("10", "");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
c.convert<int, void>("");
|
||||
REQUIRE(!c.valid());
|
||||
|
||||
@@ -438,3 +441,24 @@ TEST_CASE("testing converter with quotes spacing and escaping") {
|
||||
CHECK(tup == std::make_tuple("ju,st", "so,me", 12.34, "str\"ings"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing invalid split conversions") {
|
||||
ss::converter<ss::escape<'\\'>, ss::trim<' '>, ss::quote<'"'>> c;
|
||||
c.set_error_mode(ss::error_mode::error_string);
|
||||
|
||||
{
|
||||
// mismatched quote
|
||||
auto tup = c.convert<std::string, std::string, double, char>(
|
||||
buff(R"( "just , some , "12.3","a" )"));
|
||||
CHECK(!c.valid());
|
||||
CHECK(!c.unterminated_quote());
|
||||
}
|
||||
|
||||
{
|
||||
// unterminated quote
|
||||
auto tup = c.convert<std::string, std::string, double, std::string>(
|
||||
buff(R"( ju\,st , "so,me" , 12.34 , "str""ings)"));
|
||||
CHECK(!c.valid());
|
||||
CHECK(c.unterminated_quote());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef CMAKE_GITHUB_CI
|
||||
@@ -8,19 +9,35 @@
|
||||
#endif
|
||||
|
||||
class buffer {
|
||||
constexpr static auto buff_size = 1024;
|
||||
char data_[buff_size];
|
||||
char* data_{nullptr};
|
||||
|
||||
public:
|
||||
char* operator()(const char* data) {
|
||||
memset(data_, '\0', buff_size);
|
||||
if (data_) {
|
||||
delete[] data_;
|
||||
}
|
||||
data_ = new char[strlen(data) + 1];
|
||||
strcpy(data_, data);
|
||||
return data_;
|
||||
}
|
||||
|
||||
char* append(const char* data) {
|
||||
strcat(data_, data);
|
||||
return data_;
|
||||
if (data_) {
|
||||
char* new_data_ = new char[strlen(data_) + strlen(data) + 1];
|
||||
strcpy(new_data_, data_);
|
||||
strcat(new_data_, data);
|
||||
delete[] data_;
|
||||
data_ = new_data_;
|
||||
return data_;
|
||||
} else {
|
||||
return operator()(data);
|
||||
}
|
||||
}
|
||||
|
||||
~buffer() {
|
||||
if (data_) {
|
||||
delete[] data_;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -668,3 +668,28 @@ TEST_CASE("testing unterminated quote") {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("testing invalid splits") {
|
||||
ss::splitter<ss::quote<'"'>, ss::trim<' '>, ss::escape<'\\'>> s;
|
||||
|
||||
// empty delimiter
|
||||
s.split(buff("some,random,strings"), "");
|
||||
CHECK(!s.valid());
|
||||
CHECK(!s.unterminated_quote());
|
||||
|
||||
// mismatched delimiter
|
||||
s.split(buff(R"(some,"random,"strings")"));
|
||||
CHECK(!s.valid());
|
||||
CHECK(!s.unterminated_quote());
|
||||
|
||||
// unterminated quote
|
||||
s.split(buff("some,random,\"strings"));
|
||||
CHECK(!s.valid());
|
||||
CHECK(s.unterminated_quote());
|
||||
|
||||
// invalid resplit
|
||||
char new_line[] = "some";
|
||||
auto a = s.resplit(new_line, strlen(new_line));
|
||||
CHECK(!s.valid());
|
||||
CHECK(!s.unterminated_quote());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user