Add positions method to parser, write unit tests for it, update other parser tests

This commit is contained in:
ado 2024-02-25 02:06:48 +01:00
parent 383de57f9a
commit 88e711a5f7
9 changed files with 460 additions and 425 deletions

View File

@ -106,6 +106,10 @@ public:
: reader_.line_number_; : reader_.line_number_;
} }
size_t position() const {
return reader_.chars_read_;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error; std::optional<std::string> error;
@ -694,6 +698,7 @@ private:
csv_data_size_{other.csv_data_size_}, csv_data_size_{other.csv_data_size_},
curr_char_{other.curr_char_}, crlf_{other.crlf_}, curr_char_{other.curr_char_}, crlf_{other.crlf_},
line_number_{other.line_number_}, line_number_{other.line_number_},
chars_read_{other.chars_read_},
next_line_size_{other.next_line_size_} { next_line_size_{other.next_line_size_} {
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
@ -718,12 +723,14 @@ private:
curr_char_ = other.curr_char_; curr_char_ = other.curr_char_;
crlf_ = other.crlf_; crlf_ = other.crlf_;
line_number_ = other.line_number_; line_number_ = other.line_number_;
chars_read_ = other.chars_read_;
next_line_size_ = other.next_line_size_; next_line_size_ = other.next_line_size_;
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
other.helper_buffer_ = nullptr; other.helper_buffer_ = nullptr;
other.file_ = nullptr; other.file_ = nullptr;
other.csv_data_buffer_ = nullptr;
} }
return *this; return *this;
@ -803,9 +810,11 @@ private:
next_line_buffer_[0] = '\0'; next_line_buffer_[0] = '\0';
} }
chars_read_ = curr_char_;
if (file_) { if (file_) {
ssize = get_line_file(&next_line_buffer_, ssize = get_line_file(&next_line_buffer_,
&next_line_buffer_size_, file_); &next_line_buffer_size_, file_);
curr_char_ = ftell(file_);
} else { } else {
ssize = get_line_buffer(&next_line_buffer_, ssize = get_line_buffer(&next_line_buffer_,
&next_line_buffer_size_, &next_line_buffer_size_,
@ -1009,6 +1018,7 @@ private:
bool crlf_{false}; bool crlf_{false};
size_t line_number_{0}; size_t line_number_{0};
size_t chars_read_{0};
size_t next_line_size_{0}; size_t next_line_size_{0};
}; };

View File

@ -165,25 +165,25 @@ using get_multiline_t = typename get_multiline<Ts...>::type;
// string_error // string_error
//////////////// ////////////////
class string_error; class string_error {};
//////////////// ////////////////
// ignore_header // ignore_header
//////////////// ////////////////
class ignore_header; class ignore_header {};
//////////////// ////////////////
// ignore_empty // ignore_empty
//////////////// ////////////////
class ignore_empty; class ignore_empty {};
//////////////// ////////////////
// throw_on_error // throw_on_error
//////////////// ////////////////
class throw_on_error; class throw_on_error {};
//////////////// ////////////////
// setup implementation // setup implementation

83
ssp.hpp
View File

@ -646,7 +646,7 @@ inline ssize_t get_line_file(char** lineptr, size_t* n, FILE* stream) {
} }
#else #else
using ssize_t = int64_t; using ssize_t = intptr_t;
ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) { ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
if (lineptr == nullptr || n == nullptr || fp == nullptr) { if (lineptr == nullptr || n == nullptr || fp == nullptr) {
@ -670,14 +670,15 @@ ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
(*lineptr)[0] = '\0'; (*lineptr)[0] = '\0';
size_t line_used = 0;
while (fgets(buff, sizeof(buff), fp) != nullptr) { while (fgets(buff, sizeof(buff), fp) != nullptr) {
size_t line_used = strlen(*lineptr); line_used = strlen(*lineptr);
size_t buff_used = strlen(buff); size_t buff_used = strlen(buff);
if (*n < buff_used + line_used) { if (*n <= buff_used + line_used) {
size_t new_n = *n * 2; size_t new_n = *n * 2;
auto new_lineptr = static_cast<char*>(realloc(*lineptr, *n)); auto new_lineptr = static_cast<char*>(realloc(*lineptr, new_n));
if (new_lineptr == nullptr) { if (new_lineptr == nullptr) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@ -696,7 +697,7 @@ ssize_t get_line_file(char** lineptr, size_t* n, FILE* fp) {
} }
} }
return -1; return (line_used != 0) ? line_used : -1;
} }
#endif #endif
@ -866,25 +867,25 @@ using get_multiline_t = typename get_multiline<Ts...>::type;
// string_error // string_error
//////////////// ////////////////
class string_error; class string_error {};
//////////////// ////////////////
// ignore_header // ignore_header
//////////////// ////////////////
class ignore_header; class ignore_header {};
//////////////// ////////////////
// ignore_empty // ignore_empty
//////////////// ////////////////
class ignore_empty; class ignore_empty {};
//////////////// ////////////////
// throw_on_error // throw_on_error
//////////////// ////////////////
class throw_on_error; class throw_on_error {};
//////////////// ////////////////
// setup implementation // setup implementation
@ -2238,6 +2239,10 @@ public:
: reader_.line_number_; : reader_.line_number_;
} }
size_t position() const {
return reader_.chars_read_;
}
template <typename T, typename... Ts> template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() { no_void_validator_tup_t<T, Ts...> get_next() {
std::optional<std::string> error; std::optional<std::string> error;
@ -2826,6 +2831,7 @@ private:
csv_data_size_{other.csv_data_size_}, csv_data_size_{other.csv_data_size_},
curr_char_{other.curr_char_}, crlf_{other.crlf_}, curr_char_{other.curr_char_}, crlf_{other.crlf_},
line_number_{other.line_number_}, line_number_{other.line_number_},
chars_read_{other.chars_read_},
next_line_size_{other.next_line_size_} { next_line_size_{other.next_line_size_} {
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
@ -2850,12 +2856,14 @@ private:
curr_char_ = other.curr_char_; curr_char_ = other.curr_char_;
crlf_ = other.crlf_; crlf_ = other.crlf_;
line_number_ = other.line_number_; line_number_ = other.line_number_;
chars_read_ = other.chars_read_;
next_line_size_ = other.next_line_size_; next_line_size_ = other.next_line_size_;
other.buffer_ = nullptr; other.buffer_ = nullptr;
other.next_line_buffer_ = nullptr; other.next_line_buffer_ = nullptr;
other.helper_buffer_ = nullptr; other.helper_buffer_ = nullptr;
other.file_ = nullptr; other.file_ = nullptr;
other.csv_data_buffer_ = nullptr;
} }
return *this; return *this;
@ -2876,50 +2884,52 @@ private:
reader& operator=(const reader& other) = delete; reader& operator=(const reader& other) = delete;
ssize_t get_line_buffer(char** lineptr, size_t* n, ssize_t get_line_buffer(char** lineptr, size_t* n,
const char* const buffer, size_t csv_data_size, const char* const csv_data_buffer,
size_t& curr_char) { size_t csv_data_size, size_t& curr_char) {
size_t pos; if (lineptr == nullptr || n == nullptr ||
int c; csv_data_buffer == nullptr) {
errno = EINVAL;
return -1;
}
if (curr_char >= csv_data_size) { if (curr_char >= csv_data_size) {
return -1; return -1;
} }
c = buffer[curr_char++];
if (*lineptr == nullptr) { if (*lineptr == nullptr || *n < get_line_initial_buffer_size) {
*lineptr = auto new_lineptr = static_cast<char*>(
static_cast<char*>(malloc(get_line_initial_buffer_size)); realloc(*lineptr, get_line_initial_buffer_size));
if (*lineptr == nullptr) { if (new_lineptr == nullptr) {
return -1; return -1;
} }
*n = 128; *lineptr = new_lineptr;
*n = get_line_initial_buffer_size;
} }
pos = 0; size_t line_used = 0;
while (curr_char <= csv_data_size) { while (curr_char <= csv_data_size) {
if (pos + 1 >= *n) { if (line_used + 1 >= *n) {
size_t new_size = *n + (*n >> 2); size_t new_n = *n * 2;
if (new_size < get_line_initial_buffer_size) {
new_size = get_line_initial_buffer_size; char* new_lineptr =
} static_cast<char*>(realloc(*lineptr, new_n));
char* new_ptr = static_cast<char*>( if (new_lineptr == nullptr) {
realloc(static_cast<void*>(*lineptr), new_size)); errno = ENOMEM;
if (new_ptr == nullptr) {
return -1; return -1;
} }
*n = new_size; *n = new_n;
*lineptr = new_ptr; *lineptr = new_lineptr;
} }
(*lineptr)[pos++] = c; auto c = csv_data_buffer[curr_char++];
(*lineptr)[line_used++] = c;
if (c == '\n') { if (c == '\n') {
break; (*lineptr)[line_used] = '\0';
return line_used;
} }
c = buffer[curr_char++];
} }
(*lineptr)[pos] = '\0'; return (line_used != 0) ? line_used : -1;
return pos;
} }
// read next line each time in order to set eof_ // read next line each time in order to set eof_
@ -2933,9 +2943,11 @@ private:
next_line_buffer_[0] = '\0'; next_line_buffer_[0] = '\0';
} }
chars_read_ = curr_char_;
if (file_) { if (file_) {
ssize = get_line_file(&next_line_buffer_, ssize = get_line_file(&next_line_buffer_,
&next_line_buffer_size_, file_); &next_line_buffer_size_, file_);
curr_char_ = ftell(file_);
} else { } else {
ssize = get_line_buffer(&next_line_buffer_, ssize = get_line_buffer(&next_line_buffer_,
&next_line_buffer_size_, &next_line_buffer_size_,
@ -3139,6 +3151,7 @@ private:
bool crlf_{false}; bool crlf_{false};
size_t line_number_{0}; size_t line_number_{0};
size_t chars_read_{0};
size_t next_line_size_{0}; size_t next_line_size_{0};
}; };

View File

@ -1,12 +1,14 @@
#pragma once #pragma once
#include <algorithm>
#include <ctime> #include <ctime>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iomanip> #include <iomanip>
#include <ss/common.hpp>
#include <ss/setup.hpp>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm>
#include <fstream>
#ifdef CMAKE_GITHUB_CI #ifdef CMAKE_GITHUB_CI
#include <doctest/doctest.h> #include <doctest/doctest.h>
@ -20,6 +22,24 @@ class parser;
} /* ss */ } /* ss */
namespace { namespace {
struct bool_error {};
template <typename T, typename U = bool_error>
struct config {
using BufferMode = T;
using ErrorMode = U;
constexpr static auto ThrowOnError = std::is_same_v<U, ss::throw_on_error>;
constexpr static auto StringError = std::is_same_v<U, ss::string_error>;
};
#define ParserOptionCombinations \
config<std::true_type>, config<std::true_type, ss::string_error>, \
config<std::true_type, ss::throw_on_error>, config<std::false_type>, \
config<std::false_type, ss::string_error>, \
config<std::false_type, ss::throw_on_error>
struct buffer { struct buffer {
std::string data_; std::string data_;
@ -172,23 +192,32 @@ template <typename T>
} }
template <bool buffer_mode, typename... Ts> template <bool buffer_mode, typename... Ts>
[[maybe_unused]] std::tuple<ss::parser<Ts...>, std::string> make_parser( std::tuple<ss::parser<Ts...>, std::string> make_parser_impl(
const std::string& file_name, const std::string& delim = "") { const std::string& file_name, std::string delim = ss::default_delimiter) {
if (buffer_mode) { if (buffer_mode) {
auto buffer = make_buffer(file_name); auto buffer = make_buffer(file_name);
if (delim.empty()) {
return {ss::parser<Ts...>{buffer.data(), buffer.size()},
std::move(buffer)};
} else {
return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim}, return {ss::parser<Ts...>{buffer.data(), buffer.size(), delim},
std::move(buffer)}; std::move(buffer)};
}
} else {
if (delim.empty()) {
return {ss::parser<Ts...>{file_name}, std::string{}};
} else { } else {
return {ss::parser<Ts...>{file_name, delim}, std::string{}}; return {ss::parser<Ts...>{file_name, delim}, std::string{}};
} }
}
} }
template <bool buffer_mode, typename ErrorMode, typename... Ts>
[[maybe_unused]] std::enable_if_t<
!std::is_same_v<ErrorMode, bool_error>,
std::tuple<ss::parser<ErrorMode, Ts...>, std::string>>
make_parser(const std::string& file_name,
std::string delim = ss::default_delimiter) {
return make_parser_impl<buffer_mode, ErrorMode, Ts...>(file_name, delim);
}
template <bool buffer_mode, typename ErrorMode, typename... Ts>
[[maybe_unused]] std::enable_if_t<std::is_same_v<ErrorMode, bool_error>,
std::tuple<ss::parser<Ts...>, std::string>>
make_parser(const std::string& file_name,
std::string delim = ss::default_delimiter) {
return make_parser_impl<buffer_mode, Ts...>(file_name, delim);
}
} /* namespace */ } /* namespace */

