-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_tests.cpp
More file actions
147 lines (117 loc) · 3.74 KB
/
Copy pathcsv_tests.cpp
File metadata and controls
147 lines (117 loc) · 3.74 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @file csv_tests.cpp
* @brief Basic tests for rix/csv.
*
* @author Gaspard Kirira
*/
#include <rix/csv.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
namespace
{
void expect_true(bool condition, const std::string &message)
{
if (!condition)
{
std::cerr << "FAILED: " << message << '\n';
std::exit(1);
}
}
void test_parse_single_row()
{
const rixlib::csv::Table table =
rixlib::csv::parse("name,language,level\n");
expect_true(table.size() == 1, "parse should return 1 row");
expect_true(table[0].size() == 3, "row should contain 3 fields");
expect_true(table[0][0] == "name", "first field should be name");
expect_true(table[0][1] == "language", "second field should be language");
expect_true(table[0][2] == "level", "third field should be level");
}
void test_parse_table()
{
const std::string input =
"name,language\n"
"Ada,C++\n"
"Gaspard,Vix\n";
const rixlib::csv::Table table = rixlib::csv::parse(input);
expect_true(table.size() == 3, "parse should return 3 rows");
expect_true(table[0].size() == 2, "header row should contain 2 fields");
expect_true(table[1][0] == "Ada", "second row first field should be Ada");
expect_true(table[1][1] == "C++", "second row second field should be C++");
expect_true(table[2][0] == "Gaspard", "third row first field should be Gaspard");
expect_true(table[2][1] == "Vix", "third row second field should be Vix");
}
void test_parse_skips_empty_lines_when_enabled()
{
const std::string input =
"name,language\n"
"\n"
"Ada,C++\n";
rixlib::csv::Options options{};
options.skip_empty_lines = true;
const rixlib::csv::Table table = rixlib::csv::parse(input, options);
expect_true(table.size() == 2, "parse should skip empty lines when enabled");
expect_true(table[1][0] == "Ada", "remaining data row should be Ada");
}
void test_write_row()
{
const rixlib::csv::Row row = {"Ada", "C++", "expert"};
const std::string output = rixlib::csv::write_row(row);
expect_true(output == "Ada,C++,expert", "write_row should join fields with comma");
}
void test_write_table()
{
const rixlib::csv::Table table = {
{"name", "language"},
{"Ada", "C++"},
{"Gaspard", "Vix"},
};
const std::string output = rixlib::csv::write(table);
const std::string expected =
"name,language\n"
"Ada,C++\n"
"Gaspard,Vix\n";
expect_true(output == expected, "write should serialize table to CSV text");
}
void test_csv_facade_parse_and_write()
{
const rixlib::csv::Csv csv{};
const std::string input =
"name,language\n"
"Ada,C++\n";
const rixlib::csv::Table table = csv.parse(input);
expect_true(table.size() == 2, "Csv::parse should return 2 rows");
expect_true(table[1][0] == "Ada", "Csv::parse should parse data rows");
expect_true(table[1][1] == "C++", "Csv::parse should parse second field");
const std::string output = csv.write(table);
expect_true(
output == input,
"Csv::write should serialize parsed table");
}
void test_csv_facade_write_row()
{
const rixlib::csv::Csv csv{};
const rixlib::csv::Row row = {"Rix", "CSV"};
const std::string output = csv.write_row(row);
expect_true(
output == "Rix,CSV",
"Csv::write_row should serialize one row");
}
void run_tests()
{
test_parse_single_row();
test_parse_table();
test_parse_skips_empty_lines_when_enabled();
test_write_row();
test_write_table();
test_csv_facade_parse_and_write();
test_csv_facade_write_row();
}
}
int main()
{
run_tests();
std::cout << "csv tests passed\n";
return 0;
}