comparison tests/util/main.cpp @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children f8160d515a76
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 /*
2 * main.cpp -- test util functions
3 *
4 * Copyright (c) 2013-2016 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 #include <gtest/gtest.h>
20
21 #include <util.h>
22 #include <system.h>
23
24 namespace irccd {
25
26 /* --------------------------------------------------------
27 * util::format function
28 * -------------------------------------------------------- */
29
30 TEST(Format, nothing)
31 {
32 std::string expected = "hello world!";
33 std::string result = util::format("hello world!");
34
35 ASSERT_EQ(expected, result);
36 }
37
38 TEST(Format, keywordSimple)
39 {
40 util::Substitution params;
41
42 params.keywords.insert({"target", "irccd"});
43
44 std::string expected = "hello irccd!";
45 std::string result = util::format("hello #{target}!", params);
46
47 ASSERT_EQ(expected, result);
48 }
49
50 TEST(Format, keywordMultiple)
51 {
52 util::Substitution params;
53
54 params.keywords.insert({"target", "irccd"});
55 params.keywords.insert({"source", "nightmare"});
56
57 std::string expected = "hello irccd from nightmare!";
58 std::string result = util::format("hello #{target} from #{source}!", params);
59
60 ASSERT_EQ(expected, result);
61 }
62
63 TEST(Format, keywordAdjTwice)
64 {
65 util::Substitution params;
66
67 params.keywords.insert({"target", "irccd"});
68
69 std::string expected = "hello irccdirccd!";
70 std::string result = util::format("hello #{target}#{target}!", params);
71
72 ASSERT_EQ(expected, result);
73 }
74
75 TEST(Format, keywordMissing)
76 {
77 std::string expected = "hello !";
78 std::string result = util::format("hello #{target}!");
79
80 ASSERT_EQ(expected, result);
81 }
82
83 TEST(Format, envSimple)
84 {
85 std::string home = sys::env("HOME");
86
87 if (!home.empty()) {
88 std::string expected = "my home is " + home;
89 std::string result = util::format("my home is ${HOME}");
90
91 ASSERT_EQ(expected, result);
92 }
93 }
94
95 TEST(Format, envMissing)
96 {
97 std::string expected = "value is ";
98 std::string result = util::format("value is ${HOPE_THIS_VAR_NOT_EXIST}");
99
100 ASSERT_EQ(expected, result);
101 }
102
103 /* --------------------------------------------------------
104 * util::split function
105 * -------------------------------------------------------- */
106
107 using List = std::vector<std::string>;
108
109 TEST(Split, simple)
110 {
111 List expected { "a", "b" };
112 List result = util::split("a;b", ";");
113
114 ASSERT_EQ(expected, result);
115 }
116
117 TEST(Split, cut)
118 {
119 List expected { "msg", "#staff", "foo bar baz" };
120 List result = util::split("msg;#staff;foo bar baz", ";", 3);
121
122 ASSERT_EQ(expected, result);
123 }
124
125 /* --------------------------------------------------------
126 * util::strip function
127 * -------------------------------------------------------- */
128
129 TEST(Strip, left)
130 {
131 std::string value = " 123";
132 std::string result = util::strip(value);
133
134 ASSERT_EQ("123", result);
135 }
136
137 TEST(Strip, right)
138 {
139 std::string value = "123 ";
140 std::string result = util::strip(value);
141
142 ASSERT_EQ("123", result);
143 }
144
145 TEST(Strip, both)
146 {
147 std::string value = " 123 ";
148 std::string result = util::strip(value);
149
150 ASSERT_EQ("123", result);
151 }
152
153 TEST(Strip, none)
154 {
155 std::string value = "without";
156 std::string result = util::strip(value);
157
158 ASSERT_EQ("without", result);
159 }
160
161 TEST(Strip, betweenEmpty)
162 {
163 std::string value = "one list";
164 std::string result = util::strip(value);
165
166 ASSERT_EQ("one list", result);
167 }
168
169 TEST(Strip, betweenLeft)
170 {
171 std::string value = " space at left";
172 std::string result = util::strip(value);
173
174 ASSERT_EQ("space at left", result);
175 }
176
177 TEST(Strip, betweenRight)
178 {
179 std::string value = "space at right ";
180 std::string result = util::strip(value);
181
182 ASSERT_EQ("space at right", result);
183 }
184
185 TEST(Strip, betweenBoth)
186 {
187 std::string value = " space at both ";
188 std::string result = util::strip(value);
189
190 ASSERT_EQ("space at both", result);
191 }
192
193 TEST(Strip, empty)
194 {
195 std::string value = " ";
196 std::string result = util::strip(value);
197
198 ASSERT_EQ("", result);
199 }
200
201 /* --------------------------------------------------------
202 * util::join function
203 * -------------------------------------------------------- */
204
205 TEST(Join, empty)
206 {
207 std::string expected = "";
208 std::string result = util::join<int>({});
209
210 ASSERT_EQ(expected, result);
211 }
212
213 TEST(Join, one)
214 {
215 std::string expected = "1";
216 std::string result = util::join({1});
217
218 ASSERT_EQ(expected, result);
219 }
220
221 TEST(Join, two)
222 {
223 std::string expected = "1:2";
224 std::string result = util::join({1, 2});
225
226 ASSERT_EQ(expected, result);
227 }
228
229 TEST(Join, delimiterString)
230 {
231 std::string expected = "1;;2;;3";
232 std::string result = util::join({1, 2, 3}, ";;");
233
234 ASSERT_EQ(expected, result);
235 }
236
237 TEST(Join, delimiterChar)
238 {
239 std::string expected = "1@2@3@4";
240 std::string result = util::join({1, 2, 3, 4}, '@');
241
242 ASSERT_EQ(expected, result);
243 }
244
245 /* --------------------------------------------------------
246 * util::isIdentifierValid function
247 * -------------------------------------------------------- */
248
249 TEST(IsIdentifierValid, correct)
250 {
251 ASSERT_TRUE(util::isIdentifierValid("localhost"));
252 ASSERT_TRUE(util::isIdentifierValid("localhost2"));
253 ASSERT_TRUE(util::isIdentifierValid("localhost2-4_"));
254 }
255
256 TEST(IsIdentifierValid, incorrect)
257 {
258 ASSERT_FALSE(util::isIdentifierValid(""));
259 ASSERT_FALSE(util::isIdentifierValid("localhost with spaces"));
260 ASSERT_FALSE(util::isIdentifierValid("localhost*"));
261 ASSERT_FALSE(util::isIdentifierValid("&&"));
262 ASSERT_FALSE(util::isIdentifierValid("@'"));
263 ASSERT_FALSE(util::isIdentifierValid("##"));
264 ASSERT_FALSE(util::isIdentifierValid("===++"));
265 }
266
267 /* --------------------------------------------------------
268 * util::isBoolean function
269 * -------------------------------------------------------- */
270
271 TEST(IsBoolean, correct)
272 {
273 // true
274 ASSERT_TRUE(util::isBoolean("true"));
275 ASSERT_TRUE(util::isBoolean("True"));
276 ASSERT_TRUE(util::isBoolean("TRUE"));
277 ASSERT_TRUE(util::isBoolean("TruE"));
278
279 // yes
280 ASSERT_TRUE(util::isBoolean("yes"));
281 ASSERT_TRUE(util::isBoolean("Yes"));
282 ASSERT_TRUE(util::isBoolean("YES"));
283 ASSERT_TRUE(util::isBoolean("YeS"));
284
285 // on
286 ASSERT_TRUE(util::isBoolean("on"));
287 ASSERT_TRUE(util::isBoolean("On"));
288 ASSERT_TRUE(util::isBoolean("oN"));
289 ASSERT_TRUE(util::isBoolean("ON"));
290
291 // 1
292 ASSERT_TRUE(util::isBoolean("1"));
293 }
294
295 TEST(IsBoolean, incorrect)
296 {
297 ASSERT_FALSE(util::isBoolean("false"));
298 ASSERT_FALSE(util::isBoolean("lol"));
299 ASSERT_FALSE(util::isBoolean(""));
300 ASSERT_FALSE(util::isBoolean("0"));
301 }
302
303 /* --------------------------------------------------------
304 * util::isNumber function
305 * -------------------------------------------------------- */
306
307 TEST(IsNumber, correct)
308 {
309 ASSERT_TRUE(util::isNumber("123"));
310 ASSERT_TRUE(util::isNumber("-123"));
311 ASSERT_TRUE(util::isNumber("123.67"));
312 }
313
314 TEST(IsNumber, incorrect)
315 {
316 ASSERT_FALSE(util::isNumber("lol"));
317 ASSERT_FALSE(util::isNumber("this is not a number"));
318 }
319
320 } // !irccd
321
322 int main(int argc, char **argv)
323 {
324 testing::InitGoogleTest(&argc, argv);
325
326 return RUN_ALL_TESTS();
327 }