View File

@ -1,7 +1,7 @@
#include "test_parser1.hpp" #include "test_parser1.hpp"
TEST_CASE("test file not found") { TEST_CASE("test file not found") {
unique_file_name f{"test_parser"}; unique_file_name f{"file_not_found"};
{ {
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
@ -11,6 +11,7 @@ TEST_CASE("test file not found") {
{ {
ss::parser<ss::string_error> p{f.name, ","}; ss::parser<ss::string_error> p{f.name, ","};
CHECK_FALSE(p.valid()); CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
} }
try { try {
@ -30,6 +31,7 @@ TEST_CASE("test null buffer") {
{ {
ss::parser<ss::string_error> p{nullptr, 10, ","}; ss::parser<ss::string_error> p{nullptr, 10, ","};
CHECK_FALSE(p.valid()); CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
} }
try { try {
@ -40,20 +42,105 @@ TEST_CASE("test null buffer") {
} }
} }
template <bool buffer_mode, typename... Ts> struct Y {
void test_various_cases() { constexpr static auto delim = ",";
std::string s1;
std::string s2;
std::string s3;
std::string to_string() const {
return std::string{}
.append(s1)
.append(delim)
.append(s2)
.append(delim)
.append(s3);
}
auto tied() const {
return std::tie(s1, s2, s3);
}
};
TEST_CASE_TEMPLATE("test position method", T, ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"position_method"};
std::vector<Y> data = {{"1", "21", "x"}, {"321", "4", "y"},
{"54", "6", "zz"}, {"7", "876", "uuuu"},
{"910", "10", "v"}, {"10", "321", "ww"}};
make_and_write(f.name, data);
auto [p, buff] = make_parser<buffer_mode, ErrorMode>(f.name);
auto data_at = [&buff = buff, &f = f](auto n) {
if (!buff.empty()) {
return buff[n];
} else {
auto file = fopen(f.name.c_str(), "r");
fseek(file, n, SEEK_SET);
return static_cast<char>(fgetc(file));
}
};
while (!p.eof()) {
auto curr_char = p.position();
const auto& [s1, s2, s3] =
p.template get_next<std::string, std::string, std::string>();
auto s = s1 + "," + s2 + "," + s3;
for (size_t i = 0; i < s1.size(); ++i) {
CHECK_EQ(data_at(curr_char + i), s[i]);
}
auto last_char = data_at(curr_char + s.size());
CHECK((last_char == '\n' || last_char == '\r'));
}
}
// TODO uncomment
/*
TEST_CASE_TEMPLATE("test line method", BufferMode, std::true_type,
std::false_type) {
unique_file_name f{"test_parser"}; unique_file_name f{"test_parser"};
std::vector<Y> data = {{"1", "21", "x"}, {"321", "4", "y"},
{"54", "6", "zz"}, {"7", "876", "uuuu"},
{"910", "10", "v"}, {"10", "321", "ww"}};
make_and_write(f.name, data);
auto [p, buff] = make_parser<BufferMode::value>(f.name);
auto expected_line = 0;
CHECK_EQ(p.line(), expected_line);
while (!p.eof()) {
auto _ = p.template get_next<std::string, std::string, std::string>();
++expected_line;
CHECK_EQ(p.line(), expected_line);
}
CHECK_EQ(p.line(), data.size());
}
*/
TEST_CASE_TEMPLATE("parser test various valid cases", T,
ParserOptionCombinations) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"various_valid_cases"};
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}, std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"},
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}}; {7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
make_and_write(f.name, data); make_and_write(f.name, data);
auto csv_data_buffer = make_buffer(f.name); auto csv_data_buffer = make_buffer(f.name);
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
ss::parser p0{std::move(p)}; ss::parser p0{std::move(p)};
p = std::move(p0); p = std::move(p0);
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
auto move_rotate = [&p = p, &p0 = p0] { auto move_rotate = [&p = p, &p0 = p0] {
@ -77,13 +164,13 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
auto [p3, ___] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p3, ___] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i3; std::vector<X> i3;
std::vector<X> expected = {std::begin(data) + 1, std::end(data)}; std::vector<X> expected = {std::begin(data) + 1, std::end(data)};
@ -112,9 +199,9 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -131,7 +218,7 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (auto&& a : for (auto&& a :
@ -143,10 +230,10 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
using tup = std::tuple<int, double, std::string>; using tup = std::tuple<int, double, std::string>;
@ -164,7 +251,7 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
using tup = std::tuple<int, double, std::string>; using tup = std::tuple<int, double, std::string>;
@ -176,7 +263,7 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -187,7 +274,7 @@ void test_various_cases() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (auto&& a : p.template iterate<X>()) { for (auto&& a : p.template iterate<X>()) {
@ -199,10 +286,10 @@ void test_various_cases() {
{ {
constexpr int excluded = 3; constexpr int excluded = 3;
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -217,7 +304,7 @@ void test_various_cases() {
}; };
} }
if (!ss::setup<Ts...>::throw_on_error) { if (!T::ThrowOnError) {
for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>, for (auto&& a : p2.template iterate_object<X, ss::ax<int, excluded>,
double, std::string>()) { double, std::string>()) {
if (p2.valid()) { if (p2.valid()) {
@ -237,16 +324,16 @@ void test_various_cases() {
[&](const X& x) { return x.i != excluded; }); [&](const X& x) { return x.i != excluded; });
CHECK_EQ(i, expected); CHECK_EQ(i, expected);
if (!ss::setup<Ts...>::throw_on_error) { if (!T::ThrowOnError) {
CHECK_EQ(i2, expected); CHECK_EQ(i2, expected);
} }
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -261,7 +348,7 @@ void test_various_cases() {
} }
} }
if (!ss::setup<Ts...>::throw_on_error) { if (!T::ThrowOnError) {
for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>, for (auto&& a : p2.template iterate_object<X, ss::nx<int, 3>,
double, std::string>()) { double, std::string>()) {
if (p2.valid()) { if (p2.valid()) {
@ -272,21 +359,21 @@ void test_various_cases() {
std::vector<X> expected = {{3, 4, "y"}}; std::vector<X> expected = {{3, 4, "y"}};
CHECK_EQ(i, expected); CHECK_EQ(i, expected);
if (!ss::setup<Ts...>::throw_on_error) { if (!T::ThrowOnError) {
CHECK_EQ(i2, expected); CHECK_EQ(i2, expected);
} }
} }
{ {
unique_file_name empty_f{"test_parser"}; unique_file_name empty_f{"various_valid_cases"};
std::vector<X> empty_data = {}; std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data); make_and_write(empty_f.name, empty_data);
auto [p, _] = make_parser<buffer_mode, Ts...>(empty_f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ",");
std::vector<X> i; std::vector<X> i;
auto [p2, __] = make_parser<buffer_mode, Ts...>(empty_f.name, ","); auto [p2, __] = make_parser<buffer_mode, ErrorMode>(empty_f.name, ",");
std::vector<X> i2; std::vector<X> i2;
while (!p.eof()) { while (!p.eof()) {
@ -302,15 +389,6 @@ void test_various_cases() {
} }
} }
TEST_CASE("parser test various cases") {
test_various_cases<false>();
test_various_cases<false, ss::string_error>();
test_various_cases<false, ss::throw_on_error>();
test_various_cases<true>();
test_various_cases<true, ss::string_error>();
test_various_cases<true, ss::throw_on_error>();
}
using test_tuple = std::tuple<double, char, double>; using test_tuple = std::tuple<double, char, double>;
struct test_struct { struct test_struct {
int i; int i;
@ -324,9 +402,10 @@ struct test_struct {
static inline void expect_test_struct(const test_struct&) { static inline void expect_test_struct(const test_struct&) {
} }
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("parser test composite conversion", BufferMode,
void test_composite_conversion() { std::true_type, std::false_type) {
unique_file_name f{"test_parser"}; constexpr auto buffer_mode = BufferMode::value;
unique_file_name f{"composite_conversion"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
for (auto& i : for (auto& i :
@ -336,7 +415,7 @@ void test_composite_conversion() {
} }
} }
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ss::string_error>(f.name, ",");
auto fail = [] { FAIL(""); }; auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); };
auto ignore_error = [] {}; auto ignore_error = [] {};
@ -546,18 +625,12 @@ void test_composite_conversion() {
CHECK(p.eof()); CHECK(p.eof());
} }
// various scenarios template <bool buffer_mode, typename... Ts>
TEST_CASE("parser test composite conversion") {
test_composite_conversion<false, ss::string_error>();
test_composite_conversion<true, ss::string_error>();
}
template <bool buffer_mode>
void test_no_new_line_at_eof_impl(const std::vector<X>& data) { void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
unique_file_name f{"test_parser"}; unique_file_name f{"no_new_line_at_eof"};
make_and_write(f.name, data, {}, false); make_and_write(f.name, data, {}, false);
auto [p, _] = make_parser<buffer_mode>(f.name); auto [p, _] = make_parser<buffer_mode, Ts...>(f.name);
std::vector<X> parsed_data; std::vector<X> parsed_data;
for (const auto& el : p.template iterate<X>()) { for (const auto& el : p.template iterate<X>()) {
@ -567,32 +640,36 @@ void test_no_new_line_at_eof_impl(const std::vector<X>& data) {
CHECK_EQ(data, parsed_data); CHECK_EQ(data, parsed_data);
} }
template <bool buffer_mode> template <bool buffer_mode, typename... Ts>
void test_no_new_line_at_eof() { void test_no_new_line_at_eof() {
test_no_new_line_at_eof_impl<buffer_mode>({}); test_no_new_line_at_eof_impl<buffer_mode, Ts...>({});
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}}); test_no_new_line_at_eof_impl<buffer_mode, Ts...>({{1, 2, "X"}});
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {}}); test_no_new_line_at_eof_impl<buffer_mode, Ts...>({{1, 2, "X"}, {}});
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {3, 4, "YY"}}); test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
test_no_new_line_at_eof_impl<buffer_mode>({{1, 2, "X"}, {3, 4, "YY"}, {}}); {{1, 2, "X"}, {3, 4, "YY"}});
test_no_new_line_at_eof_impl<buffer_mode>( test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, "X"}, {3, 4, "YY"}, {}});
test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, "X"}, {3, 4, "YY"}, {5, 6, "ZZZ"}, {7, 8, "UUU"}}); {{1, 2, "X"}, {3, 4, "YY"}, {5, 6, "ZZZ"}, {7, 8, "UUU"}});
for (size_t i = 0; i < 2 * ss::get_line_initial_buffer_size; ++i) { for (size_t i = 0; i < 2 * ss::get_line_initial_buffer_size; ++i) {
test_no_new_line_at_eof_impl<buffer_mode>( test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(i, 'X')}}); {{1, 2, std::string(i, 'X')}});
for (size_t j = 0; j < 2 * ss::get_line_initial_buffer_size; j += 13) { for (size_t j = 0; j < 2 * ss::get_line_initial_buffer_size; j += 13) {
test_no_new_line_at_eof_impl<buffer_mode>( test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}}); {{1, 2, std::string(i, 'X')}, {3, 4, std::string(j, 'Y')}});
test_no_new_line_at_eof_impl<buffer_mode>( test_no_new_line_at_eof_impl<buffer_mode, Ts...>(
{{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}}); {{1, 2, std::string(j, 'X')}, {3, 4, std::string(i, 'Y')}});
} }
} }
} }
TEST_CASE("test no new line at end of data") { TEST_CASE_TEMPLATE("test no new line at end of data", T,
test_no_new_line_at_eof<false>(); ParserOptionCombinations) {
test_no_new_line_at_eof<true>(); constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
test_no_new_line_at_eof<buffer_mode, ErrorMode>();
} }

