GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/unittests/utils/parameters.cc Lines: 217 217 100.0 %
Date: 2021-03-17 Branches: 724 1920 37.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "unittests/unittests.h"
23
24
#include "utils/parameters.h"
25
26
#include "debug.h"
27
28
3
TEST_CASE("parameters basic 1", "")
29
{
30
1
    StringVect pars;
31





10
    REQUIRE(splitParameters(pars, "", ",", '\"') == true);
32



4
    REQUIRE(pars.empty());
33
1
}
34
35
3
TEST_CASE("parameters basic 2", "")
36
{
37
1
    StringVect pars;
38





10
    REQUIRE(splitParameters(pars, "one,two, tree", ",", '\"') == true);
39



5
    REQUIRE(pars.size() == 3);
40



4
    REQUIRE(pars[0] == "one");
41



5
    REQUIRE(pars[1] == "two");
42



5
    REQUIRE(pars[2] == "tree");
43
1
}
44
45
3
TEST_CASE("parameters basic 3", "")
46
{
47
1
    StringVect pars;
48





10
    REQUIRE(splitParameters(pars, ", ,,,", ",", '\"') == true);
49



4
    REQUIRE(pars.empty());
50
1
}
51
52
3
TEST_CASE("parameters basic 4", "")
53
{
54
1
    StringVect pars;
55





10
    REQUIRE(splitParameters(pars, "one,,two, tree", ",", '\"') == true);
56



5
    REQUIRE(pars.size() == 3);
57



4
    REQUIRE(pars[0] == "one");
58



5
    REQUIRE(pars[1] == "two");
59



5
    REQUIRE(pars[2] == "tree");
60
1
}
61
62
3
TEST_CASE("parameters escape 1", "")
63
{
64
1
    StringVect pars;
65





10
    REQUIRE(splitParameters(pars, "\\\"", ",", '\"') == true);
66



5
    REQUIRE(pars.size() == 1);
67



4
    REQUIRE(pars[0] == "\"");
68
1
}
69
70
3
TEST_CASE("parameters escape 2", "")
71
{
72
1
    StringVect pars;
73





10
    REQUIRE(splitParameters(pars, "\\\", test", ",", '\"') == true);
74



5
    REQUIRE(pars.size() == 2);
75



4
    REQUIRE(pars[0] == "\"");
76



5
    REQUIRE(pars[1] == "test");
77
1
}
78
79
3
TEST_CASE("parameters escape 3", "")
80
{
81
1
    StringVect pars;
82





10
    REQUIRE(splitParameters(pars, "test,\\\"", ",", '\"') == true);
83



5
    REQUIRE(pars.size() == 2);
84



4
    REQUIRE(pars[0] == "test");
85



5
    REQUIRE(pars[1] == "\"");
86
1
}
87
88
3
TEST_CASE("parameters quote 1", "")
89
{
90
1
    StringVect pars;
91





10
    REQUIRE(splitParameters(pars, "\"one\",,two, tree", ",", '\"') == true);
92



5
    REQUIRE(pars.size() == 3);
93



4
    REQUIRE(pars[0] == "one");
94



5
    REQUIRE(pars[1] == "two");
95



5
    REQUIRE(pars[2] == "tree");
96
1
}
97
98
3
TEST_CASE("parameters quote 2", "")
99
{
100
1
    StringVect pars;
101





10
    REQUIRE(splitParameters(pars, "\"\",,two, tree", ",", '\"') == true);
102



5
    REQUIRE(pars.size() == 3);
103



4
    REQUIRE(pars[0].empty());
104



5
    REQUIRE(pars[1] == "two");
105



5
    REQUIRE(pars[2] == "tree");
106
1
}
107
108
3
TEST_CASE("parameters quote 3", "")
109
{
110
1
    StringVect pars;
111





10
    REQUIRE(splitParameters(pars, "\"one test\",,two, tree", ",", '\"') ==
112
        true);
113



5
    REQUIRE(pars.size() == 3);
114



4
    REQUIRE(pars[0] == "one test");
115



5
    REQUIRE(pars[1] == "two");
116



5
    REQUIRE(pars[2] == "tree");
117
1
}
118
119
3
TEST_CASE("parameters quote 4", "")
120
{
121
1
    StringVect pars;
122





10
    REQUIRE(splitParameters(pars, "\"\\\"one test\\\"\",,two, tree", ",", '\"')
123
        == true);
124



5
    REQUIRE(pars.size() == 3);
125



4
    REQUIRE(pars[0] == "\"one test\"");
126



5
    REQUIRE(pars[1] == "two");
127



5
    REQUIRE(pars[2] == "tree");
128
1
}
129
130
3
TEST_CASE("parameters quote 5", "")
131
{
132
1
    StringVect pars;
133





10
    REQUIRE(splitParameters(pars, "\"\\\"one \\\"test\\\"\",,two, tree",
134
        ",", '\"') == true);
135



5
    REQUIRE(pars.size() == 3);
136



4
    REQUIRE(pars[0] == "\"one \"test\"");
137



5
    REQUIRE(pars[1] == "two");
138



5
    REQUIRE(pars[2] == "tree");
139
1
}
140
141
3
TEST_CASE("parameters quote 6", "")
142
{
143
1
    StringVect pars;
144





10
    REQUIRE(splitParameters(pars, "\"one, test\",,two, tree", ",", '\"')
145
        == true);
146



5
    REQUIRE(pars.size() == 3);
147



4
    REQUIRE(pars[0] == "one, test");
148



5
    REQUIRE(pars[1] == "two");
149



5
    REQUIRE(pars[2] == "tree");
150
1
}
151
152
3
TEST_CASE("parameters quote 7", "")
153
{
154
1
    StringVect pars;
155





10
    REQUIRE(splitParameters(pars, "\"\\\"one, test\\\"\",,two, tree",
156
        ",", '\"') == true);
157



5
    REQUIRE(pars.size() == 3);
158



4
    REQUIRE(pars[0] == "\"one, test\"");
159



5
    REQUIRE(pars[1] == "two");
160



5
    REQUIRE(pars[2] == "tree");
161
1
}
162
163
3
TEST_CASE("parameters quote 8", "")
164
{
165
1
    StringVect pars;
166





10
    REQUIRE(splitParameters(pars, "\"\\\"\",,two, tree", ",", '\"')
167
        == true);
168



5
    REQUIRE(pars.size() == 3);
169



4
    REQUIRE(pars[0] == "\"");
170



5
    REQUIRE(pars[1] == "two");
171



5
    REQUIRE(pars[2] == "tree");
172
1
}
173
174
3
TEST_CASE("parameters quote 9", "")
175
{
176
1
    StringVect pars;
177





10
    REQUIRE(splitParameters(pars, "\"\\\",,two, tree", ",", '\"')
178
        == true);
179



5
    REQUIRE(pars.size() == 1);
180



4
    REQUIRE(pars[0] == "\"\",,two, tree");
181
1
}
182
183
3
TEST_CASE("parameters quote 10", "")
184
{
185
1
    StringVect pars;
186





10
    REQUIRE(splitParameters(pars, "\"", ",", '\"')
187
        == true);
188



5
    REQUIRE(pars.size() == 1);
189



4
    REQUIRE(pars[0] == "\"");
190
1
}
191
192
3
TEST_CASE("parameters quote 11", "")
193
{
194
1
    StringVect pars;
195





10
    REQUIRE(splitParameters(pars, "\\\"", ",", '\"')
196
        == true);
197



5
    REQUIRE(pars.size() == 1);
198



4
    REQUIRE(pars[0] == "\"");
199
1
}
200
201
3
TEST_CASE("parameters quote 12", "")
202
{
203
1
    StringVect pars;
204





10
    REQUIRE(splitParameters(pars, ",\"", ",", '\"')
205
        == true);
206



5
    REQUIRE(pars.size() == 1);
207



4
    REQUIRE(pars[0] == "\"");
208
1
}
209
210
3
TEST_CASE("parameters quote 13", "")
211
{
212
1
    StringVect pars;
213





10
    REQUIRE(splitParameters(pars, "\",", ",", '\"')
214
        == true);
215



5
    REQUIRE(pars.size() == 1);
216



4
    REQUIRE(pars[0] == "\",");
217
1
}
218
219
3
TEST_CASE("parameters quote 14", "")
220
{
221
1
    StringVect pars;
222





10
    REQUIRE(splitParameters(pars, "\\\",", ",", '\"')
223
        == true);
224



5
    REQUIRE(pars.size() == 1);
225



4
    REQUIRE(pars[0] == "\"");
226
1
}
227
228
3
TEST_CASE("parameters quote 15", "")
229
{
230
1
    StringVect pars;
231





10
    REQUIRE(splitParameters(pars, ",\\\"", ",", '\"')
232
        == true);
233



5
    REQUIRE(pars.size() == 1);
234



4
    REQUIRE(pars[0] == "\"");
235
1
}
236
237
3
TEST_CASE("parameters quote 16", "")
238
{
239
1
    StringVect pars;
240





10
    REQUIRE(splitParameters(pars, "\"one test\"", ",", '\"') ==
241
        true);
242



5
    REQUIRE(pars.size() == 1);
243



4
    REQUIRE(pars[0] == "one test");
244
1
}
245
246
3
TEST_CASE("parameters quote 17", "")
247
{
248
1
    StringVect pars;
249





10
    REQUIRE(splitParameters(pars, "\"one, test\"", ",", '\"') ==
250
        true);
251



5
    REQUIRE(pars.size() == 1);
252



4
    REQUIRE(pars[0] == "one, test");
253
1
}
254
255
3
TEST_CASE("parameters quote 18", "")
256
{
257
1
    StringVect pars;
258





10
    REQUIRE(splitParameters(pars, "\"one\\\" test\"", ",", '\"') ==
259
        true);
260



5
    REQUIRE(pars.size() == 1);
261



4
    REQUIRE(pars[0] == "one\" test");
262
1
}
263
264
3
TEST_CASE("parameters quote 19", "")
265
{
266
1
    StringVect pars;
267





10
    REQUIRE(splitParameters(pars, "\"one\\\" ,test\"", ",", '\"') ==
268
        true);
269



5
    REQUIRE(pars.size() == 1);
270



4
    REQUIRE(pars[0] == "one\" ,test");
271
1
}
272
273
3
TEST_CASE("parameters quote 20", "")
274
{
275
1
    StringVect pars;
276





10
    REQUIRE(splitParameters(pars, "\"one\\\" test,\"", ",", '\"') ==
277
        true);
278



5
    REQUIRE(pars.size() == 1);
279



4
    REQUIRE(pars[0] == "one\" test,");
280
1
}
281
282
3
TEST_CASE("parameters complex 1", "")
283
{
284
1
    StringVect pars;
285





10
    REQUIRE(splitParameters(pars, "\"test\" \"line\"", ",", '\"') == true);
286



5
    REQUIRE(pars.size() == 1);
287



4
    REQUIRE(pars[0] == "test\" \"line");
288
1
}
289
290
3
TEST_CASE("parameters complex 2", "")
291
{
292
1
    StringVect pars;
293





10
    REQUIRE(splitParameters(pars, "\"test\", \"line\"", ",", '\"') == true);
294



5
    REQUIRE(pars.size() == 2);
295



4
    REQUIRE(pars[0] == "test");
296



5
    REQUIRE(pars[1] == "line");
297
1
}
298
299
3
TEST_CASE("parameters complex 3", "")
300
{
301
1
    StringVect pars;
302





10
    REQUIRE(splitParameters(pars, "\"test,\" \"line\"", ",", '\"') == true);
303



5
    REQUIRE(pars.size() == 1);
304



4
    REQUIRE(pars[0] == "test,\" \"line");
305
1
}
306
307
3
TEST_CASE("parameters broken 1", "")
308
{
309
1
    StringVect pars;
310





10
    REQUIRE(splitParameters(pars, "\"", ",", '\"') == true);
311



5
    REQUIRE(pars.size() == 1);
312



4
    REQUIRE(pars[0] == "\"");
313
1
}
314
315
3
TEST_CASE("parameters broken 2", "")
316
{
317
1
    StringVect pars;
318





10
    REQUIRE(splitParameters(pars, ",", ",", '\"') == true);
319



4
    REQUIRE(pars.empty());
320
1
}
321
322
3
TEST_CASE("parameters broken 3", "")
323
{
324
1
    StringVect pars;
325





10
    REQUIRE(splitParameters(pars,
326
        ",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,", ",", '\"') == true);
327



4
    REQUIRE(pars.empty());
328

4
}