Add invalid header usage tests

This commit is contained in:
ado 2023-08-04 21:22:23 +02:00
parent c981ed6644
commit 634abdd38b
4 changed files with 25 additions and 53 deletions

View File

@ -344,39 +344,6 @@ private:
} }
} }
void set_error_invalid_mapping() {
constexpr static auto error_msg = "received empty mapping";
if constexpr (string_error) {
error_.clear();
error_.append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{error_msg};
} else {
error_ = true;
}
}
void set_error_mapping_out_of_range(size_t maximum_index,
size_t number_of_columnts) {
constexpr static auto error_msg1 = "maximum index: ";
constexpr static auto error_msg2 = ", greater than number of columns: ";
if constexpr (string_error) {
error_.clear();
error_.append(error_msg1)
.append(std::to_string(maximum_index))
.append(error_msg2)
.append(std::to_string(number_of_columnts));
} else if constexpr (throw_on_error) {
throw ss::exception{error_msg1 + std::to_string(maximum_index) +
error_msg2 +
std::to_string(number_of_columnts)};
} else {
error_ = true;
}
}
//////////////// ////////////////
// convert implementation // convert implementation
//////////////// ////////////////
@ -433,19 +400,9 @@ private:
return column_mappings_[tuple_position]; return column_mappings_[tuple_position];
} }
// assumes positions are valid and the vector is not empty
void set_column_mapping(std::vector<size_t> positions, void set_column_mapping(std::vector<size_t> positions,
size_t number_of_columns) { size_t number_of_columns) {
if (positions.empty()) {
set_error_invalid_mapping();
return;
}
auto max_index = *std::max_element(positions.begin(), positions.end());
if (max_index >= number_of_columns) {
set_error_mapping_out_of_range(max_index, number_of_columns);
return;
}
column_mappings_ = positions; column_mappings_ = positions;
number_of_columns_ = number_of_columns; number_of_columns_ = number_of_columns;
} }

View File

@ -138,6 +138,12 @@ public:
} }
auto fields = std::vector<std::string>{fields_args...}; auto fields = std::vector<std::string>{fields_args...};
if (fields.empty()) {
set_error_empty_mapping();
return;
}
std::vector<size_t> column_mappings; std::vector<size_t> column_mappings;
for (const auto& field : fields) { for (const auto& field : fields) {
@ -159,6 +165,7 @@ public:
reader_.converter_.set_column_mapping(column_mappings, header_.size()); reader_.converter_.set_column_mapping(column_mappings, header_.size());
reader_.next_line_converter_.set_column_mapping(column_mappings, reader_.next_line_converter_.set_column_mapping(column_mappings,
header_.size()); header_.size());
if (line() == 0) { if (line() == 0) {
ignore_next(); ignore_next();
} }
@ -485,15 +492,11 @@ private:
void set_error_invalid_conversion() { void set_error_invalid_conversion() {
if constexpr (string_error) { if constexpr (string_error) {
// TODO remove buffer from error msg
error_.append(file_name_) error_.append(file_name_)
.append(" ") .append(" ")
.append(std::to_string(reader_.line_number_)) .append(std::to_string(reader_.line_number_))
.append(": ") .append(": ")
.append(reader_.converter_.error_msg()) .append(reader_.converter_.error_msg());
.append(": \"")
.append(reader_.buffer_)
.append("\"");
} else if constexpr (!throw_on_error) { } else if constexpr (!throw_on_error) {
error_ = true; error_ = true;
} }
@ -538,6 +541,19 @@ private:
} }
} }
void set_error_empty_mapping() {
constexpr static auto error_msg = "received empty mapping";
if constexpr (string_error) {
error_.clear();
error_.append(error_msg);
} else if constexpr (throw_on_error) {
throw ss::exception{error_msg};
} else {
error_ = true;
}
}
//////////////// ////////////////
// line reading // line reading
//////////////// ////////////////

View File

@ -978,3 +978,4 @@ TEST_CASE("converter test invalid split conversions with exceptions") {
buff(R"(just,some,2,"strings\")"))); buff(R"(just,some,2,"strings\")")));
CHECK(c.unterminated_quote()); CHECK(c.unterminated_quote());
} }

View File

@ -1168,14 +1168,12 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
} }
out.close(); out.close();
/* TODO test
{ {
// No fields specified // No fields specified
ss::parser<Ts...> p{f.name, ","}; ss::parser<Ts...> p{f.name, ","};
p.use_fields(); auto command = [&] { p.use_fields(); };
CHECK(!p.valid()); expect_error_on_command(p, command);
} }
*/
{ {
// Unknown field // Unknown field