mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 13:05:20 +01:00
Fix test_parser.cpp formating
This commit is contained in:
parent
78c75abec7
commit
f58091cf02
@ -20,15 +20,18 @@ struct unique_file_name {
|
||||
const std::string name;
|
||||
|
||||
unique_file_name()
|
||||
: name{"random_" + std::to_string(i++) + time_now_rand() + "_file.csv"} {}
|
||||
: name{"random_" + std::to_string(i++) + time_now_rand() +
|
||||
"_file.csv"} {
|
||||
}
|
||||
|
||||
~unique_file_name() { std::filesystem::remove(name); }
|
||||
~unique_file_name() {
|
||||
std::filesystem::remove(name);
|
||||
}
|
||||
};
|
||||
|
||||
void replace_all(std::string& s, const std::string& from,
|
||||
const std::string& to) {
|
||||
if (from.empty())
|
||||
return;
|
||||
if (from.empty()) return;
|
||||
size_t start_pos = 0;
|
||||
while ((start_pos = s.find(from, start_pos)) != std::string::npos) {
|
||||
s.replace(start_pos, from.length(), to);
|
||||
@ -62,7 +65,9 @@ struct X {
|
||||
.append(delim)
|
||||
.append(s);
|
||||
}
|
||||
auto tied() const { return std::tie(i, d, s); }
|
||||
auto tied() const {
|
||||
return std::tie(i, d, s);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@ -256,14 +261,15 @@ TEST_CASE("parser test various cases") {
|
||||
std::vector<X> i2;
|
||||
|
||||
while (!p.eof()) {
|
||||
auto a = p.get_object<X, ss::ax<int, excluded>, double, std::string>();
|
||||
auto a =
|
||||
p.get_object<X, ss::ax<int, excluded>, double, std::string>();
|
||||
if (p.valid()) {
|
||||
i.push_back(a);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &&a :
|
||||
p2.iterate_object<X, ss::ax<int, excluded>, double, std::string>()) {
|
||||
for (auto&& a : p2.iterate_object<X, ss::ax<int, excluded>, double,
|
||||
std::string>()) {
|
||||
if (p2.valid()) {
|
||||
i2.push_back(std::move(a));
|
||||
}
|
||||
@ -338,10 +344,13 @@ struct test_struct {
|
||||
int i;
|
||||
double d;
|
||||
char c;
|
||||
auto tied() { return std::tie(i, d, c); }
|
||||
auto tied() {
|
||||
return std::tie(i, d, c);
|
||||
}
|
||||
};
|
||||
|
||||
void expect_test_struct(const test_struct &) {}
|
||||
void expect_test_struct(const test_struct&) {
|
||||
}
|
||||
|
||||
// various scenarios
|
||||
TEST_CASE("parser test composite conversion") {
|
||||
@ -365,11 +374,11 @@ TEST_CASE("parser test composite conversion") {
|
||||
{
|
||||
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
|
||||
|
||||
auto [d1, d2, d3, d4] = p.try_next<int, int, double>(fail)
|
||||
auto [d1, d2, d3, d4] =
|
||||
p.try_next<int, int, double>(fail)
|
||||
.or_else<test_struct>(fail)
|
||||
.or_else<int, char, double>([&](auto &&data) {
|
||||
CHECK_EQ(data, expectedData);
|
||||
})
|
||||
.or_else<int, char, double>(
|
||||
[&](auto&& data) { CHECK_EQ(data, expectedData); })
|
||||
.on_error(fail)
|
||||
.or_else<test_tuple>(fail)
|
||||
.values();
|
||||
@ -429,7 +438,8 @@ TEST_CASE("parser test composite conversion") {
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2] = p.try_next<int, double>([](auto &i, auto &d) {
|
||||
auto [d1, d2] =
|
||||
p.try_next<int, double>([](auto& i, auto& d) {
|
||||
REQUIRE_EQ(std::tie(i, d), std::tuple{10, 11.1});
|
||||
})
|
||||
.or_else<int, double>([](auto&, auto&) { FAIL(""); })
|
||||
@ -456,7 +466,8 @@ TEST_CASE("parser test composite conversion") {
|
||||
{
|
||||
REQUIRE(!p.eof());
|
||||
|
||||
auto [d1, d2, d3, d4, d5] = p.try_next<int, int, double>(fail)
|
||||
auto [d1, d2, d3, d4, d5] =
|
||||
p.try_next<int, int, double>(fail)
|
||||
.or_object<test_struct, int, double, char>()
|
||||
.or_else<test_struct>(expect_test_struct)
|
||||
.or_else<test_tuple>(fail)
|
||||
@ -558,7 +569,9 @@ struct my_string {
|
||||
|
||||
my_string() = default;
|
||||
|
||||
~my_string() { delete[] data; }
|
||||
~my_string() {
|
||||
delete[] data;
|
||||
}
|
||||
|
||||
// make sure no object is copied
|
||||
my_string(const my_string&) = delete;
|
||||
@ -587,7 +600,9 @@ struct xyz {
|
||||
my_string x;
|
||||
my_string y;
|
||||
my_string z;
|
||||
auto tied() { return std::tie(x, y, z); }
|
||||
auto tied() {
|
||||
return std::tie(x, y, z);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("parser test the moving of parsed composite values") {
|
||||
@ -673,9 +688,12 @@ std::string no_escape(std::string &s) {
|
||||
|
||||
TEST_CASE("parser test csv on multiple lines with escapes") {
|
||||
unique_file_name f;
|
||||
std::vector<X> data = {
|
||||
{1, 2, "x\\\nx\\\r\nx"}, {5, 6, "z\\\nz\\\nz"}, {7, 8, "u"},
|
||||
{3, 4, "y\\\ny\\\ny"}, {9, 10, "v\\\\"}, {11, 12, "w\\\n"}};
|
||||
std::vector<X> data = {{1, 2, "x\\\nx\\\r\nx"},
|
||||
{5, 6, "z\\\nz\\\nz"},
|
||||
{7, 8, "u"},
|
||||
{3, 4, "y\\\ny\\\ny"},
|
||||
{9, 10, "v\\\\"},
|
||||
{11, 12, "w\\\n"}};
|
||||
for (auto& [_, __, s] : data) {
|
||||
update_if_crlf(s);
|
||||
}
|
||||
@ -768,8 +786,8 @@ TEST_CASE("parser test multiline restricted") {
|
||||
out << "19,20,just strings" << std::endl;
|
||||
}
|
||||
|
||||
ss::parser<ss::multiline_restricted<2>, ss::quote<'"'>, ss::escape<'\\'>> p{
|
||||
f.name, ","};
|
||||
ss::parser<ss::multiline_restricted<2>, ss::quote<'"'>, ss::escape<'\\'>>
|
||||
p{f.name, ","};
|
||||
std::vector<X> i;
|
||||
|
||||
while (!p.eof()) {
|
||||
@ -794,13 +812,16 @@ TEST_CASE("parser test multiline restricted") {
|
||||
CHECK_EQ(i, data);
|
||||
}
|
||||
|
||||
template <typename T, typename Tuple> struct has_type;
|
||||
template <typename T, typename Tuple>
|
||||
struct has_type;
|
||||
|
||||
template <typename T, typename... Us>
|
||||
struct has_type<T, std::tuple<Us...>>
|
||||
: std::disjunction<std::is_same<T, Us>...> {};
|
||||
|
||||
void checkSize(size_t size1, size_t size2) { CHECK_EQ(size1, size2); }
|
||||
void checkSize(size_t size1, size_t size2) {
|
||||
CHECK_EQ(size1, size2);
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
void testFields(const std::string file_name, const std::vector<X>& data,
|
||||
@ -928,7 +949,8 @@ TEST_CASE("parser test various cases with header") {
|
||||
p.use_fields(Str, Int, Dbl);
|
||||
|
||||
{
|
||||
auto [string_, int_, double_] = p.get_next<std::string, int, double>();
|
||||
auto [string_, int_, double_] =
|
||||
p.get_next<std::string, int, double>();
|
||||
CHECK_EQ(double_, data[3].d);
|
||||
CHECK_EQ(int_, data[3].i);
|
||||
CHECK_EQ(string_, data[3].s);
|
||||
|
Loading…
Reference in New Issue
Block a user