View File

@ -41,11 +41,16 @@ struct xyz {
} }
}; };
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("test moving of parsed composite values", T,
void test_moving_of_parsed_composite_values() { config<std::true_type>, config<std::false_type>,
config<std::true_type, ss::string_error>,
config<std::false_type, ss::string_error>) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
// to compile is enough // to compile is enough
return; return;
auto [p, _] = make_parser<buffer_mode, Ts...>("", ""); auto [p, _] = make_parser<buffer_mode, ErrorMode>("", "");
p.template try_next<my_string, my_string, my_string>() p.template try_next<my_string, my_string, my_string>()
.template or_else<my_string, my_string, my_string, my_string>( .template or_else<my_string, my_string, my_string, my_string>(
[](auto&&) {}) [](auto&&) {})
@ -56,71 +61,42 @@ void test_moving_of_parsed_composite_values() {
[](auto&, auto&, auto&) {}); [](auto&, auto&, auto&) {});
} }
TEST_CASE("parser test the moving of parsed composite values") { TEST_CASE_TEMPLATE("parser test string error mode", BufferMode, std::true_type,
test_moving_of_parsed_composite_values<false>(); std::false_type) {
test_moving_of_parsed_composite_values<false, ss::string_error>(); unique_file_name f{"string_error"};
test_moving_of_parsed_composite_values<true>();
test_moving_of_parsed_composite_values<true, ss::string_error>();
}
TEST_CASE("parser test error mode") {
unique_file_name f{"test_parser"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "junk" << std::endl; out << "junk" << std::endl;
out << "junk" << std::endl; out << "junk" << std::endl;
} }
{ auto [p, _] = make_parser<BufferMode::value, ss::string_error>(f.name, ",");
auto [p, _] = make_parser<false, ss::string_error>(f.name, ",");
REQUIRE_FALSE(p.eof()); REQUIRE_FALSE(p.eof());
p.get_next<int>(); p.template get_next<int>();
CHECK_FALSE(p.valid()); CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty()); CHECK_FALSE(p.error_msg().empty());
}
{
auto [p, _] = make_parser<true, ss::string_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
p.get_next<int>();
CHECK_FALSE(p.valid());
CHECK_FALSE(p.error_msg().empty());
}
} }
TEST_CASE("parser throw on error mode") { TEST_CASE_TEMPLATE("parser throw on error mode", BufferMode, std::true_type,
unique_file_name f{"test_parser"}; std::false_type) {
unique_file_name f{"throw_on_error"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "junk" << std::endl; out << "junk" << std::endl;
out << "junk" << std::endl; out << "junk" << std::endl;
} }
{ auto [p, _] =
auto [p, _] = make_parser<false, ss::throw_on_error>(f.name, ","); make_parser<BufferMode::value, ss::throw_on_error>(f.name, ",");
REQUIRE_FALSE(p.eof()); REQUIRE_FALSE(p.eof());
try { try {
p.get_next<int>(); p.template get_next<int>();
FAIL("Expected exception..."); FAIL("Expected exception...");
} catch (const std::exception& e) { } catch (const std::exception& e) {
CHECK_FALSE(std::string{e.what()}.empty()); CHECK_FALSE(std::string{e.what()}.empty());
} }
}
{
auto [p, _] = make_parser<true, ss::throw_on_error>(f.name, ",");
REQUIRE_FALSE(p.eof());
try {
p.get_next<int>();
FAIL("Expected exception...");
} catch (const std::exception& e) {
CHECK_FALSE(std::string{e.what()}.empty());
}
}
} }
static inline std::string no_quote(const std::string& s) { static inline std::string no_quote(const std::string& s) {
@ -130,9 +106,11 @@ static inline std::string no_quote(const std::string& s) {
return s; return s;
} }
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("test quote multiline", T, ParserOptionCombinations) {
void test_quote_multiline() { constexpr auto buffer_mode = T::BufferMode::value;
unique_file_name f{"test_parser"}; using ErrorMode = typename T::ErrorMode;
unique_file_name f{"quote_multiline"};
std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""}, std::vector<X> data = {{1, 2, "\"x\r\nx\nx\""},
{3, 4, "\"y\ny\r\ny\""}, {3, 4, "\"y\ny\r\ny\""},
{5, 6, "\"z\nz\""}, {5, 6, "\"z\nz\""},
@ -151,9 +129,8 @@ void test_quote_multiline() {
} }
} }
auto [p, _] = auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
make_parser<buffer_mode, ss::multiline, ss::quote<'"'>, Ts...>(f.name, ss::quote<'"'>>(f.name, ",");
",");
std::vector<X> i; std::vector<X> i;
@ -168,7 +145,7 @@ void test_quote_multiline() {
CHECK_EQ(i, data); CHECK_EQ(i, data);
auto [p_no_multiline, __] = auto [p_no_multiline, __] =
make_parser<buffer_mode, ss::quote<'"'>, Ts...>(f.name, ","); make_parser<buffer_mode, ErrorMode, ss::quote<'"'>>(f.name, ",");
while (!p.eof()) { while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] { auto command = [&p_no_multiline = p_no_multiline] {
p_no_multiline.template get_next<int, double, std::string>(); p_no_multiline.template get_next<int, double, std::string>();
@ -177,23 +154,16 @@ void test_quote_multiline() {
} }
} }
TEST_CASE("parser test csv on multiple lines with quotes") {
test_quote_multiline<false>();
test_quote_multiline<false, ss::string_error>();
test_quote_multiline<false, ss::throw_on_error>();
test_quote_multiline<true>();
test_quote_multiline<true, ss::string_error>();
test_quote_multiline<true, ss::throw_on_error>();
}
static inline std::string no_escape(std::string& s) { static inline std::string no_escape(std::string& s) {
s.erase(std::remove(begin(s), end(s), '\\'), end(s)); s.erase(std::remove(begin(s), end(s), '\\'), end(s));
return s; return s;
} }
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("test escape multiline", T, ParserOptionCombinations) {
void test_escape_multiline() { constexpr auto buffer_mode = T::BufferMode::value;
unique_file_name f{"test_parser"}; using ErrorMode = typename T::ErrorMode;
unique_file_name f{"escape_multiline"};
std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"}, std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"},
{5, 6, "z\\\nz\\\nz"}, {5, 6, "z\\\nz\\\nz"},
{7, 8, "u"}, {7, 8, "u"},
@ -212,9 +182,8 @@ void test_escape_multiline() {
} }
} }
auto [p, _] = auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>, Ts...>(f.name, ss::escape<'\\'>>(f.name, ",");
",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -228,7 +197,7 @@ void test_escape_multiline() {
CHECK_EQ(i, data); CHECK_EQ(i, data);
auto [p_no_multiline, __] = auto [p_no_multiline, __] =
make_parser<buffer_mode, ss::escape<'\\'>, Ts...>(f.name, ","); make_parser<buffer_mode, ErrorMode, ss::escape<'\\'>>(f.name, ",");
while (!p.eof()) { while (!p.eof()) {
auto command = [&p_no_multiline = p_no_multiline] { auto command = [&p_no_multiline = p_no_multiline] {
auto a = auto a =
@ -238,18 +207,11 @@ void test_escape_multiline() {
} }
} }
TEST_CASE("parser test csv on multiple lines with escapes") { TEST_CASE_TEMPLATE("test quote escape multiline", T, ParserOptionCombinations) {
test_escape_multiline<false>(); constexpr auto buffer_mode = T::BufferMode::value;
test_escape_multiline<false, ss::string_error>(); using ErrorMode = typename T::ErrorMode;
test_escape_multiline<false, ss::throw_on_error>();
test_escape_multiline<true>();
test_escape_multiline<true, ss::string_error>();
test_escape_multiline<true, ss::throw_on_error>();
}
template <bool buffer_mode, typename... Ts> unique_file_name f{"quote_escape_multiline"};
void test_quote_escape_multiline() {
unique_file_name f{"test_parser"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "1,2,\"just\\\n\nstrings\"" << std::endl; out << "1,2,\"just\\\n\nstrings\"" << std::endl;
@ -266,8 +228,8 @@ void test_quote_escape_multiline() {
size_t bad_lines = 1; size_t bad_lines = 1;
auto num_errors = 0; auto num_errors = 0;
auto [p, _] = make_parser<buffer_mode, ss::multiline, ss::escape<'\\'>, auto [p, _] = make_parser<buffer_mode, ErrorMode, ss::multiline,
ss::quote<'"'>, Ts...>(f.name); ss::escape<'\\'>, ss::quote<'"'>>(f.name);
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -298,12 +260,3 @@ void test_quote_escape_multiline() {
} }
CHECK_EQ(i, data); CHECK_EQ(i, data);
} }
TEST_CASE("parser test csv on multiple lines with quotes and escapes") {
test_quote_escape_multiline<false>();
test_quote_escape_multiline<false, ss::string_error>();
test_quote_escape_multiline<false, ss::throw_on_error>();
test_quote_escape_multiline<true>();
test_quote_escape_multiline<true, ss::string_error>();
test_quote_escape_multiline<true, ss::throw_on_error>();
}

View File

@ -1,8 +1,10 @@
#include "test_parser1.hpp" #include "test_parser1.hpp"
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("test multiline restricted", T, ParserOptionCombinations) {
void test_multiline_restricted() { constexpr auto buffer_mode = T::BufferMode::value;
unique_file_name f{"test_parser"}; using ErrorMode = typename T::ErrorMode;
unique_file_name f{"multiline_restricted"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "1,2,\"just\n\nstrings\"" << std::endl; out << "1,2,\"just\n\nstrings\"" << std::endl;
@ -24,8 +26,8 @@ void test_multiline_restricted() {
auto num_errors = 0; auto num_errors = 0;
auto [p, _] = auto [p, _] =
make_parser<buffer_mode, ss::multiline_restricted<2>, ss::quote<'"'>, make_parser<buffer_mode, ErrorMode, ss::multiline_restricted<2>,
ss::escape<'\\'>, Ts...>(f.name, ","); ss::quote<'"'>, ss::escape<'\\'>>(f.name, ",");
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
@ -63,26 +65,20 @@ void test_multiline_restricted() {
CHECK_EQ(i, data); CHECK_EQ(i, data);
} }
TEST_CASE("parser test multiline restricted") { template <typename T, typename... Ts>
test_multiline_restricted<false>(); void test_unterminated_line(const std::vector<std::string>& lines,
test_multiline_restricted<false, ss::string_error>();
test_multiline_restricted<false, ss::throw_on_error>();
test_multiline_restricted<true>();
test_multiline_restricted<true, ss::string_error>();
test_multiline_restricted<true, ss::throw_on_error>();
}
template <bool buffer_mode, typename... Ts>
void test_unterminated_line_impl(const std::vector<std::string>& lines,
size_t bad_line) { size_t bad_line) {
unique_file_name f{"test_parser"}; constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"unterminated_line"};
std::ofstream out{f.name}; std::ofstream out{f.name};
for (const auto& line : lines) { for (const auto& line : lines) {
out << line << std::endl; out << line << std::endl;
} }
out.close(); out.close();
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode, ErrorMode, Ts...>(f.name);
size_t line = 0; size_t line = 0;
while (!p.eof()) { while (!p.eof()) {
auto command = [&p = p] { auto command = [&p = p] {
@ -100,21 +96,8 @@ void test_unterminated_line_impl(const std::vector<std::string>& lines,
} }
} }
template <typename... Ts> TEST_CASE_TEMPLATE("parser test csv on multiline with errors", T,
void test_unterminated_line(const std::vector<std::string>& lines, ParserOptionCombinations) {
size_t bad_line) {
test_unterminated_line_impl<false, Ts...>(lines, bad_line);
test_unterminated_line_impl<false, Ts..., ss::string_error>(lines,
bad_line);
test_unterminated_line_impl<false, Ts..., ss::throw_on_error>(lines,
bad_line);
test_unterminated_line_impl<true, Ts...>(lines, bad_line);
test_unterminated_line_impl<true, Ts..., ss::string_error>(lines, bad_line);
test_unterminated_line_impl<true, Ts..., ss::throw_on_error>(lines,
bad_line);
}
TEST_CASE("parser test csv on multiline with errors") {
using multiline = ss::multiline_restricted<3>; using multiline = ss::multiline_restricted<3>;
using escape = ss::escape<'\\'>; using escape = ss::escape<'\\'>;
using quote = ss::quote<'"'>; using quote = ss::quote<'"'>;
@ -122,209 +105,209 @@ TEST_CASE("parser test csv on multiline with errors") {
// unterminated escape // unterminated escape
{ {
const std::vector<std::string> lines{"1,2,just\\"}; const std::vector<std::string> lines{"1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 0); test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,just\\", "9,8,second"}; const std::vector<std::string> lines{"1,2,just\\", "9,8,second"};
test_unterminated_line<multiline, escape>(lines, 0); test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,just\\"}; const std::vector<std::string> lines{"9,8,first", "1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,just\\", const std::vector<std::string> lines{"9,8,first", "1,2,just\\",
"3,4,third"}; "3,4,third"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,just\\\nstrings\\", "1,2,just\\\nstrings\\",
"3,4,th\\\nird"}; "3,4,th\\\nird"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "3,4,second", const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,just\\"}; "1,2,just\\"};
test_unterminated_line<multiline, escape>(lines, 2); test_unterminated_line<T, multiline, escape>(lines, 2);
test_unterminated_line<multiline, escape, quote>(lines, 2); test_unterminated_line<T, multiline, escape, quote>(lines, 2);
} }
{ {
const std::vector<std::string> lines{"9,8,\\first", "3,4,second", const std::vector<std::string> lines{"9,8,\\first", "3,4,second",
"1,2,jus\\t\\"}; "1,2,jus\\t\\"};
test_unterminated_line<multiline, escape>(lines, 2); test_unterminated_line<T, multiline, escape>(lines, 2);
test_unterminated_line<multiline, escape, quote>(lines, 2); test_unterminated_line<T, multiline, escape, quote>(lines, 2);
} }
// unterminated quote // unterminated quote
{ {
const std::vector<std::string> lines{"1,2,\"just"}; const std::vector<std::string> lines{"1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0); test_unterminated_line<T, multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just", "9,8,second"}; const std::vector<std::string> lines{"1,2,\"just", "9,8,second"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0); test_unterminated_line<T, multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just"}; const std::vector<std::string> lines{"9,8,first", "1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1); test_unterminated_line<T, multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just", const std::vector<std::string> lines{"9,8,first", "1,2,\"just",
"3,4,th\\,ird"}; "3,4,th\\,ird"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1); test_unterminated_line<T, multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "3,4,second", const std::vector<std::string> lines{"9,8,first", "3,4,second",
"1,2,\"just"}; "1,2,\"just"};
test_unterminated_line<multiline, escape, quote>(lines, 2); test_unterminated_line<T, multiline, escape, quote>(lines, 2);
test_unterminated_line<multiline, quote>(lines, 2); test_unterminated_line<T, multiline, quote>(lines, 2);
} }
{ {
const std::vector<std::string> lines{"9,8,\"first\"", const std::vector<std::string> lines{"9,8,\"first\"",
"\"3\",4,\"sec,ond\"", "\"3\",4,\"sec,ond\"",
"1,2,\"ju\"\"st"}; "1,2,\"ju\"\"st"};
test_unterminated_line<multiline, escape, quote>(lines, 2); test_unterminated_line<T, multiline, escape, quote>(lines, 2);
test_unterminated_line<multiline, quote>(lines, 2); test_unterminated_line<T, multiline, quote>(lines, 2);
} }
// unterminated quote and escape // unterminated quote and escape
{ {
const std::vector<std::string> lines{"1,2,\"just\\"}; const std::vector<std::string> lines{"1,2,\"just\\"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just\\\n\\"}; const std::vector<std::string> lines{"1,2,\"just\\\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"1,2,\"just\n\\"}; const std::vector<std::string> lines{"1,2,\"just\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"}; const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\", const std::vector<std::string> lines{"9,8,first", "1,2,\"just\n\\",
"4,3,thrid"}; "4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\", const std::vector<std::string> lines{"9,8,f\\\nirst", "1,2,\"just\n\\",
"4,3,thrid"}; "4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"", const std::vector<std::string> lines{"9,8,\"f\ni\nrst\"",
"1,2,\"just\n\\", "4,3,thrid"}; "1,2,\"just\n\\", "4,3,thrid"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
// multiline limmit reached escape // multiline limmit reached escape
{ {
const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"}; const std::vector<std::string> lines{"1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 0); test_unterminated_line<T, multiline, escape>(lines, 0);
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust"}; "1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt", const std::vector<std::string> lines{"9,8,fi\\\nrs\\\nt",
"1,2,\\\n\\\n\\\n\\\njust"}; "1,2,\\\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\\\n\\\n\\\n\\\njust", "1,2,\\\n\\\n\\\n\\\njust",
"4,3,third"}; "4,3,third"};
test_unterminated_line<multiline, escape>(lines, 1); test_unterminated_line<T, multiline, escape>(lines, 1);
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
// multiline limmit reached quote // multiline limmit reached quote
{ {
const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""}; const std::vector<std::string> lines{"1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
test_unterminated_line<multiline, quote>(lines, 0); test_unterminated_line<T, multiline, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\"\n\n\n\n\njust\""}; "1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1); test_unterminated_line<T, multiline, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fir\nst\"", const std::vector<std::string> lines{"9,8,\"fir\nst\"",
"1,2,\"\n\n\n\n\njust\""}; "1,2,\"\n\n\n\n\njust\""};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
test_unterminated_line<multiline, quote>(lines, 1); test_unterminated_line<T, multiline, quote>(lines, 1);
} }
// multiline limmit reached quote and escape // multiline limmit reached quote and escape
{ {
const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"}; const std::vector<std::string> lines{"1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 0); test_unterminated_line<T, multiline, escape, quote>(lines, 0);
} }
{ {
const std::vector<std::string> lines{"9,8,first", const std::vector<std::string> lines{"9,8,first",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,fi\\\nrst", const std::vector<std::string> lines{"9,8,fi\\\nrst",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fi\nrst\"", const std::vector<std::string> lines{"9,8,\"fi\nrst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
{ {
const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"", const std::vector<std::string> lines{"9,8,\"fi\nr\\\nst\"",
"1,2,\"\\\n\n\\\n\\\n\\\njust"}; "1,2,\"\\\n\n\\\n\\\n\\\njust"};
test_unterminated_line<multiline, escape, quote>(lines, 1); test_unterminated_line<T, multiline, escape, quote>(lines, 1);
} }
} }

View File

@ -7,13 +7,14 @@ template <typename T, typename... Us>
struct has_type<T, std::tuple<Us...>> struct has_type<T, std::tuple<Us...>>
: std::disjunction<std::is_same<T, Us>...> {}; : std::disjunction<std::is_same<T, Us>...> {};
template <bool buffer_mode, typename Setup, typename... Ts> template <typename T, typename... Ts>
static void test_fields_impl(const std::string file_name, static void test_fields(const std::string file_name, const std::vector<X>& data,
const std::vector<X>& data,
const std::vector<std::string>& fields) { const std::vector<std::string>& fields) {
constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
using CaseType = std::tuple<Ts...>; using CaseType = std::tuple<Ts...>;
auto [p, _] = make_parser<buffer_mode, Setup>(file_name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(file_name, ",");
CHECK_FALSE(p.field_exists("Unknown")); CHECK_FALSE(p.field_exists("Unknown"));
p.use_fields(fields); p.use_fields(fields);
std::vector<CaseType> i; std::vector<CaseType> i;
@ -36,23 +37,9 @@ static void test_fields_impl(const std::string file_name,
} }
} }
template <typename... Ts> TEST_CASE_TEMPLATE("test various cases with header", T,
static void test_fields(const std::string file_name, const std::vector<X>& data, ParserOptionCombinations) {
const std::vector<std::string>& fields) { unique_file_name f{"various_cases_with_header"};
test_fields_impl<false, ss::setup<>, Ts...>(file_name, data, fields);
test_fields_impl<false, ss::setup<ss::string_error>, Ts...>(file_name, data,
fields);
test_fields_impl<false, ss::setup<ss::throw_on_error>, Ts...>(file_name,
data, fields);
test_fields_impl<true, ss::setup<>, Ts...>(file_name, data, fields);
test_fields_impl<true, ss::setup<ss::string_error>, Ts...>(file_name, data,
fields);
test_fields_impl<true, ss::setup<ss::throw_on_error>, Ts...>(file_name,
data, fields);
}
TEST_CASE("parser test various cases with header") {
unique_file_name f{"test_parser"};
constexpr static auto Int = "Int"; constexpr static auto Int = "Int";
constexpr static auto Dbl = "Double"; constexpr static auto Dbl = "Double";
constexpr static auto Str = "String"; constexpr static auto Str = "String";
@ -180,27 +167,30 @@ TEST_CASE("parser test various cases with header") {
print(call) print(call)
*/ */
test_fields<str>(o, d, {Str}); test_fields<T, str>(o, d, {Str});
test_fields<int>(o, d, {Int}); test_fields<T, int>(o, d, {Int});
test_fields<double>(o, d, {Dbl}); test_fields<T, double>(o, d, {Dbl});
test_fields<str, int>(o, d, {Str, Int}); test_fields<T, str, int>(o, d, {Str, Int});
test_fields<str, double>(o, d, {Str, Dbl}); test_fields<T, str, double>(o, d, {Str, Dbl});
test_fields<int, str>(o, d, {Int, Str}); test_fields<T, int, str>(o, d, {Int, Str});
test_fields<int, double>(o, d, {Int, Dbl}); test_fields<T, int, double>(o, d, {Int, Dbl});
test_fields<double, str>(o, d, {Dbl, Str}); test_fields<T, double, str>(o, d, {Dbl, Str});
test_fields<double, int>(o, d, {Dbl, Int}); test_fields<T, double, int>(o, d, {Dbl, Int});
test_fields<str, int, double>(o, d, {Str, Int, Dbl}); test_fields<T, str, int, double>(o, d, {Str, Int, Dbl});
test_fields<str, double, int>(o, d, {Str, Dbl, Int}); test_fields<T, str, double, int>(o, d, {Str, Dbl, Int});
test_fields<int, str, double>(o, d, {Int, Str, Dbl}); test_fields<T, int, str, double>(o, d, {Int, Str, Dbl});
test_fields<int, double, str>(o, d, {Int, Dbl, Str}); test_fields<T, int, double, str>(o, d, {Int, Dbl, Str});
test_fields<double, str, int>(o, d, {Dbl, Str, Int}); test_fields<T, double, str, int>(o, d, {Dbl, Str, Int});
test_fields<double, int, str>(o, d, {Dbl, Int, Str}); test_fields<T, double, int, str>(o, d, {Dbl, Int, Str});
} }
template <bool buffer_mode, typename... Ts> template <typename T>
void test_invalid_fields_impl(const std::vector<std::string>& lines, void test_invalid_fields(const std::vector<std::string>& lines,
const std::vector<std::string>& fields) { const std::vector<std::string>& fields) {
unique_file_name f{"test_parser"}; constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"invalid_fields"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
for (const auto& line : lines) { for (const auto& line : lines) {
@ -210,21 +200,21 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
{ {
// No fields specified // No fields specified
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
auto command = [&p = p] { p.use_fields(); }; auto command = [&p = p] { p.use_fields(); };
expect_error_on_command(p, command); expect_error_on_command(p, command);
} }
{ {
// Unknown field // Unknown field
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
auto command = [&p = p] { p.use_fields("Unknown"); }; auto command = [&p = p] { p.use_fields("Unknown"); };
expect_error_on_command(p, command); expect_error_on_command(p, command);
} }
{ {
// Field used multiple times // Field used multiple times
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
auto command = [&p = p, &fields = fields] { auto command = [&p = p, &fields = fields] {
p.use_fields(fields.at(0), fields.at(0)); p.use_fields(fields.at(0), fields.at(0));
}; };
@ -235,7 +225,7 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
{ {
// Mapping out of range // Mapping out of range
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
auto command = [&p = p, &fields = fields] { auto command = [&p = p, &fields = fields] {
p.use_fields(fields.at(0)); p.use_fields(fields.at(0));
p.template get_next<std::string, std::string>(); p.template get_next<std::string, std::string>();
@ -247,7 +237,7 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
{ {
// Invalid header // Invalid header
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
auto command = [&p = p, &fields = fields] { p.use_fields(fields); }; auto command = [&p = p, &fields = fields] { p.use_fields(fields); };
if (!fields.empty()) { if (!fields.empty()) {
@ -259,7 +249,7 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
command(); command();
CHECK(p.valid()); CHECK(p.valid());
if (!p.valid()) { if (!p.valid()) {
if constexpr (ss::setup<Ts...>::string_error) { if constexpr (T::StringError) {
std::cout << p.error_msg() << std::endl; std::cout << p.error_msg() << std::endl;
} }
} }
@ -268,44 +258,38 @@ void test_invalid_fields_impl(const std::vector<std::string>& lines,
} }
} }
template <typename... Ts> TEST_CASE_TEMPLATE("test invalid fheader fields usage", T,
void test_invalid_fields(const std::vector<std::string>& lines, ParserOptionCombinations) {
const std::vector<std::string>& fields) { test_invalid_fields<T>({}, {});
test_invalid_fields_impl<false>(lines, fields);
test_invalid_fields_impl<false, ss::string_error>(lines, fields);
test_invalid_fields_impl<false, ss::throw_on_error>(lines, fields);
test_invalid_fields_impl<true>(lines, fields);
test_invalid_fields_impl<true, ss::string_error>(lines, fields);
test_invalid_fields_impl<true, ss::throw_on_error>(lines, fields);
}
TEST_CASE("parser test invalid header fields usage") { test_invalid_fields<T>({"Int"}, {"Int"});
test_invalid_fields({}, {}); test_invalid_fields<T>({"Int", "1"}, {"Int"});
test_invalid_fields<T>({"Int", "1", "2"}, {"Int"});
test_invalid_fields({"Int"}, {"Int"}); test_invalid_fields<T>({"Int,String"}, {"Int", "String"});
test_invalid_fields({"Int", "1"}, {"Int"}); test_invalid_fields<T>({"Int,String", "1,hi"}, {"Int", "String"});
test_invalid_fields({"Int", "1", "2"}, {"Int"}); test_invalid_fields<T>({"Int,String", "2,hello"}, {"Int", "String"});
test_invalid_fields({"Int,String"}, {"Int", "String"}); test_invalid_fields<T>({"Int,String,Double"}, {"Int", "String", "Double"});
test_invalid_fields({"Int,String", "1,hi"}, {"Int", "String"}); test_invalid_fields<T>({"Int,String,Double", "1,hi,2.34"},
test_invalid_fields({"Int,String", "2,hello"}, {"Int", "String"});
test_invalid_fields({"Int,String,Double"}, {"Int", "String", "Double"});
test_invalid_fields({"Int,String,Double", "1,hi,2.34"},
{"Int", "String", "Double"}); {"Int", "String", "Double"});
test_invalid_fields({"Int,String,Double", "1,hi,2.34", "2,hello,3.45"}, test_invalid_fields<T>({"Int,String,Double", "1,hi,2.34", "2,hello,3.45"},
{"Int", "String", "Double"}); {"Int", "String", "Double"});
test_invalid_fields({"Int,Int,Int"}, {"Int", "Int", "Int"}); test_invalid_fields<T>({"Int,Int,Int"}, {"Int", "Int", "Int"});
test_invalid_fields({"Int,Int,Int", "1,2,3"}, {"Int", "Int", "Int"}); test_invalid_fields<T>({"Int,Int,Int", "1,2,3"}, {"Int", "Int", "Int"});
test_invalid_fields({"Int,String,Int"}, {"Int", "String", "Int"}); test_invalid_fields<T>({"Int,String,Int"}, {"Int", "String", "Int"});
test_invalid_fields({"Int,String,Int", "1,hi,3"}, {"Int", "String", "Int"}); test_invalid_fields<T>({"Int,String,Int", "1,hi,3"},
{"Int", "String", "Int"});
} }
template <bool buffer_mode, typename... Ts> TEST_CASE_TEMPLATE("test invalid rows with header", T,
void test_invalid_rows_with_header() { ParserOptionCombinations) {
unique_file_name f{"test_parser"}; constexpr auto buffer_mode = T::BufferMode::value;
using ErrorMode = typename T::ErrorMode;
unique_file_name f{"invalid rows with header"};
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "Int,String,Double" << std::endl; out << "Int,String,Double" << std::endl;
@ -318,7 +302,7 @@ void test_invalid_rows_with_header() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
p.use_fields("Int", "String", "Double"); p.use_fields("Int", "String", "Double");
using data = std::tuple<int, std::string, double>; using data = std::tuple<int, std::string, double>;
@ -344,7 +328,7 @@ void test_invalid_rows_with_header() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
p.use_fields("Double", "Int"); p.use_fields("Double", "Int");
using data = std::tuple<double, int>; using data = std::tuple<double, int>;
@ -368,7 +352,7 @@ void test_invalid_rows_with_header() {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name);
p.use_fields("String", "Double"); p.use_fields("String", "Double");
using data = std::tuple<std::string, double>; using data = std::tuple<std::string, double>;
@ -395,18 +379,12 @@ void test_invalid_rows_with_header() {
} }
} }
TEST_CASE("parser test invalid rows with header") { template <typename T>
test_invalid_rows_with_header<false>(); void test_ignore_empty(const std::vector<X>& data) {
test_invalid_rows_with_header<false, ss::string_error>(); constexpr auto buffer_mode = T::BufferMode::value;
test_invalid_rows_with_header<false, ss::throw_on_error>(); using ErrorMode = typename T::ErrorMode;
test_invalid_rows_with_header<true>();
test_invalid_rows_with_header<true, ss::string_error>();
test_invalid_rows_with_header<true, ss::throw_on_error>();
}
template <bool buffer_mode, typename... Ts> unique_file_name f{"ignore_empty"};
void test_ignore_empty_impl(const std::vector<X>& data) {
unique_file_name f{"test_parser"};
make_and_write(f.name, data); make_and_write(f.name, data);
std::vector<X> expected; std::vector<X> expected;
@ -418,7 +396,7 @@ void test_ignore_empty_impl(const std::vector<X>& data) {
{ {
auto [p, _] = auto [p, _] =
make_parser<buffer_mode, ss::ignore_empty, Ts...>(f.name, ","); make_parser<buffer_mode, ErrorMode, ss::ignore_empty>(f.name, ",");
std::vector<X> i; std::vector<X> i;
for (const auto& a : p.template iterate<X>()) { for (const auto& a : p.template iterate<X>()) {
@ -429,7 +407,7 @@ void test_ignore_empty_impl(const std::vector<X>& data) {
} }
{ {
auto [p, _] = make_parser<buffer_mode, Ts...>(f.name, ","); auto [p, _] = make_parser<buffer_mode, ErrorMode>(f.name, ",");
std::vector<X> i; std::vector<X> i;
size_t n = 0; size_t n = 0;
while (!p.eof()) { while (!p.eof()) {
@ -450,52 +428,44 @@ void test_ignore_empty_impl(const std::vector<X>& data) {
} }
} }
template <typename... Ts> TEST_CASE_TEMPLATE("test various cases with empty lines", T,
void test_ignore_empty(const std::vector<X>& data) { ParserOptionCombinations) {
test_ignore_empty_impl<false>(data); test_ignore_empty<T>(
test_ignore_empty_impl<false, ss::string_error>(data); {{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty_impl<false, ss::throw_on_error>(data);
test_ignore_empty_impl<true>(data);
test_ignore_empty_impl<true, ss::string_error>(data);
test_ignore_empty_impl<true, ss::throw_on_error>(data);
}
TEST_CASE("parser test various cases with empty lines") { test_ignore_empty<T>(
test_ignore_empty({{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, "x"}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}}); {{1, 2, X::empty}, {5, 6, X::empty}, {9, 10, "v"}, {11, 12, "w"}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, "v"}, {11, 12, X::empty}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, X::empty}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, X::empty}, {3, 4, "y"}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty({{1, 2, X::empty}, test_ignore_empty<T>({{1, 2, X::empty},
{3, 4, X::empty}, {3, 4, X::empty},
{9, 10, X::empty}, {9, 10, X::empty},
{11, 12, X::empty}}); {11, 12, X::empty}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, "x"}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, X::empty}}); {{1, 2, "x"}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, X::empty}});
test_ignore_empty( test_ignore_empty<T>(
{{1, 2, X::empty}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, "w"}}); {{1, 2, X::empty}, {3, 4, X::empty}, {9, 10, X::empty}, {11, 12, "w"}});
test_ignore_empty({{11, 12, X::empty}}); test_ignore_empty<T>({{11, 12, X::empty}});
test_ignore_empty({}); test_ignore_empty<T>({});
} }

View File

@ -314,7 +314,7 @@ void test_data_combinations(const std::vector<column>& input_data,
return; return;
} }
unique_file_name f{"test_parser2" + std::string{SEGMENT_NAME}}; unique_file_name f{"parser_data_combinations" + std::string{SEGMENT_NAME}};
std::vector<std::vector<field>> expected_data; std::vector<std::vector<field>> expected_data;
std::vector<std::string> header; std::vector<std::string> header;
std::vector<field> field_header; std::vector<field> field_header;