forked from stein197/lua-string
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
509 lines (420 loc) · 15.5 KB
/
test.lua
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
require "."
local luaunit = require "luaunit"
local a = "a"
local abc = "abc"
local abcdef = "abcdef"
local empty = ""
local space = " "
local blank = " "
local whitespaced = " a "
local ae = luaunit.assertEquals
local an = luaunit.assertNil
local at = luaunit.assertTrue
local af = luaunit.assertFalse
TestString = {
["test: split(): Splitting empty string with empty one returns empty table"] = function()
ae(empty:split(empty), {})
end;
["test: split(): Splitting by empty string returns table of chars"] = function ()
ae(abc:split(empty), {"a", "b", "c"})
end;
["test: split(): Default"] = function ()
ae(("a b c"):split(space), {"a", "b", "c"})
end;
["test: split(): Splitting by multicharacter string"] = function ()
ae(("a, b, c"):split(", "), {"a", "b", "c"})
end;
["test: split(): Splitting repeating separators results in empty strings"] = function ()
ae(("a-b--c"):split("-"), {"a", "b", "", "c"})
end;
["test: split(): Splitting separator returns empty strings"] = function ()
ae(("-"):split("-"), {"", ""})
ae(("--"):split("-"), {"", "", ""})
end;
["test: split(): Regex: Splitting empty string with empty one returns empty table"] = function()
ae(empty:split(empty, true), {})
end;
["test: split(): Regex: Splitting by empty string returns table of chars"] = function ()
ae(abc:split(empty, true), {"a", "b", "c"})
end;
["test: split(): Regex: Default"] = function ()
ae(("a b c"):split("%s", true), {"a", "b", "c"})
end;
["test: split(): Regex: Splitting by multicharacter string"] = function ()
ae(("a, b,c"):split("%s*,%s*", true), {"a", "b", "c"})
end;
["test: split(): Regex: Splitting repeating separators results in empty strings"] = function ()
ae(("a-b--c"):split("%-+", true), {"a", "b", "c"})
end;
["test: trim(): Trimming empty string returns empty one"] = function ()
ae(empty:trim(), "")
end;
["test: trim(): Trimming blank string returns empty one"] = function ()
ae(blank:trim(), "")
end;
["test: trim(): Default"] = function ()
ae(whitespaced:trim(), "a")
end;
["test: trim(): Trimming specified char at single side"] = function ()
ae(abc:trim("c"), "ab")
end;
["test: trim(): Trimming specified char"] = function ()
ae(("abcba"):trim("a"), "bcb")
end;
["test: trim(): Trimming group of chars"] = function ()
ae(("/path/?"):trim("/%?"), "path")
end;
["test: trimstart(): Trimming empty string returns empty one"] = function ()
ae(empty:trimstart(), "")
end;
["test: trimstart(): Trimming blank string returns empty one"] = function ()
ae(blank:trimstart(), "")
end;
["test: trimstart(): Default"] = function ()
ae(whitespaced:trimstart(), "a ")
end;
["test: trimstart(): Trimming specified char at single side"] = function ()
ae(abc:trimstart("a"), "bc")
end;
["test: trimstart(): Trimming group of chars"] = function ()
ae(abcdef:trimstart("ba%s"), "cdef")
end;
["test: trimend(): Trimming empty string returns empty one"] = function ()
ae(empty:trimend(), "")
end;
["test: trimend(): Trimming blank string returns empty one"] = function ()
ae(blank:trimend(), "")
end;
["test: trimend(): Default"] = function ()
ae(whitespaced:trimend(), " a")
end;
["test: trimend(): Trimming specified char at single side"] = function ()
ae(abc:trimend("c"), "ab")
end;
["test: trimend(): Trimming group of chars"] = function ()
ae(abcdef:trimend("fe%s"), "abcd")
end;
["test: padstart(): Padding an empty string returns pad string"] = function ()
ae(empty:padstart(3, "0"), "000")
end;
["test: padstart(): Default"] = function ()
ae(a:padstart(3, "0"), "00a")
end;
["test: padstart(): Padding to length less or equal than string's one returns string"] = function ()
ae(abc:padstart(1, "0"), "abc")
ae(abc:padstart(3, "0"), "abc")
end;
["test: padstart(): Padding with multicharacter string trims trailing characters"] = function ()
ae(abcdef:padstart(6, "12"), "abcdef")
ae(abcdef:padstart(7, "12"), "2abcdef")
ae(abcdef:padstart(8, "12"), "12abcdef")
ae(abcdef:padstart(9, "12"), "212abcdef")
ae(abcdef:padstart(10, "12"), "1212abcdef")
ae(abcdef:padstart(13, "123"), "3123123abcdef")
ae(abcdef:padstart(14, "123"), "23123123abcdef")
end;
["test: padend(): Padding an empty string returns pad string"] = function ()
ae(empty:padend(3, "0"), "000")
end;
["test: padend(): Default"] = function ()
ae(a:padend(3, "0"), "a00")
end;
["test: padend(): Padding to length less or equal than string's one returns string"] = function ()
ae(abc:padend(1, "0"), "abc")
ae(abc:padend(3, "0"), "abc")
end;
["test: padend(): Padding with multicharacter string trims trailing characters"] = function ()
ae(abcdef:padend(6, "12"), "abcdef")
ae(abcdef:padend(7, "12"), "abcdef1")
ae(abcdef:padend(8, "12"), "abcdef12")
ae(abcdef:padend(9, "12"), "abcdef121")
ae(abcdef:padend(10, "12"), "abcdef1212")
ae(abcdef:padend(13, "123"), "abcdef1231231")
ae(abcdef:padend(14, "123"), "abcdef12312312")
end;
["test: ensurestart(): Ensuring empty string returns prefix"] = function ()
ae(empty:ensurestart("/"), "/")
ae(empty:ensurestart("path/"), "path/")
end;
["test: ensurestart(): Ensuring with empty string returns the string"] = function ()
ae(a:ensurestart(""), "a")
ae(abc:ensurestart(""), "abc")
end;
["test: ensurestart(): Ensuring with single char"] = function ()
ae(a:ensurestart("/"), "/a")
ae(("/a"):ensurestart("/"), "/a")
ae(abc:ensurestart("/"), "/abc")
ae(("/abc"):ensurestart("/"), "/abc")
end;
["test: ensurestart(): Ensuring with string"] = function ()
ae(("def"):ensurestart("abc"), "abcdef")
ae(("cdef"):ensurestart("abc"), "abcdef")
ae(("abcdef"):ensurestart("abc"), "abcdef")
end;
["test: ensurestart(): Ensuring with prefix larger than the string returns prefix"] = function ()
ae(("c"):ensurestart("bc"), "bc")
ae(("def"):ensurestart("abcdef"), "abcdef")
end;
["test: ensurestart(): Ensuring with prefix larger than the string and partially matches the string"] = function ()
ae(("defghi"):ensurestart("abcdefg"), "abcdefghi")
end;
["test: ensurestart(): Ensuring with prefix larger than the string and does not match the string"] = function ()
ae(abc:ensurestart("defghi"), "defghiabc")
end;
["test: ensureend(): Ensuring empty string returns suffix"] = function ()
ae(empty:ensureend("/"), "/")
ae(empty:ensureend("path/"), "path/")
end;
["test: ensureend(): Ensuring with empty string returns the string"] = function ()
ae(a:ensureend(""), "a")
ae(abc:ensureend(""), "abc")
end;
["test: ensureend(): Ensuring with single char"] = function ()
ae(a:ensureend("/"), "a/")
ae(("a/"):ensureend("/"), "a/")
ae(abc:ensureend("/"), "abc/")
ae(("abc/"):ensureend("/"), "abc/")
end;
["test: ensureend(): Ensuring with string"] = function ()
ae(abc:ensureend("def"), "abcdef")
ae(abcdef:ensureend("def"), "abcdef")
end;
["test: ensureend(): Ensuring with suffix larger than the string returns suffix"] = function ()
ae(a:ensureend("abc"), "abc")
ae(abc:ensureend("abcdef"), "abcdef")
end;
["test: ensureend(): Ensuring with suffix larger than the string and partially matches the string"] = function ()
ae(("abcdef"):ensureend("cdefghi"), "abcdefghi")
end;
["test: ensureend(): Ensuring with suffix larger than the string and does not match the string"] = function ()
ae(abc:ensureend("defghi"), "abcdefghi")
end;
["test: esc(): Escaping empty string returns empty one"] = function ()
ae(empty:esc(), "")
end;
["test: esc(): Escaping regular string returns the string itself"] = function ()
ae(abc:esc(), "abc")
end;
["test: esc(): Escaping quotes"] = function ()
ae(("abc'"):esc(), "abc\\'")
ae(("abc\""):esc(), "abc\\\"")
end;
["test: esc(): Double escaping"] = function ()
ae(("abc'"):esc():esc(), "abc\\\\\\'")
end;
["test: esc(): Escaping single backslash"] = function ()
ae(("\\"):esc(), "\\\\")
end;
["test: unesc(): Unescaping empty string returns empty one"] = function ()
ae(empty:unesc(), "")
end;
["test: unesc(): Unescaping regular string returns the string itself"] = function ()
ae(abc:unesc(), "abc")
end;
["test: unesc(): Unescaping quotes returns the string itself"] = function ()
ae(("'"):unesc(), "'")
ae(("\""):unesc(), "\"")
ae(("abc'"):unesc(), "abc'")
end;
["test: unesc(): Default"] = function ()
ae(("abc\\'"):unesc(), "abc'")
ae(("abc\\\\\\'"):unesc():unesc(), "abc'")
ae(("\\"):unesc(), "")
ae(("\\\\"):unesc(), "\\")
ae(("abc\\"):unesc(), "abc")
end;
["test: unesc(): Unescaping escaped string returns itself"] = function ()
ae(("a\\bc'"):esc():unesc(), "a\\bc'")
end;
["test: escregex()"] = function ()
luaunit.assertEquals((""):escregex(), "")
luaunit.assertEquals(("."):escregex(), "%.")
luaunit.assertEquals(("%a"):escregex(), "%%a")
luaunit.assertEquals(("%c"):escregex(), "%%c")
luaunit.assertEquals(("%d"):escregex(), "%%d")
luaunit.assertEquals(("%g"):escregex(), "%%g")
luaunit.assertEquals(("%l"):escregex(), "%%l")
luaunit.assertEquals(("%p"):escregex(), "%%p")
luaunit.assertEquals(("%s"):escregex(), "%%s")
luaunit.assertEquals(("%u"):escregex(), "%%u")
luaunit.assertEquals(("%w"):escregex(), "%%w")
luaunit.assertEquals(("%x"):escregex(), "%%x")
luaunit.assertEquals(("("):escregex(), "%(")
luaunit.assertEquals((")"):escregex(), "%)")
luaunit.assertEquals(("."):escregex(), "%.")
luaunit.assertEquals(("%"):escregex(), "%%")
luaunit.assertEquals(("%"):escregex():escregex(), "%%%%")
luaunit.assertEquals(("+"):escregex(), "%+")
luaunit.assertEquals(("-"):escregex(), "%-")
luaunit.assertEquals(("*"):escregex(), "%*")
luaunit.assertEquals(("?"):escregex(), "%?")
luaunit.assertEquals(("["):escregex(), "%[")
luaunit.assertEquals(("]"):escregex(), "%]")
luaunit.assertEquals(("^"):escregex(), "%^")
luaunit.assertEquals(("$"):escregex(), "%$")
luaunit.assertEquals((".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$"):escregex(), "%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$")
end;
["test: unescregex()"] = function ()
luaunit.assertEquals((""):unescregex(), "")
luaunit.assertEquals(("%."):unescregex(), ".")
luaunit.assertEquals(("%%a"):unescregex(), "%a")
luaunit.assertEquals(("%%c"):unescregex(), "%c")
luaunit.assertEquals(("%%d"):unescregex(), "%d")
luaunit.assertEquals(("%%g"):unescregex(), "%g")
luaunit.assertEquals(("%%l"):unescregex(), "%l")
luaunit.assertEquals(("%%p"):unescregex(), "%p")
luaunit.assertEquals(("%%s"):unescregex(), "%s")
luaunit.assertEquals(("%%u"):unescregex(), "%u")
luaunit.assertEquals(("%%w"):unescregex(), "%w")
luaunit.assertEquals(("%%x"):unescregex(), "%x")
luaunit.assertEquals(("%("):unescregex(), "(")
luaunit.assertEquals(("%)"):unescregex(), ")")
luaunit.assertEquals(("%."):unescregex(), ".")
luaunit.assertEquals(("%%"):unescregex(), "%")
luaunit.assertEquals(("%%%%"):unescregex():unescregex(), "%")
luaunit.assertEquals(("%+"):unescregex(), "+")
luaunit.assertEquals(("%-"):unescregex(), "-")
luaunit.assertEquals(("%*"):unescregex(), "*")
luaunit.assertEquals(("%?"):unescregex(), "?")
luaunit.assertEquals(("%["):unescregex(), "[")
luaunit.assertEquals(("%]"):unescregex(), "]")
luaunit.assertEquals(("%^"):unescregex(), "^")
luaunit.assertEquals(("%$"):unescregex(), "$")
luaunit.assertEquals(("%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$"):unescregex(), ".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$")
end;
["test: iter(): Calling at empty string returns empty loop"] = function ()
local r = {}
for char in empty:iter() do
table.insert(r, char)
end
luaunit.assertEquals(r, {})
end;
["test: iter(): Default"] = function ()
local r = {}
for char in abc:iter() do
table.insert(r, char)
end
luaunit.assertEquals(r, {"a", "b", "c"})
end;
["test: truncate(): Truncating empty string always returns empty one"] = function ()
ae(empty:truncate(1), "")
ae(empty:truncate(0), "")
ae(empty:truncate(-1), "")
ae(empty:truncate(1, ""), "")
ae(empty:truncate(0, ""), "")
ae(empty:truncate(-1, ""), "")
ae(empty:truncate(1, "..."), "")
ae(empty:truncate(0, "..."), "")
ae(empty:truncate(-1, "..."), "")
end;
["test: truncate(): Truncating single character always returns either character itself or an empty one"] = function ()
ae(a:truncate(1), "a")
ae(a:truncate(0), "")
ae(a:truncate(-1), "a")
ae(a:truncate(1, ""), "a")
ae(a:truncate(0, ""), "")
ae(a:truncate(-1, ""), "a")
ae(a:truncate(1, "b"), "a")
ae(a:truncate(0, "b"), "")
ae(a:truncate(-1, "b"), "a")
ae(a:truncate(1, "..."), "a")
ae(a:truncate(0, "..."), "")
ae(a:truncate(-1, "..."), "a")
end;
["test: truncate(): Default"] = function ()
ae(abcdef:truncate(3), "abc")
end;
["test: truncate(): Truncating the string to it's length returns the string itself"] = function ()
ae(abcdef:truncate(6), "abcdef")
end;
["test: truncate(): Truncating the string to it's length with prefix"] = function ()
ae(abcdef:truncate(6, "..."), "abc...")
end;
["test: truncate(): Truncating the string to large length returns the string itself"] = function ()
ae(abcdef:truncate(100), "abcdef")
end;
["test: truncate(): Truncating the string to large length with prefix returns the string itself"] = function ()
ae(abcdef:truncate(100, "..."), "abcdef")
end;
["test: truncate(): Truncating the string to a total length with prefix returns the string itself"] = function ()
luaunit.assertEquals(("abcdef"):truncate(9, "..."), "abcdef")
end;
["test: startswith(): Calling with empty string always returns true"] = function ()
at(empty:startswith(""))
at(a:startswith(""))
at(abc:startswith(""))
end;
["test: startswith(): Default"] = function ()
at(a:startswith("a"))
at(abc:startswith("ab"))
af(abc:startswith("c"))
end;
["test: endswith(): Calling with empty string always returns true"] = function ()
at(empty:endswith(""))
at(a:endswith(""))
at(abc:endswith(""))
end;
["test: endswith(): Default"] = function ()
at(a:endswith("a"))
at(abc:endswith("bc"))
af(abc:endswith("a"))
end;
["test: isempty(): Calling at empty string returns true"] = function ()
at(empty:isempty())
end;
["test: isempty(): Calling at blank string returns false"] = function ()
af(space:isempty())
end;
["test: isempty(): Calling at arbitrary string returns true"] = function ()
af(a:isempty())
af(abc:isempty())
end;
["test: isblank(): Calling at empty string returns true"] = function ()
at(empty:isblank())
end;
["test: isblank(): Calling at whitespaced string returns true"] = function ()
at((" "):isblank())
at((" "):isblank())
at((" \n"):isblank())
end;
["test: isblank(): Calling at arbitrary string returns false"] = function ()
af(a:isblank())
af(abc:isblank())
end;
["test: tobool(): Default"] = function ()
at(("1"):tobool())
at(("true"):tobool())
at(("on"):tobool())
at(("yes"):tobool())
at(("y"):tobool())
at(("TRUE"):tobool())
at(("ON"):tobool())
at(("YES"):tobool())
at(("Y"):tobool())
af(("0"):tobool())
af(("false"):tobool())
af(("off"):tobool())
af(("no"):tobool())
af(("n"):tobool())
af(("FALSE"):tobool())
af(("OFF"):tobool())
af(("NO"):tobool())
af(("N"):tobool())
end;
["test: tobool(): Converting empty string returns nil"] = function ()
an(empty:tobool())
end;
["test: tobool(): Converting arbitrary string returns nil"] = function ()
an(abc:tobool())
end;
["test: totable(): Converting empty string returns empty table"] = function ()
ae(empty:totable(), {})
end;
["test: totable(): Converting single char string returns table with one item"] = function ()
ae(a:totable(), {"a"})
end;
["test: totable(): Default"] = function ()
ae(abc:totable(), {"a", "b", "c"})
end;
}
os.exit(luaunit.run())