1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include "testing/testing.hpp"
#include "coding/csv_file_reader.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"
#include <string>
#include <vector>
namespace
{
std::string const kCSV1 = "a,b,c,d\ne,f,g h";
std::string const kCSV2 = "a,b,cd a b, c";
std::string const kCSV3 = "";
} // namespace
using coding::CSVReader;
using Row = std::vector<std::string>;
using File = std::vector<Row>;
UNIT_TEST(CSVReaderSmoke)
{
auto const fileName = "test.csv";
platform::tests_support::ScopedFile sf(fileName, kCSV1);
auto const & filePath = sf.GetFullPath();
CSVReader reader;
reader.ReadFullFile(filePath, [](File const & file) {
TEST_EQUAL(file.size(), 1, ());
TEST_EQUAL(file[0].size(), 3, ());
Row const firstRow = {"e", "f", "g h"};
TEST_EQUAL(file[0], firstRow, ());
});
CSVReader::Params p;
p.m_shouldReadHeader = true;
reader.ReadFullFile(filePath,
[](File const & file) {
TEST_EQUAL(file.size(), 2, ());
Row const headerRow = {"a", "b", "c", "d"};
TEST_EQUAL(file[0], headerRow, ());
},
p);
}
UNIT_TEST(CSVReaderCustomDelimiter)
{
auto const fileName = "test.csv";
platform::tests_support::ScopedFile sf(fileName, kCSV2);
auto const & filePath = sf.GetFullPath();
CSVReader reader;
CSVReader::Params p;
p.m_shouldReadHeader = true;
p.m_delimiter = ' ';
reader.ReadLineByLine(filePath,
[](Row const & row) {
Row const firstRow = {"a,b,cd", "a", "b,", "c"};
TEST_EQUAL(row, firstRow, ());
},
p);
}
UNIT_TEST(CSVReaderEmptyFile)
{
auto const fileName = "test.csv";
platform::tests_support::ScopedFile sf(fileName, kCSV2);
auto const & filePath = sf.GetFullPath();
CSVReader reader;
reader.ReadFullFile(filePath, [](File const & file) { TEST_EQUAL(file.size(), 0, ()); });
}
|