comparison tests/src/libcommon/string-util/main.cpp @ 669:6eb4caea77a5

Tests: split libirccd util tests into libcommon, closes #789 @1h While here, remove unneeded and deprecated functions from string_util and adapt existing code to new functions.
author David Demelier <markand@malikania.fr>
date Fri, 06 Apr 2018 22:06:07 +0200
parents
children e8c4ba5ed1c6
comparison
equal deleted inserted replaced
668:8a79b5c0ddc7 669:6eb4caea77a5
1 /*
2 * main.cpp -- test string_util functions
3 *
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define BOOST_TEST_MODULE "string_util"
20 #include <boost/test/unit_test.hpp>
21
22 #include <irccd/string_util.hpp>
23 #include <irccd/system.hpp>
24
25 namespace irccd {
26
27 /*
28 * string_util::format function
29 * --------------------------------------------------------
30 */
31 BOOST_AUTO_TEST_SUITE(format)
32
33 BOOST_AUTO_TEST_CASE(nothing)
34 {
35 std::string expected = "hello world!";
36 std::string result = string_util::format("hello world!");
37
38 BOOST_TEST(expected == result);
39 }
40
41 BOOST_AUTO_TEST_CASE(escape)
42 {
43 string_util::subst params;
44
45 params.keywords.emplace("target", "hello");
46
47 BOOST_TEST(string_util::format("$@#") == "$@#");
48 BOOST_TEST(string_util::format(" $ @ # ") == " $ @ # ");
49 BOOST_TEST(string_util::format("#") == "#");
50 BOOST_TEST(string_util::format(" # ") == " # ");
51 BOOST_TEST(string_util::format("#@") == "#@");
52 BOOST_TEST(string_util::format("##") == "##");
53 BOOST_TEST(string_util::format("#!") == "#!");
54 BOOST_TEST(string_util::format("##{target}") == "#{target}");
55 BOOST_TEST(string_util::format("@#{target}", params) == "@hello");
56 BOOST_TEST(string_util::format("#{target}#", params) == "hello#");
57 BOOST_REQUIRE_THROW(string_util::format("#{failure"), std::exception);
58 }
59
60 BOOST_AUTO_TEST_CASE(disable_date)
61 {
62 string_util::subst params;
63
64 params.flags &= ~(string_util::subst_flags::date);
65
66 BOOST_TEST(string_util::format("%H:%M", params) == "%H:%M");
67 }
68
69 BOOST_AUTO_TEST_CASE(disable_keywords)
70 {
71 string_util::subst params;
72
73 params.keywords.emplace("target", "hello");
74 params.flags &= ~(string_util::subst_flags::keywords);
75
76 BOOST_TEST(string_util::format("#{target}", params) == "#{target}");
77 }
78
79 BOOST_AUTO_TEST_CASE(disable_env)
80 {
81 string_util::subst params;
82
83 params.flags &= ~(string_util::subst_flags::env);
84
85 BOOST_TEST(string_util::format("${HOME}", params) == "${HOME}");
86 }
87
88 BOOST_AUTO_TEST_CASE(keyword_simple)
89 {
90 string_util::subst params;
91
92 params.keywords.insert({"target", "irccd"});
93
94 std::string expected = "hello irccd!";
95 std::string result = string_util::format("hello #{target}!", params);
96
97 BOOST_TEST(expected == result);
98 }
99
100 BOOST_AUTO_TEST_CASE(keyword_multiple)
101 {
102 string_util::subst params;
103
104 params.keywords.insert({"target", "irccd"});
105 params.keywords.insert({"source", "nightmare"});
106
107 std::string expected = "hello irccd from nightmare!";
108 std::string result = string_util::format("hello #{target} from #{source}!", params);
109
110 BOOST_TEST(expected == result);
111 }
112
113 BOOST_AUTO_TEST_CASE(keyword_adj_twice)
114 {
115 string_util::subst params;
116
117 params.keywords.insert({"target", "irccd"});
118
119 std::string expected = "hello irccdirccd!";
120 std::string result = string_util::format("hello #{target}#{target}!", params);
121
122 BOOST_TEST(expected == result);
123 }
124
125 BOOST_AUTO_TEST_CASE(keyword_missing)
126 {
127 std::string expected = "hello !";
128 std::string result = string_util::format("hello #{target}!");
129
130 BOOST_TEST(expected == result);
131 }
132
133 BOOST_AUTO_TEST_CASE(env_simple)
134 {
135 std::string home = sys::env("HOME");
136
137 if (!home.empty()) {
138 std::string expected = "my home is " + home;
139 std::string result = string_util::format("my home is ${HOME}");
140
141 BOOST_TEST(expected == result);
142 }
143 }
144
145 BOOST_AUTO_TEST_CASE(env_missing)
146 {
147 std::string expected = "value is ";
148 std::string result = string_util::format("value is ${HOPE_THIS_VAR_NOT_EXIST}");
149
150 BOOST_TEST(expected == result);
151 }
152
153 BOOST_AUTO_TEST_SUITE_END()
154
155 /*
156 * string_util::split function
157 * --------------------------------------------------------
158 */
159
160 BOOST_AUTO_TEST_SUITE(split)
161
162 using list = std::vector<std::string>;
163
164 BOOST_AUTO_TEST_CASE(simple)
165 {
166 list expected { "a", "b" };
167 list result = string_util::split("a;b", ";");
168
169 BOOST_TEST(expected == result);
170 }
171
172 BOOST_AUTO_TEST_CASE(cut)
173 {
174 list expected { "msg", "#staff", "foo bar baz" };
175 list result = string_util::split("msg;#staff;foo bar baz", ";", 3);
176
177 BOOST_TEST(expected == result);
178 }
179
180 BOOST_AUTO_TEST_SUITE_END()
181
182 /*
183 * string_util::strip function
184 * --------------------------------------------------------
185 */
186
187 BOOST_AUTO_TEST_SUITE(strip)
188
189 BOOST_AUTO_TEST_CASE(left)
190 {
191 std::string value = " 123";
192 std::string result = string_util::strip(value);
193
194 BOOST_TEST(result == "123");
195 }
196
197 BOOST_AUTO_TEST_CASE(right)
198 {
199 std::string value = "123 ";
200 std::string result = string_util::strip(value);
201
202 BOOST_TEST(result == "123");
203 }
204
205 BOOST_AUTO_TEST_CASE(both)
206 {
207 std::string value = " 123 ";
208 std::string result = string_util::strip(value);
209
210 BOOST_TEST(result == "123");
211 }
212
213 BOOST_AUTO_TEST_CASE(none)
214 {
215 std::string value = "without";
216 std::string result = string_util::strip(value);
217
218 BOOST_TEST(result == "without");
219 }
220
221 BOOST_AUTO_TEST_CASE(betweenEmpty)
222 {
223 std::string value = "one list";
224 std::string result = string_util::strip(value);
225
226 BOOST_TEST(result == "one list");
227 }
228
229 BOOST_AUTO_TEST_CASE(betweenLeft)
230 {
231 std::string value = " space at left";
232 std::string result = string_util::strip(value);
233
234 BOOST_TEST(result == "space at left");
235 }
236
237 BOOST_AUTO_TEST_CASE(betweenRight)
238 {
239 std::string value = "space at right ";
240 std::string result = string_util::strip(value);
241
242 BOOST_TEST(result == "space at right");
243 }
244
245 BOOST_AUTO_TEST_CASE(betweenBoth)
246 {
247 std::string value = " space at both ";
248 std::string result = string_util::strip(value);
249
250 BOOST_TEST(result == "space at both");
251 }
252
253 BOOST_AUTO_TEST_CASE(empty)
254 {
255 std::string value = " ";
256 std::string result = string_util::strip(value);
257
258 BOOST_TEST(result == "");
259 }
260
261 BOOST_AUTO_TEST_SUITE_END()
262
263 /*
264 * string_util::join function
265 * --------------------------------------------------------
266 */
267
268 BOOST_AUTO_TEST_SUITE(join)
269
270 BOOST_AUTO_TEST_CASE(empty)
271 {
272 std::string expected = "";
273 std::string result = string_util::join<int>({});
274
275 BOOST_TEST(expected == result);
276 }
277
278 BOOST_AUTO_TEST_CASE(one)
279 {
280 std::string expected = "1";
281 std::string result = string_util::join({1});
282
283 BOOST_TEST(expected == result);
284 }
285
286 BOOST_AUTO_TEST_CASE(two)
287 {
288 std::string expected = "1:2";
289 std::string result = string_util::join({1, 2});
290
291 BOOST_TEST(expected == result);
292 }
293
294 BOOST_AUTO_TEST_CASE(delimiterString)
295 {
296 std::string expected = "1;;2;;3";
297 std::string result = string_util::join({1, 2, 3}, ";;");
298
299 BOOST_TEST(expected == result);
300 }
301
302 BOOST_AUTO_TEST_CASE(delimiterChar)
303 {
304 std::string expected = "1@2@3@4";
305 std::string result = string_util::join({1, 2, 3, 4}, '@');
306
307 BOOST_TEST(expected == result);
308 }
309
310 BOOST_AUTO_TEST_SUITE_END()
311
312 /*
313 * string_util::is_identifier function
314 * --------------------------------------------------------
315 */
316
317 BOOST_AUTO_TEST_SUITE(is_identifier_valid)
318
319 BOOST_AUTO_TEST_CASE(correct)
320 {
321 BOOST_TEST(string_util::is_identifier("localhost"));
322 BOOST_TEST(string_util::is_identifier("localhost2"));
323 BOOST_TEST(string_util::is_identifier("localhost2-4_"));
324 }
325
326 BOOST_AUTO_TEST_CASE(incorrect)
327 {
328 BOOST_TEST(!string_util::is_identifier(""));
329 BOOST_TEST(!string_util::is_identifier("localhost with spaces"));
330 BOOST_TEST(!string_util::is_identifier("localhost*"));
331 BOOST_TEST(!string_util::is_identifier("&&"));
332 BOOST_TEST(!string_util::is_identifier("@'"));
333 BOOST_TEST(!string_util::is_identifier("##"));
334 BOOST_TEST(!string_util::is_identifier("===++"));
335 }
336
337 BOOST_AUTO_TEST_SUITE_END()
338
339 /*
340 * string_util::is_boolean function
341 * --------------------------------------------------------
342 */
343
344 BOOST_AUTO_TEST_SUITE(is_boolean)
345
346 BOOST_AUTO_TEST_CASE(correct)
347 {
348 // true
349 BOOST_TEST(string_util::is_boolean("true"));
350 BOOST_TEST(string_util::is_boolean("True"));
351 BOOST_TEST(string_util::is_boolean("TRUE"));
352 BOOST_TEST(string_util::is_boolean("TruE"));
353
354 // yes
355 BOOST_TEST(string_util::is_boolean("yes"));
356 BOOST_TEST(string_util::is_boolean("Yes"));
357 BOOST_TEST(string_util::is_boolean("YES"));
358 BOOST_TEST(string_util::is_boolean("YeS"));
359
360 // on
361 BOOST_TEST(string_util::is_boolean("on"));
362 BOOST_TEST(string_util::is_boolean("On"));
363 BOOST_TEST(string_util::is_boolean("oN"));
364 BOOST_TEST(string_util::is_boolean("ON"));
365
366 // 1
367 BOOST_TEST(string_util::is_boolean("1"));
368 }
369
370 BOOST_AUTO_TEST_CASE(incorrect)
371 {
372 BOOST_TEST(!string_util::is_boolean("false"));
373 BOOST_TEST(!string_util::is_boolean("lol"));
374 BOOST_TEST(!string_util::is_boolean(""));
375 BOOST_TEST(!string_util::is_boolean("0"));
376 }
377
378 BOOST_AUTO_TEST_SUITE_END()
379
380 } // !irccd