-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathassert_test.sh
More file actions
469 lines (375 loc) · 13.1 KB
/
assert_test.sh
File metadata and controls
469 lines (375 loc) · 13.1 KB
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
#!/usr/bin/env bash
function test_successful_fail() {
true || fail "This cannot fail"
}
function test_unsuccessful_fail() {
assert_same\
"$(console_results::print_failure_message \
"Unsuccessful fail" "Failure message")"\
"$(fail "Failure message")"
}
# @data_provider provider_successful_assert_true
function test_successful_assert_true() {
# shellcheck disable=SC2086
assert_empty "$(assert_true $1)"
}
function provider_successful_assert_true() {
echo true
echo "true"
echo 0
}
function test_unsuccessful_assert_true() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert true" \
"true or 0" \
"but got " "false")"\
"$(assert_true false)"
}
function test_successful_assert_true_on_function() {
assert_empty "$(assert_true ls)"
}
function test_unsuccessful_assert_true_on_function() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert true on function"\
"command or function with zero exit code"\
"but got " "exit code: 2")" \
"$(assert_true "eval return 2")"
}
# @data_provider provider_successful_assert_false
function test_successful_assert_false() {
# shellcheck disable=SC2086
assert_empty "$(assert_false $1)"
}
function provider_successful_assert_false() {
echo false
echo "false"
echo 1
}
function test_unsuccessful_assert_false() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert false" \
"false or 1" \
"but got " "true")"\
"$(assert_false true)"
}
function test_successful_assert_false_on_function() {
assert_empty "$(assert_false "eval return 1")"
}
function test_unsuccessful_assert_false_on_function() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert false on function"\
"command or function with non-zero exit code"\
"but got " "exit code: 0")" \
"$(assert_false "eval return 0")"
}
function test_successful_assert_same() {
assert_empty "$(assert_same "1" "1")"
}
function test_unsuccessful_assert_same() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert same" "1" "but got " "2")"\
"$(assert_same "1" "2")"
}
function test_successful_assert_empty() {
assert_empty "$(assert_empty "")"
}
function test_unsuccessful_assert_empty() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert empty" "to be empty" "but got " "1")"\
"$(assert_empty "1")"
}
function test_successful_assert_not_empty() {
assert_empty "$(assert_not_empty "a_random_string")"
}
function test_unsuccessful_assert_not_empty() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not empty" "to not be empty" "but got " "")"\
"$(assert_not_empty "")"
}
function test_successful_assert_not_same() {
assert_empty "$(assert_not_same "1" "2")"
}
function test_unsuccessful_assert_not_same() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not same" "1" "but got " "1")"\
"$(assert_not_same "1" "1")"
}
function test_successful_assert_not_equals() {
assert_empty "$(assert_not_equals "1" "2")"
}
function test_unsuccessful_assert_not_equals() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not equals" "1" "but got " "1")"\
"$(assert_not_equals "1" "1")"
}
function test_unsuccessful_assert_not_equals_with_special_chars() {
local str1="${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} foo"
local str2="✗ Failed foo"
assert_equals\
"$(console_results::print_failed_test \
"Unsuccessful assert not equals with special chars" \
"$str1" "but got " "$str2")"\
"$(assert_not_equals "$str1" "$str2")"
}
function test_successful_assert_contains_ignore_case() {
assert_empty "$(assert_contains_ignore_case "Linux" "GNU/LINUX")"
}
function test_unsuccessful_assert_contains_ignore_case() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert contains ignore case" "GNU/LINUX" "to contain" "Unix")"\
"$(assert_contains_ignore_case "Unix" "GNU/LINUX")"
}
function test_successful_assert_contains() {
assert_empty "$(assert_contains "Linux" "GNU/Linux")"
}
function test_unsuccessful_assert_contains() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert contains" "GNU/Linux" "to contain" "Unix")"\
"$(assert_contains "Unix" "GNU/Linux")"
}
function test_successful_assert_not_contains() {
assert_empty "$(assert_not_contains "Linus" "GNU/Linux")"
}
function test_unsuccessful_assert_not_contains() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not contains" "GNU/Linux" "to not contain" "Linux")"\
"$(assert_not_contains "Linux" "GNU/Linux")"
}
function test_successful_assert_matches() {
assert_empty "$(assert_matches ".*Linu*" "GNU/Linux")"
}
function test_unsuccessful_assert_matches() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert matches" "GNU/Linux" "to match" ".*Pinux*")"\
"$(assert_matches ".*Pinux*" "GNU/Linux")"
}
function test_successful_assert_not_matches() {
assert_empty "$(assert_not_matches ".*Pinux*" "GNU/Linux")"
}
function test_unsuccessful_assert_not_matches() {
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert not matches" "GNU/Linux" "to not match" ".*Linu*")"\
"$(assert_not_matches ".*Linu*" "GNU/Linux")"
}
function test_successful_assert_exit_code() {
function fake_function() {
exit 0
}
assert_empty "$(assert_exit_code "0" "$(fake_function)")"
}
function test_unsuccessful_assert_exit_code() {
function fake_function() {
exit 1
}
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert exit code" "1" "to be" "0")"\
"$(assert_exit_code "0" "$(fake_function)")"
}
function test_successful_return_assert_exit_code() {
function fake_function() {
return 0
}
fake_function
assert_exit_code "0"
}
function test_unsuccessful_return_assert_exit_code() {
function fake_function() {
return 1
}
assert_exit_code "1" "$(fake_function)"
}
function test_successful_assert_successful_code() {
function fake_function() {
return 0
}
assert_empty "$(assert_successful_code "$(fake_function)")"
}
function test_unsuccessful_assert_successful_code() {
function fake_function() {
return 2
}
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert successful code" "2" "to be exactly" "0")"\
"$(assert_successful_code "$(fake_function)")"
}
function test_successful_assert_general_error() {
function fake_function() {
return 1
}
assert_empty "$(assert_general_error "$(fake_function)")"
}
function test_unsuccessful_assert_general_error() {
function fake_function() {
return 2
}
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert general error" "2" "to be exactly" "1")"\
"$(assert_general_error "$(fake_function)")"
}
function test_successful_assert_command_not_found() {
assert_empty "$(assert_command_not_found "$(a_non_existing_function > /dev/null 2>&1)")"
}
function test_unsuccessful_assert_command_not_found() {
function fake_function() {
return 0
}
assert_same\
"$(console_results::print_failed_test "Unsuccessful assert command not found" "0" "to be exactly" "127")"\
"$(assert_command_not_found "$(fake_function)")"
}
function test_successful_assert_array_contains() {
local distros=(Ubuntu 123 Linux\ Mint)
assert_empty "$(assert_array_contains "123" "${distros[@]}")"
}
function test_unsuccessful_assert_array_contains() {
local distros=(Ubuntu 123 Linux\ Mint)
assert_same\
"$(console_results::print_failed_test \
"Unsuccessful assert array contains"\
"Ubuntu 123 Linux Mint"\
"to contain"\
"non_existing_element")"\
"$(assert_array_contains "non_existing_element" "${distros[@]}")"
}
function test_successful_assert_array_not_contains() {
local distros=(Ubuntu 123 Linux\ Mint)
assert_empty "$(assert_array_not_contains "a_non_existing_element" "${distros[@]}")"
}
function test_unsuccessful_assert_array_not_contains() {
local distros=(Ubuntu 123 Linux\ Mint)
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert array not contains" "Ubuntu 123 Linux Mint" "to not contain" "123")"\
"$(assert_array_not_contains "123" "${distros[@]}")"
}
function test_successful_assert_string_starts_with() {
assert_empty "$(assert_string_starts_with "ho" "house")"
}
function test_unsuccessful_assert_string_starts_with() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert string starts with" "pause" "to start with" "hou")"\
"$(assert_string_starts_with "hou" "pause")"
}
function test_successful_assert_string_not_starts_with() {
assert_empty "$(assert_string_not_starts_with "hou" "pause")"
}
function test_unsuccessful_assert_string_not_starts_with() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert string not starts with" "house" "to not start with" "ho")"\
"$(assert_string_not_starts_with "ho" "house")"
}
function test_successful_assert_string_ends_with() {
assert_empty "$(assert_string_ends_with "bar" "foobar")"
}
function test_unsuccessful_assert_string_ends_with() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert string ends with" "foobar" "to end with" "foo")"\
"$(assert_string_ends_with "foo" "foobar")"
}
function test_successful_assert_string_not_ends_with() {
assert_empty "$(assert_string_not_ends_with "foo" "foobar")"
}
function test_unsuccessful_assert_string_not_ends_with() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert string not ends with" "foobar" "to not end with" "bar")"\
"$(assert_string_not_ends_with "bar" "foobar")"
}
function test_successful_assert_less_than() {
assert_empty "$(assert_less_than "3" "1")"
}
function test_unsuccessful_assert_less_than() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert less than" "3" "to be less than" "1")"\
"$(assert_less_than "1" "3")"
}
function test_successful_assert_less_or_equal_than_with_a_smaller_number() {
assert_empty "$(assert_less_or_equal_than "3" "1")"
}
function test_successful_assert_less_or_equal_than_with_an_equal_number() {
assert_empty "$(assert_less_or_equal_than "3" "3")"
}
function test_unsuccessful_assert_less_or_equal_than() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert less or equal than" "3" "to be less or equal than" "1")"\
"$(assert_less_or_equal_than "1" "3")"
}
function test_successful_assert_greater_than() {
assert_empty "$(assert_greater_than "1" "3")"
}
function test_unsuccessful_assert_greater_than() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert greater than" "1" "to be greater than" "3")"\
"$(assert_greater_than "3" "1")"
}
function test_successful_assert_greater_or_equal_than_with_a_smaller_number() {
assert_empty "$(assert_greater_or_equal_than "1" "3")"
}
function test_successful_assert_greater_or_equal_than_with_an_equal_number() {
assert_empty "$(assert_greater_or_equal_than "3" "3")"
}
function test_unsuccessful_assert_greater_or_equal_than() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert greater or equal than" "1" "to be greater or equal than" "3")"\
"$(assert_greater_or_equal_than "3" "1")"
}
function test_successful_assert_equals() {
assert_equals\
"✗ Failed foo"\
"${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} foo"
}
function test_successful_assert_equals_with_special_chars() {
local string="${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} foo"
assert_equals "$string" "$string"
}
function test_unsuccessful_assert_equals() {
local str1="${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} str1"
local str2="${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT} str2"
assert_same\
"$(console_results::print_failed_test \
"Unsuccessful assert equals"\
"✗ Failed str1"\
"but got "\
"✗ Failed str2")"\
"$(assert_equals "$str1" "$str2")"
}
function test_successful_assert_line_count_empty_str() {
assert_empty "$(assert_line_count 0 "")"
}
function test_successful_assert_line_count_one_line() {
assert_empty "$(assert_line_count 1 "one line")"
}
function test_successful_assert_count_multiline() {
local multiline_string="this is line one
this is line two
this is line three"
assert_empty "$(assert_line_count 3 "$multiline_string")"
}
function test_successful_assert_line_count_multiline_string_in_one_line() {
assert_empty "$(assert_line_count 4 "one\ntwo\nthree\nfour")"
}
function test_successful_assert_line_count_multiline_with_new_lines() {
local multiline_str="this \n is \n a multiline \n in one
\n
this is line 7
this is \n line nine
"
assert_empty "$(assert_line_count 10 "$multiline_str")"
}
function test_unsuccessful_assert_line_count() {
assert_same\
"$(console_results::print_failed_test\
"Unsuccessful assert line count" "one_line_string" "to contain number of lines equal to" "10" "but found" "1")"\
"$(assert_line_count 10 "one_line_string")"
}