2022-03-12 00:43:00 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2022-03-12 00:08:38 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "csv/read-csv.hh"
|
2022-03-12 00:43:00 +01:00
|
|
|
#include "csv/write-csv.hh"
|
2022-03-12 00:08:38 +01:00
|
|
|
|
|
|
|
// Allow namespace pollution in tests for convenience
|
|
|
|
using namespace kraken::csv;
|
|
|
|
|
2022-03-12 00:32:05 +01:00
|
|
|
TEST(read_csv, empty) {
|
2022-03-12 00:08:38 +01:00
|
|
|
auto const expected = csv_type{};
|
|
|
|
ASSERT_EQ(read_csv(""), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:32:05 +01:00
|
|
|
TEST(read_csv, skip_header) {
|
2022-03-12 00:08:38 +01:00
|
|
|
auto const expected = csv_type{};
|
|
|
|
ASSERT_EQ(read_csv("a,b,c"), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:32:05 +01:00
|
|
|
TEST(read_csv, do_not_skip_header) {
|
2022-03-12 00:08:38 +01:00
|
|
|
auto const expected = csv_type{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
};
|
|
|
|
ASSERT_EQ(read_csv("a,b,c", CsvHeader::KEEP), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:32:05 +01:00
|
|
|
TEST(read_csv, single_line) {
|
2022-03-12 00:08:38 +01:00
|
|
|
auto const expected = csv_type{
|
|
|
|
{"1", "2", "3"},
|
|
|
|
};
|
|
|
|
ASSERT_EQ(read_csv("a,b,c\n1,2,3"), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:32:05 +01:00
|
|
|
TEST(read_csv, single_line_with_header) {
|
2022-03-12 00:08:38 +01:00
|
|
|
auto const expected = csv_type{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
{"1", "2", "3"},
|
|
|
|
};
|
|
|
|
ASSERT_EQ(read_csv("a,b,c\n1,2,3", CsvHeader::KEEP), expected);
|
|
|
|
}
|
2022-03-12 00:43:00 +01:00
|
|
|
|
|
|
|
TEST(write_csv, empty) {
|
|
|
|
auto const csv = csv_type{};
|
|
|
|
auto const expected = "";
|
|
|
|
auto output = std::ostringstream{};
|
|
|
|
write_csv(output, csv);
|
|
|
|
ASSERT_EQ(output.str(), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:45:27 +01:00
|
|
|
TEST(write_csv, single_line) {
|
2022-03-12 00:43:00 +01:00
|
|
|
auto const csv = csv_type{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
};
|
|
|
|
auto const expected = "a,b,c\n";
|
|
|
|
auto output = std::ostringstream{};
|
|
|
|
write_csv(output, csv);
|
|
|
|
ASSERT_EQ(output.str(), expected);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:45:27 +01:00
|
|
|
TEST(write_csv, multiple_lines) {
|
2022-03-12 00:43:00 +01:00
|
|
|
auto const csv = csv_type{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
{"1", "2", "3"},
|
|
|
|
};
|
|
|
|
auto const expected = "a,b,c\n1,2,3\n";
|
|
|
|
auto output = std::ostringstream{};
|
|
|
|
write_csv(output, csv);
|
|
|
|
ASSERT_EQ(output.str(), expected);
|
|
|
|
}
|