comparison tests/util/main.cpp @ 207:6635b9187d71

Irccd: switch to 4 spaces indent, #518
author David Demelier <markand@malikania.fr>
date Tue, 21 Jun 2016 20:52:17 +0200
parents b39573fc066e
children c6fbb6e0e06d
comparison
equal deleted inserted replaced
206:11808e98218f 207:6635b9187d71
29 * util::format function 29 * util::format function
30 * -------------------------------------------------------- */ 30 * -------------------------------------------------------- */
31 31
32 TEST(Format, nothing) 32 TEST(Format, nothing)
33 { 33 {
34 std::string expected = "hello world!"; 34 std::string expected = "hello world!";
35 std::string result = util::format("hello world!"); 35 std::string result = util::format("hello world!");
36 36
37 ASSERT_EQ(expected, result); 37 ASSERT_EQ(expected, result);
38 } 38 }
39 39
40 TEST(Format, escape) 40 TEST(Format, escape)
41 { 41 {
42 util::Substitution params; 42 util::Substitution params;
43 43
44 params.keywords.emplace("target", "hello"); 44 params.keywords.emplace("target", "hello");
45 45
46 ASSERT_EQ("$@#", util::format("$@#")); 46 ASSERT_EQ("$@#", util::format("$@#"));
47 ASSERT_EQ(" $ @ # ", util::format(" $ @ # ")); 47 ASSERT_EQ(" $ @ # ", util::format(" $ @ # "));
48 ASSERT_EQ("#", util::format("#")); 48 ASSERT_EQ("#", util::format("#"));
49 ASSERT_EQ(" # ", util::format(" # ")); 49 ASSERT_EQ(" # ", util::format(" # "));
50 ASSERT_EQ("#@", util::format("#@")); 50 ASSERT_EQ("#@", util::format("#@"));
51 ASSERT_EQ("##", util::format("##")); 51 ASSERT_EQ("##", util::format("##"));
52 ASSERT_EQ("#!", util::format("#!")); 52 ASSERT_EQ("#!", util::format("#!"));
53 ASSERT_EQ("#{target}", util::format("##{target}")); 53 ASSERT_EQ("#{target}", util::format("##{target}"));
54 ASSERT_EQ("@hello", util::format("@#{target}", params)); 54 ASSERT_EQ("@hello", util::format("@#{target}", params));
55 ASSERT_EQ("hello#", util::format("#{target}#", params)); 55 ASSERT_EQ("hello#", util::format("#{target}#", params));
56 ASSERT_ANY_THROW(util::format("#{failure")); 56 ASSERT_ANY_THROW(util::format("#{failure"));
57 } 57 }
58 58
59 TEST(Format, disableDate) 59 TEST(Format, disableDate)
60 { 60 {
61 util::Substitution params; 61 util::Substitution params;
62 62
63 params.flags &= ~(util::Substitution::Date); 63 params.flags &= ~(util::Substitution::Date);
64 64
65 ASSERT_EQ("%H:%M", util::format("%H:%M", params)); 65 ASSERT_EQ("%H:%M", util::format("%H:%M", params));
66 } 66 }
67 67
68 TEST(Format, disableKeywords) 68 TEST(Format, disableKeywords)
69 { 69 {
70 util::Substitution params; 70 util::Substitution params;
71 71
72 params.keywords.emplace("target", "hello"); 72 params.keywords.emplace("target", "hello");
73 params.flags &= ~(util::Substitution::Keywords); 73 params.flags &= ~(util::Substitution::Keywords);
74 74
75 ASSERT_EQ("#{target}", util::format("#{target}", params)); 75 ASSERT_EQ("#{target}", util::format("#{target}", params));
76 } 76 }
77 77
78 TEST(Format, disableEnv) 78 TEST(Format, disableEnv)
79 { 79 {
80 util::Substitution params; 80 util::Substitution params;
81 81
82 params.flags &= ~(util::Substitution::Env); 82 params.flags &= ~(util::Substitution::Env);
83 83
84 ASSERT_EQ("${HOME}", util::format("${HOME}", params)); 84 ASSERT_EQ("${HOME}", util::format("${HOME}", params));
85 } 85 }
86 86
87 TEST(Format, keywordSimple) 87 TEST(Format, keywordSimple)
88 { 88 {
89 util::Substitution params; 89 util::Substitution params;
90 90
91 params.keywords.insert({"target", "irccd"}); 91 params.keywords.insert({"target", "irccd"});
92 92
93 std::string expected = "hello irccd!"; 93 std::string expected = "hello irccd!";
94 std::string result = util::format("hello #{target}!", params); 94 std::string result = util::format("hello #{target}!", params);
95 95
96 ASSERT_EQ(expected, result); 96 ASSERT_EQ(expected, result);
97 } 97 }
98 98
99 TEST(Format, keywordMultiple) 99 TEST(Format, keywordMultiple)
100 { 100 {
101 util::Substitution params; 101 util::Substitution params;
102 102
103 params.keywords.insert({"target", "irccd"}); 103 params.keywords.insert({"target", "irccd"});
104 params.keywords.insert({"source", "nightmare"}); 104 params.keywords.insert({"source", "nightmare"});
105 105
106 std::string expected = "hello irccd from nightmare!"; 106 std::string expected = "hello irccd from nightmare!";
107 std::string result = util::format("hello #{target} from #{source}!", params); 107 std::string result = util::format("hello #{target} from #{source}!", params);
108 108
109 ASSERT_EQ(expected, result); 109 ASSERT_EQ(expected, result);
110 } 110 }
111 111
112 TEST(Format, keywordAdjTwice) 112 TEST(Format, keywordAdjTwice)
113 { 113 {
114 util::Substitution params; 114 util::Substitution params;
115 115
116 params.keywords.insert({"target", "irccd"}); 116 params.keywords.insert({"target", "irccd"});
117 117
118 std::string expected = "hello irccdirccd!"; 118 std::string expected = "hello irccdirccd!";
119 std::string result = util::format("hello #{target}#{target}!", params); 119 std::string result = util::format("hello #{target}#{target}!", params);
120 120
121 ASSERT_EQ(expected, result); 121 ASSERT_EQ(expected, result);
122 } 122 }
123 123
124 TEST(Format, keywordMissing) 124 TEST(Format, keywordMissing)
125 { 125 {
126 std::string expected = "hello !"; 126 std::string expected = "hello !";
127 std::string result = util::format("hello #{target}!"); 127 std::string result = util::format("hello #{target}!");
128 128
129 ASSERT_EQ(expected, result); 129 ASSERT_EQ(expected, result);
130 } 130 }
131 131
132 TEST(Format, envSimple) 132 TEST(Format, envSimple)
133 { 133 {
134 std::string home = sys::env("HOME"); 134 std::string home = sys::env("HOME");
135 135
136 if (!home.empty()) { 136 if (!home.empty()) {
137 std::string expected = "my home is " + home; 137 std::string expected = "my home is " + home;
138 std::string result = util::format("my home is ${HOME}"); 138 std::string result = util::format("my home is ${HOME}");
139 139
140 ASSERT_EQ(expected, result); 140 ASSERT_EQ(expected, result);
141 } 141 }
142 } 142 }
143 143
144 TEST(Format, envMissing) 144 TEST(Format, envMissing)
145 { 145 {
146 std::string expected = "value is "; 146 std::string expected = "value is ";
147 std::string result = util::format("value is ${HOPE_THIS_VAR_NOT_EXIST}"); 147 std::string result = util::format("value is ${HOPE_THIS_VAR_NOT_EXIST}");
148 148
149 ASSERT_EQ(expected, result); 149 ASSERT_EQ(expected, result);
150 } 150 }
151 151
152 /* -------------------------------------------------------- 152 /* --------------------------------------------------------
153 * util::split function 153 * util::split function
154 * -------------------------------------------------------- */ 154 * -------------------------------------------------------- */
155 155
156 using List = std::vector<std::string>; 156 using List = std::vector<std::string>;
157 157
158 TEST(Split, simple) 158 TEST(Split, simple)
159 { 159 {
160 List expected { "a", "b" }; 160 List expected { "a", "b" };
161 List result = util::split("a;b", ";"); 161 List result = util::split("a;b", ";");
162 162
163 ASSERT_EQ(expected, result); 163 ASSERT_EQ(expected, result);
164 } 164 }
165 165
166 TEST(Split, cut) 166 TEST(Split, cut)
167 { 167 {
168 List expected { "msg", "#staff", "foo bar baz" }; 168 List expected { "msg", "#staff", "foo bar baz" };
169 List result = util::split("msg;#staff;foo bar baz", ";", 3); 169 List result = util::split("msg;#staff;foo bar baz", ";", 3);
170 170
171 ASSERT_EQ(expected, result); 171 ASSERT_EQ(expected, result);
172 } 172 }
173 173
174 /* -------------------------------------------------------- 174 /* --------------------------------------------------------
175 * util::strip function 175 * util::strip function
176 * -------------------------------------------------------- */ 176 * -------------------------------------------------------- */
177 177
178 TEST(Strip, left) 178 TEST(Strip, left)
179 { 179 {
180 std::string value = " 123"; 180 std::string value = " 123";
181 std::string result = util::strip(value); 181 std::string result = util::strip(value);
182 182
183 ASSERT_EQ("123", result); 183 ASSERT_EQ("123", result);
184 } 184 }
185 185
186 TEST(Strip, right) 186 TEST(Strip, right)
187 { 187 {
188 std::string value = "123 "; 188 std::string value = "123 ";
189 std::string result = util::strip(value); 189 std::string result = util::strip(value);
190 190
191 ASSERT_EQ("123", result); 191 ASSERT_EQ("123", result);
192 } 192 }
193 193
194 TEST(Strip, both) 194 TEST(Strip, both)
195 { 195 {
196 std::string value = " 123 "; 196 std::string value = " 123 ";
197 std::string result = util::strip(value); 197 std::string result = util::strip(value);
198 198
199 ASSERT_EQ("123", result); 199 ASSERT_EQ("123", result);
200 } 200 }
201 201
202 TEST(Strip, none) 202 TEST(Strip, none)
203 { 203 {
204 std::string value = "without"; 204 std::string value = "without";
205 std::string result = util::strip(value); 205 std::string result = util::strip(value);
206 206
207 ASSERT_EQ("without", result); 207 ASSERT_EQ("without", result);
208 } 208 }
209 209
210 TEST(Strip, betweenEmpty) 210 TEST(Strip, betweenEmpty)
211 { 211 {
212 std::string value = "one list"; 212 std::string value = "one list";
213 std::string result = util::strip(value); 213 std::string result = util::strip(value);
214 214
215 ASSERT_EQ("one list", result); 215 ASSERT_EQ("one list", result);
216 } 216 }
217 217
218 TEST(Strip, betweenLeft) 218 TEST(Strip, betweenLeft)
219 { 219 {
220 std::string value = " space at left"; 220 std::string value = " space at left";
221 std::string result = util::strip(value); 221 std::string result = util::strip(value);
222 222
223 ASSERT_EQ("space at left", result); 223 ASSERT_EQ("space at left", result);
224 } 224 }
225 225
226 TEST(Strip, betweenRight) 226 TEST(Strip, betweenRight)
227 { 227 {
228 std::string value = "space at right "; 228 std::string value = "space at right ";
229 std::string result = util::strip(value); 229 std::string result = util::strip(value);
230 230
231 ASSERT_EQ("space at right", result); 231 ASSERT_EQ("space at right", result);
232 } 232 }
233 233
234 TEST(Strip, betweenBoth) 234 TEST(Strip, betweenBoth)
235 { 235 {
236 std::string value = " space at both "; 236 std::string value = " space at both ";
237 std::string result = util::strip(value); 237 std::string result = util::strip(value);
238 238
239 ASSERT_EQ("space at both", result); 239 ASSERT_EQ("space at both", result);
240 } 240 }
241 241
242 TEST(Strip, empty) 242 TEST(Strip, empty)
243 { 243 {
244 std::string value = " "; 244 std::string value = " ";
245 std::string result = util::strip(value); 245 std::string result = util::strip(value);
246 246
247 ASSERT_EQ("", result); 247 ASSERT_EQ("", result);
248 } 248 }
249 249
250 /* -------------------------------------------------------- 250 /* --------------------------------------------------------
251 * util::join function 251 * util::join function
252 * -------------------------------------------------------- */ 252 * -------------------------------------------------------- */
253 253
254 TEST(Join, empty) 254 TEST(Join, empty)
255 { 255 {
256 std::string expected = ""; 256 std::string expected = "";
257 std::string result = util::join<int>({}); 257 std::string result = util::join<int>({});
258 258
259 ASSERT_EQ(expected, result); 259 ASSERT_EQ(expected, result);
260 } 260 }
261 261
262 TEST(Join, one) 262 TEST(Join, one)
263 { 263 {
264 std::string expected = "1"; 264 std::string expected = "1";
265 std::string result = util::join({1}); 265 std::string result = util::join({1});
266 266
267 ASSERT_EQ(expected, result); 267 ASSERT_EQ(expected, result);
268 } 268 }
269 269
270 TEST(Join, two) 270 TEST(Join, two)
271 { 271 {
272 std::string expected = "1:2"; 272 std::string expected = "1:2";
273 std::string result = util::join({1, 2}); 273 std::string result = util::join({1, 2});
274 274
275 ASSERT_EQ(expected, result); 275 ASSERT_EQ(expected, result);
276 } 276 }
277 277
278 TEST(Join, delimiterString) 278 TEST(Join, delimiterString)
279 { 279 {
280 std::string expected = "1;;2;;3"; 280 std::string expected = "1;;2;;3";
281 std::string result = util::join({1, 2, 3}, ";;"); 281 std::string result = util::join({1, 2, 3}, ";;");
282 282
283 ASSERT_EQ(expected, result); 283 ASSERT_EQ(expected, result);
284 } 284 }
285 285
286 TEST(Join, delimiterChar) 286 TEST(Join, delimiterChar)
287 { 287 {
288 std::string expected = "1@2@3@4"; 288 std::string expected = "1@2@3@4";
289 std::string result = util::join({1, 2, 3, 4}, '@'); 289 std::string result = util::join({1, 2, 3, 4}, '@');
290 290
291 ASSERT_EQ(expected, result); 291 ASSERT_EQ(expected, result);
292 } 292 }
293 293
294 /* -------------------------------------------------------- 294 /* --------------------------------------------------------
295 * util::isIdentifierValid function 295 * util::isIdentifierValid function
296 * -------------------------------------------------------- */ 296 * -------------------------------------------------------- */
297 297
298 TEST(IsIdentifierValid, correct) 298 TEST(IsIdentifierValid, correct)
299 { 299 {
300 ASSERT_TRUE(util::isIdentifierValid("localhost")); 300 ASSERT_TRUE(util::isIdentifierValid("localhost"));
301 ASSERT_TRUE(util::isIdentifierValid("localhost2")); 301 ASSERT_TRUE(util::isIdentifierValid("localhost2"));
302 ASSERT_TRUE(util::isIdentifierValid("localhost2-4_")); 302 ASSERT_TRUE(util::isIdentifierValid("localhost2-4_"));
303 } 303 }
304 304
305 TEST(IsIdentifierValid, incorrect) 305 TEST(IsIdentifierValid, incorrect)
306 { 306 {
307 ASSERT_FALSE(util::isIdentifierValid("")); 307 ASSERT_FALSE(util::isIdentifierValid(""));
308 ASSERT_FALSE(util::isIdentifierValid("localhost with spaces")); 308 ASSERT_FALSE(util::isIdentifierValid("localhost with spaces"));
309 ASSERT_FALSE(util::isIdentifierValid("localhost*")); 309 ASSERT_FALSE(util::isIdentifierValid("localhost*"));
310 ASSERT_FALSE(util::isIdentifierValid("&&")); 310 ASSERT_FALSE(util::isIdentifierValid("&&"));
311 ASSERT_FALSE(util::isIdentifierValid("@'")); 311 ASSERT_FALSE(util::isIdentifierValid("@'"));
312 ASSERT_FALSE(util::isIdentifierValid("##")); 312 ASSERT_FALSE(util::isIdentifierValid("##"));
313 ASSERT_FALSE(util::isIdentifierValid("===++")); 313 ASSERT_FALSE(util::isIdentifierValid("===++"));
314 } 314 }
315 315
316 /* -------------------------------------------------------- 316 /* --------------------------------------------------------
317 * util::isBoolean function 317 * util::isBoolean function
318 * -------------------------------------------------------- */ 318 * -------------------------------------------------------- */
319 319
320 TEST(IsBoolean, correct) 320 TEST(IsBoolean, correct)
321 { 321 {
322 // true 322 // true
323 ASSERT_TRUE(util::isBoolean("true")); 323 ASSERT_TRUE(util::isBoolean("true"));
324 ASSERT_TRUE(util::isBoolean("True")); 324 ASSERT_TRUE(util::isBoolean("True"));
325 ASSERT_TRUE(util::isBoolean("TRUE")); 325 ASSERT_TRUE(util::isBoolean("TRUE"));
326 ASSERT_TRUE(util::isBoolean("TruE")); 326 ASSERT_TRUE(util::isBoolean("TruE"));
327 327
328 // yes 328 // yes
329 ASSERT_TRUE(util::isBoolean("yes")); 329 ASSERT_TRUE(util::isBoolean("yes"));
330 ASSERT_TRUE(util::isBoolean("Yes")); 330 ASSERT_TRUE(util::isBoolean("Yes"));
331 ASSERT_TRUE(util::isBoolean("YES")); 331 ASSERT_TRUE(util::isBoolean("YES"));
332 ASSERT_TRUE(util::isBoolean("YeS")); 332 ASSERT_TRUE(util::isBoolean("YeS"));
333 333
334 // on 334 // on
335 ASSERT_TRUE(util::isBoolean("on")); 335 ASSERT_TRUE(util::isBoolean("on"));
336 ASSERT_TRUE(util::isBoolean("On")); 336 ASSERT_TRUE(util::isBoolean("On"));
337 ASSERT_TRUE(util::isBoolean("oN")); 337 ASSERT_TRUE(util::isBoolean("oN"));
338 ASSERT_TRUE(util::isBoolean("ON")); 338 ASSERT_TRUE(util::isBoolean("ON"));
339 339
340 // 1 340 // 1
341 ASSERT_TRUE(util::isBoolean("1")); 341 ASSERT_TRUE(util::isBoolean("1"));
342 } 342 }
343 343
344 TEST(IsBoolean, incorrect) 344 TEST(IsBoolean, incorrect)
345 { 345 {
346 ASSERT_FALSE(util::isBoolean("false")); 346 ASSERT_FALSE(util::isBoolean("false"));
347 ASSERT_FALSE(util::isBoolean("lol")); 347 ASSERT_FALSE(util::isBoolean("lol"));
348 ASSERT_FALSE(util::isBoolean("")); 348 ASSERT_FALSE(util::isBoolean(""));
349 ASSERT_FALSE(util::isBoolean("0")); 349 ASSERT_FALSE(util::isBoolean("0"));
350 } 350 }
351 351
352 /* -------------------------------------------------------- 352 /* --------------------------------------------------------
353 * util::isNumber function 353 * util::isNumber function
354 * -------------------------------------------------------- */ 354 * -------------------------------------------------------- */
355 355
356 TEST(IsNumber, correct) 356 TEST(IsNumber, correct)
357 { 357 {
358 ASSERT_TRUE(util::isNumber("123")); 358 ASSERT_TRUE(util::isNumber("123"));
359 ASSERT_TRUE(util::isNumber("-123")); 359 ASSERT_TRUE(util::isNumber("-123"));
360 ASSERT_TRUE(util::isNumber("123.67")); 360 ASSERT_TRUE(util::isNumber("123.67"));
361 } 361 }
362 362
363 TEST(IsNumber, incorrect) 363 TEST(IsNumber, incorrect)
364 { 364 {
365 ASSERT_FALSE(util::isNumber("lol")); 365 ASSERT_FALSE(util::isNumber("lol"));
366 ASSERT_FALSE(util::isNumber("this is not a number")); 366 ASSERT_FALSE(util::isNumber("this is not a number"));
367 } 367 }
368 368
369 /* 369 /*
370 * util::toNumber function 370 * util::toNumber function
371 * ------------------------------------------------------------------ 371 * ------------------------------------------------------------------
372 */ 372 */
373 373
374 TEST(ToNumber, correct) 374 TEST(ToNumber, correct)
375 { 375 {
376 /* unsigned */ 376 /* unsigned */
377 ASSERT_EQ(50u, util::toNumber<std::uint8_t>("50")); 377 ASSERT_EQ(50u, util::toNumber<std::uint8_t>("50"));
378 ASSERT_EQ(5000u, util::toNumber<std::uint16_t>("5000")); 378 ASSERT_EQ(5000u, util::toNumber<std::uint16_t>("5000"));
379 ASSERT_EQ(50000u, util::toNumber<std::uint32_t>("50000")); 379 ASSERT_EQ(50000u, util::toNumber<std::uint32_t>("50000"));
380 ASSERT_EQ(500000u, util::toNumber<std::uint64_t>("500000")); 380 ASSERT_EQ(500000u, util::toNumber<std::uint64_t>("500000"));
381 381
382 /* signed */ 382 /* signed */
383 ASSERT_EQ(-50, util::toNumber<std::int8_t>("-50")); 383 ASSERT_EQ(-50, util::toNumber<std::int8_t>("-50"));
384 ASSERT_EQ(-500, util::toNumber<std::int16_t>("-500")); 384 ASSERT_EQ(-500, util::toNumber<std::int16_t>("-500"));
385 ASSERT_EQ(-5000, util::toNumber<std::int32_t>("-5000")); 385 ASSERT_EQ(-5000, util::toNumber<std::int32_t>("-5000"));
386 ASSERT_EQ(-50000, util::toNumber<std::int64_t>("-50000")); 386 ASSERT_EQ(-50000, util::toNumber<std::int64_t>("-50000"));
387 } 387 }
388 388
389 TEST(ToNumber, incorrect) 389 TEST(ToNumber, incorrect)
390 { 390 {
391 /* unsigned */ 391 /* unsigned */
392 ASSERT_THROW(util::toNumber<std::uint8_t>("300"), std::out_of_range); 392 ASSERT_THROW(util::toNumber<std::uint8_t>("300"), std::out_of_range);
393 ASSERT_THROW(util::toNumber<std::uint16_t>("80000"), std::out_of_range); 393 ASSERT_THROW(util::toNumber<std::uint16_t>("80000"), std::out_of_range);
394 ASSERT_THROW(util::toNumber<std::uint8_t>("-125"), std::out_of_range); 394 ASSERT_THROW(util::toNumber<std::uint8_t>("-125"), std::out_of_range);
395 ASSERT_THROW(util::toNumber<std::uint16_t>("-25000"), std::out_of_range); 395 ASSERT_THROW(util::toNumber<std::uint16_t>("-25000"), std::out_of_range);
396 396
397 /* signed */ 397 /* signed */
398 ASSERT_THROW(util::toNumber<std::int8_t>("300"), std::out_of_range); 398 ASSERT_THROW(util::toNumber<std::int8_t>("300"), std::out_of_range);
399 ASSERT_THROW(util::toNumber<std::int16_t>("80000"), std::out_of_range); 399 ASSERT_THROW(util::toNumber<std::int16_t>("80000"), std::out_of_range);
400 ASSERT_THROW(util::toNumber<std::int8_t>("-300"), std::out_of_range); 400 ASSERT_THROW(util::toNumber<std::int8_t>("-300"), std::out_of_range);
401 ASSERT_THROW(util::toNumber<std::int16_t>("-80000"), std::out_of_range); 401 ASSERT_THROW(util::toNumber<std::int16_t>("-80000"), std::out_of_range);
402 402
403 /* not numbers */ 403 /* not numbers */
404 ASSERT_THROW(util::toNumber<std::uint8_t>("nonono"), std::invalid_argument); 404 ASSERT_THROW(util::toNumber<std::uint8_t>("nonono"), std::invalid_argument);
405 405
406 /* custom ranges */ 406 /* custom ranges */
407 ASSERT_THROW(util::toNumber<std::uint8_t>("50", 0, 10), std::out_of_range); 407 ASSERT_THROW(util::toNumber<std::uint8_t>("50", 0, 10), std::out_of_range);
408 ASSERT_THROW(util::toNumber<std::int8_t>("-50", -10, 10), std::out_of_range); 408 ASSERT_THROW(util::toNumber<std::int8_t>("-50", -10, 10), std::out_of_range);
409 } 409 }
410 410
411 } // !irccd 411 } // !irccd
412 412
413 int main(int argc, char **argv) 413 int main(int argc, char **argv)
414 { 414 {
415 testing::InitGoogleTest(&argc, argv); 415 testing::InitGoogleTest(&argc, argv);
416 416
417 return RUN_ALL_TESTS(); 417 return RUN_ALL_TESTS();
418 } 418 }