-
Notifications
You must be signed in to change notification settings - Fork 11
/
parsecmdTest.cpp
219 lines (156 loc) · 7.49 KB
/
parsecmdTest.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <vector>
#include <algorithm>
#include <iostream>
#include "../csv2xlslib/parsecmd.hpp"
#include "catch.hpp"
using namespace std;
using namespace csv2xls;
auto CmdArgsArray(std::vector<std::string>&args){
std::vector<char *> arg_ptrs(args.size(), nullptr);
auto const IntoPtr = [](std::string & str){ return str.data();};
std::transform(std::begin(args), std::end(args),std::begin(arg_ptrs), IntoPtr);
return arg_ptrs;
}
struct TheFixture
{
Config opts;
TheFixture() = default;
XlsSheetName const DEFAULT_XLS_SHEET_NAME = XlsSheetName("Table1");
virtual ~TheFixture() = default;
};
TEST_CASE_METHOD(TheFixture, "When_no_commandline_options_are_given then set_default_values")
{
std::vector<std::string> args{"prgname", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
CHECK(!my_opts.csv_file_has_headline.Get());
REQUIRE(DEFAULT_CSV_TAB_DELIMITER == my_opts.csv_separator);
REQUIRE(DEFAULT_XLS_MAX_LINES == my_opts.output_row_limit);
REQUIRE(DEFAULT_XLS_DIGIT_COUNT == my_opts.output_file_name.digit_count);
REQUIRE(DEFAULT_CSV_BUFFER_SIZE == my_opts.input_buffer_size);
REQUIRE(DEFAULT_XLS_SHEET_NAME == my_opts.xls_sheet_name);
}
TEST_CASE_METHOD(TheFixture, "When_no_commandline_options_are_given then guess_output_name_from_inputname")
{
std::vector<std::string> args{"prgname", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(InputFile("input1.csv") == my_opts.csv_file_name);
REQUIRE("input1.xls" == my_opts.output_file_name.Get(FileNumber(0)));
}
TEST_CASE_METHOD(TheFixture, "Given -o output input, adds xls to output")
{
std::vector<std::string> args{"prgname", "-o output", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE("output.xls" == my_opts.output_file_name.Get(FileNumber(0)));
}
TEST_CASE_METHOD(TheFixture, "Given -o output input.lol, adds xls to output")
{
std::vector<std::string> args{"prgname", "-o output.lo", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE("output.lo.xls" == my_opts.output_file_name.Get(FileNumber(0)));
}
TEST_CASE_METHOD(TheFixture, "Given -o output.Xls input.csv, the extension will be taken as it is")
{
std::vector<std::string> args{"prgname", "-o output.Xls", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE("output.Xls" == my_opts.output_file_name.Get(FileNumber(0)));
}
TEST_CASE_METHOD(TheFixture, "When_output_name_is_a_directory, guess_output_name_from_inputname")
{
std::vector<std::string> args{"prgname", "-o tmp/", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(InputFile("input1.csv") == my_opts.csv_file_name);
REQUIRE("tmp/input1.xls" == my_opts.output_file_name.Get(FileNumber(0)));
}
TEST_CASE_METHOD(TheFixture, "When_first_line_is_headline then it_should_fail_with_line_limit_1")
{
std::vector<std::string> args{"prgname", "-l 1", "-H", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_THROWS(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}
TEST_CASE_METHOD(TheFixture, "When user requests -l 65536 it_should_not throw")
{
std::vector<std::string> args{"prgname", "-l 65536", "-H", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE_NOTHROW(checkOptions(my_opts));
}
TEST_CASE_METHOD(TheFixture, "When user requests -l 65537 it_should_ throw")
{
std::vector<std::string> args{"prgname", "-l 65537", "-H", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_THROWS(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}
TEST_CASE_METHOD(TheFixture, "When_first_line_is_headline then it_accepts_line_limit_set_to_2")
{
std::vector<std::string> args{"prgname", "-l 2", "-H", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.csv_file_name == InputFile("input1.csv"));
REQUIRE(my_opts.output_row_limit == OutputRowLimit(2));
REQUIRE(my_opts.csv_file_has_headline == InputHasHeadLine(true));
}
TEST_CASE_METHOD(TheFixture, "When_first_line_is_not_headline then it_accepts_line_limit_set_to_1")
{
std::vector<std::string> args{"prgname", "-l 1", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.output_row_limit == OutputRowLimit(1));
}
TEST_CASE_METHOD(TheFixture, "When_line_limit_is_0 then it_should_fail")
{
std::vector<std::string> args{"prgname", "-l 0", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_THROWS(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}
TEST_CASE("Option v should not throw")
{
std::vector<std::string> args{"prgname", "-v"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_NOTHROW(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}
TEST_CASE("Option h should not throw")
{
std::vector<std::string> args{"prgname", "-h"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_NOTHROW(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}
TEST_CASE_METHOD(TheFixture, "Given option -d ',' Config.csv_separator is set to ','")
{
std::vector<std::string> args{"prgname", "-d ','", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.csv_separator == CsvSeparator (','));
}
TEST_CASE_METHOD(TheFixture, "Given option -t Config.csv_separator is set to '\t'")
{
std::vector<std::string> args{"prgname", "-t", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.csv_separator == CsvSeparator ('\t'));
}
TEST_CASE_METHOD(TheFixture, "Given option -t overrides option -d ',' and sets Config.csv_separator to '\t'")
{
std::vector<std::string> args{"prgname", "-t","-d ','", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.csv_separator == CsvSeparator ('\t'));
}
TEST_CASE_METHOD(TheFixture, R"(Given option -w Tab1 Config.xls_sheet_name to "Tab1")")
{
std::vector<std::string> args{"prgname", "-t", R"(-w Tab1)", "input1.csv"};
auto arg_ptrs = CmdArgsArray(args);
auto const my_opts = parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data());
REQUIRE(my_opts.xls_sheet_name == XlsSheetName ("Tab1"));
}
TEST_CASE_METHOD(TheFixture, "Given no argument then it_should_fail")
{
std::vector<std::string> args{"prgname"};
auto arg_ptrs = CmdArgsArray(args);
REQUIRE_THROWS(parse_commandline(static_cast<int>(arg_ptrs.size()), arg_ptrs.data()));
}