-
Notifications
You must be signed in to change notification settings - Fork 28
/
test_csv.cpp
149 lines (143 loc) · 4.62 KB
/
test_csv.cpp
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
148
149
#include "../include/argagg/argagg.hpp"
#include "../include/argagg/convert/csv.hpp"
#include "doctest.h"
#include <iostream>
#include <vector>
// Define a custom conversion function for the test that follows
struct plus_triple {
double x;
double y;
double z;
};
namespace argagg {
namespace convert {
template <>
plus_triple arg(const char* s)
{
plus_triple result {0.0, 0.0, 0.0};
if (!parse_next_component(s, result.x, '+')) {
return result;
}
if (!parse_next_component(s, result.y, '+')) {
return result;
}
if (!parse_next_component(s, result.z, '+')) {
return result;
}
return result;
}
} // namespace convert
} // namespace argagg
TEST_CASE("comma separated values")
{
argagg::parser argparser {{
{ "items", {"--items"},
"items as a comma separated list of components (e.g. 'alice,bob,1,2')", 1},
}};
SUBCASE("empty") {
std::vector<const char*> argv {
"test", "--items", ""};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<std::string>>();
CHECK(items.values.size() == 1);
CHECK(items.values[0] == "");
}
SUBCASE("one") {
std::vector<const char*> argv {
"test", "--items", "alice"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<std::string>>();
CHECK(items.values.size() == 1);
CHECK(items.values[0] == "alice");
}
SUBCASE("two") {
std::vector<const char*> argv {
"test", "--items", "alice,bob"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<std::string>>();
CHECK(items.values.size() == 2);
CHECK(items.values[0] == "alice");
CHECK(items.values[1] == "bob");
}
SUBCASE("multiple empty") {
std::vector<const char*> argv {
"test", "--items", "alice,,,bob"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<std::string>>();
CHECK(items.values.size() == 4);
CHECK(items.values[0] == "alice");
CHECK(items.values[1] == "");
CHECK(items.values[2] == "");
CHECK(items.values[3] == "bob");
}
SUBCASE("integers") {
std::vector<const char*> argv {
"test", "--items", "1,2,3"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<int>>();
CHECK(items.values.size() == 3);
CHECK(items.values[0] == 1);
CHECK(items.values[1] == 2);
CHECK(items.values[2] == 3);
}
SUBCASE("integers, bad conversion") {
std::vector<const char*> argv {
"test", "--items", "1,bob,3"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
CHECK_THROWS({
args["items"].as<argagg::csv<int>>();
});
}
SUBCASE("doubles") {
std::vector<const char*> argv {
"test", "--items", "1.2,3.45,6.789"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<double>>();
CHECK(items.values.size() == 3);
CHECK(items.values[0] == 1.2);
CHECK(items.values[1] == 3.45);
CHECK(items.values[2] == 6.789);
}
SUBCASE("doubles, bad conversion") {
std::vector<const char*> argv {
"test", "--items", "alice,3.45,6.789"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
CHECK_THROWS({
args["items"].as<argagg::csv<double>>();
});
SUBCASE("plus triples") {
std::vector<const char*> argv {
"test", "--items", "1.2+2.4+3.141,4.0+5.32,6.5341"};
argagg::parser_results args =
argparser.parse(argv.size(), &(argv.front()));
CHECK(args.has_option("items") == true);
auto items = args["items"].as<argagg::csv<plus_triple>>();
CHECK(items.values.size() == 3);
CHECK(items.values[0].x == 1.2);
CHECK(items.values[0].y == 2.4);
CHECK(items.values[0].z == 3.141);
CHECK(items.values[1].x == 4.0);
CHECK(items.values[1].y == 5.32);
CHECK(items.values[1].z == 0.0);
CHECK(items.values[2].x == 6.5341);
CHECK(items.values[2].y == 0.0);
CHECK(items.values[2].z == 0.0);
}
}
}