-
Notifications
You must be signed in to change notification settings - Fork 89
/
kwio_test.sh
executable file
·322 lines (254 loc) · 12.1 KB
/
kwio_test.sh
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
#!/bin/bash
include './tests/utils.sh'
include './src/kwio.sh'
include './src/kwlib.sh'
# NOTE: All executions off 'alert_completion' in this test file must be done
# inside a subshell (i.e. "$(alert_completion ...)"), because this function
# invokes other commands in the background. So if not done inside a subshell,
# the function will return before the background commands finish.
declare -A configurations
declare -g load_module_text_path="$PWD/tests/samples/load_module_text_test_samples/"
sound_file="$PWD/tests/.kwio_test_aux/sound.file"
visual_file="$PWD/tests/.kwio_test_aux/visual.file"
function setUp()
{
mkdir -p tests/.kwio_test_aux
configurations['sound_alert_command']="touch $sound_file"
configurations['visual_alert_command']="touch $visual_file"
}
function tearDown()
{
rm -rf tests/.kwio_test_aux
}
function test_alert_completion_options()
{
configurations['alert']='n'
rm -f "$sound_file" "$visual_file"
alert_completion '' '--alert=vs'
wait "$!"
[[ -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's vs option didn't work." $?
rm -f "$sound_file" "$visual_file"
alert_completion '' '--alert=sv'
wait "$!"
[[ -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's sv option didn't work." $?
rm -f "$sound_file" "$visual_file"
alert_completion '' '--alert=s'
wait "$!"
[[ -f "$sound_file" && ! -f "$visual_file" ]]
assertTrue "Alert's s option didn't work." $?
rm -f "$sound_file" "$visual_file"
alert_completion '' '--alert=v'
wait "$!"
[[ ! -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's v option didn't work." $?
rm -f "$sound_file" "$visual_file"
alert_completion '' '--alert=n'
wait "$!"
[[ ! -f "$sound_file" && ! -f "$visual_file" ]]
assertTrue "Alert's n option didn't work." $?
true
}
function test_alert_completition_validate_config_file_options()
{
mkdir -p tests/.kwio_test_aux
rm -f "$sound_file" "$visual_file"
configurations['alert']='vs'
alert_completion '' ''
wait "$!"
[[ -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's vs option didn't work." $?
rm -f "$sound_file" "$visual_file"
configurations['alert']='sv'
alert_completion '' ''
wait "$!"
[[ -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's sv option didn't work." $?
rm -f "$sound_file" "$visual_file"
configurations['alert']='s'
alert_completion '' ''
wait "$!"
[[ -f "$sound_file" && ! -f "$visual_file" ]]
assertTrue "Alert's s option didn't work." $?
rm -f "$sound_file" "$visual_file"
configurations['alert']='v'
alert_completion '' ''
wait "$!"
[[ ! -f "$sound_file" && -f "$visual_file" ]]
assertTrue "Alert's v option didn't work." $?
rm -f "$sound_file" "$visual_file"
configurations['alert']='n'
alert_completion '' ''
wait "$!"
[[ ! -f "$sound_file" && ! -f "$visual_file" ]]
assertTrue "Alert's n option didn't work." $?
true
}
function test_alert_completion_visual_alert()
{
local output
local expected='TESTING COMMAND'
configurations['visual_alert_command']='/bin/printf "%s\n" "$COMMAND"'
output="$(alert_completion "$expected" '--alert=v')"
assertEquals 'Variable v should exist.' "$expected" "$output"
}
function test_alert_completion_sound_alert()
{
local output
local expected='TESTING COMMAND'
configurations['sound_alert_command']='/bin/printf "%s\n" "$COMMAND"'
output="$(alert_completion "$expected" '--alert=s')"
assertEquals 'Variable s should exist.' "$expected" "$output"
}
function test_ask_with_default()
{
local output=''
local expected_output=''
local assert_equals_message=''
# Default option showing
expected_output=$'Insert something here (lala): \nsomething'
assert_equals_message='Default answer and user answer are different.'
output=$(printf 'something\n' | ask_with_default 'Insert something here' 'lala' '' 'TEST_MODE')
assert_equals_helper "$assert_equals_message" "$LINENO" "$expected_output" "$output"
expected_output=$'Insert something here (lala): \nlala'
assert_equals_message='User selected default answer.'
output=$(printf '\n' | ask_with_default 'Insert something here' 'lala' '' 'TEST_MODE')
assert_equals_helper "$assert_equals_message" "$LINENO" "$expected_output" "$output"
# Default option not showing (third parameter not empty)
expected_output=$'Insert something here: \nsomething'
assert_equals_message='Not showing default answer, user answered different.'
output=$(printf 'something\n' | ask_with_default 'Insert something here' 'lala' 'false' 'TEST_MODE')
assert_equals_helper "$assert_equals_message" "$LINENO" "$expected_output" "$output"
expected_output=$'Insert something here: \nlala'
assert_equals_message='Not showing default answer, user selected it.'
output=$(printf '\n' | ask_with_default 'Insert something here' 'lala' 'false' 'TEST_MODE')
assert_equals_helper "$assert_equals_message" "$LINENO" "$expected_output" "$output"
}
function test_ask_yN()
{
local assert_equals_message=''
assert_equals_message='Default answer: no, user answer: y'
output=$(printf 'y\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: no, user answer: Y'
output=$(printf 'Y\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: no, user answer: Yes'
output=$(printf 'Yes\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: no, user answer: invalid (sim)'
output=$(printf 'Sim\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: no, user answer: No'
output=$(printf 'No\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: no, user answer: N'
output=$(printf 'N\n' | ask_yN 'Test message')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
# Tests with default option selected
assert_equals_message='Default answer: N, user answer: y'
output=$(printf 'y\n' | ask_yN 'Test message' 'N')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: y, user answer: Y'
output=$(printf 'Y\n' | ask_yN 'Test message' 'y')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: y, user answer: default'
output=$(printf '\n' | ask_yN 'Test message' 'y')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: Y, user answer: n'
output=$(printf 'n\n' | ask_yN 'Test message' 'Y')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: n, user answer: N'
output=$(printf 'N\n' | ask_yN 'Test message' 'n')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: n, user anser: default'
output=$(printf '\n' | ask_yN 'Test message' 'n')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
# Invalid default
assert_equals_message='Default answer: invalid (lala), user answer: default'
output=$(printf '\n' | ask_yN 'Test message' 'lala')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: invalid (lala), user answer: n'
output=$(printf 'n\n' | ask_yN 'Test message' 'lala')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: invalid (lala), user answer: y'
output=$(printf 'y\n' | ask_yN 'Test message' 'lala')
assert_equals_helper "$assert_equals_message" "$LINENO" '1' "$output"
assert_equals_message='Default answer: invalid (lalaYes), user answer: default (no)'
output=$(printf '\n' | ask_yN 'Test message' 'lalaYes')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: invalid (lalaNo), user answer: default (no)'
output=$(printf '\n' | ask_yN 'Test message' 'lalaNo')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
# Invalid answer
assert_equals_message='Default answer: invalid (lala), user answer: no (invalid: lalaYes)'
output=$(printf 'lalaYes\n' | ask_yN 'Test message' 'lala')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
assert_equals_message='Default answer: invalid (lala), user answer: no (invalid: lalano)'
output=$(printf 'lalano\n' | ask_yN 'Test message' 'lala')
assert_equals_helper "$assert_equals_message" "$LINENO" '0' "$output"
}
function test_load_module_text_good_files()
{
local multiple_line_str
load_module_text "$load_module_text_path/file_correct"
assertEquals 'Should work without any errors.' 0 "$?"
assertEquals 'Key1' 'Hello, there! How are you? I hope you are enjoying reading this test suit!' "${module_text_dictionary[key1]}"
assertEquals 'Key2' 'Hey, you still there? []' "${module_text_dictionary[key2]}"
multiple_line_str=$'This should work with multiple lines.\nLine 1\nLine 2\nLine 3\nLine 4\nLine 5'
assertEquals 'Key3' "$multiple_line_str" "${module_text_dictionary[key3]}"
assertEquals 'Key4' 'done.' "${module_text_dictionary[key4]}"
assertEquals 'Key5' '' "${module_text_dictionary[key5]}"
multiple_line_str=$'\n\n\n\n\nThe one above should have an empty value.\n'
assertEquals 'Key6' "$multiple_line_str" "${module_text_dictionary[key6]}"
multiple_line_str=$'\nThis value should be ok\n'
assertEquals 'Key7' "$multiple_line_str" "${module_text_dictionary[key7]}"
}
function test_load_module_text_bad_keys()
{
local expected
local output
declare -a expected_sequence=(
"[ERROR]:$load_module_text_path/file_wrong_key:7: Keys should be alphanum chars."
"[ERROR]:$load_module_text_path/file_wrong_key:10: Keys should be alphanum chars."
"[ERROR]:$load_module_text_path/file_wrong_key:13: Keys should be alphanum chars."
"[ERROR]:$load_module_text_path/file_wrong_key:16: Keys should be alphanum chars."
"[ERROR]:$load_module_text_path/file_wrong_key:19: Keys should be alphanum chars."
)
output=$(load_module_text "$load_module_text_path/file_wrong_key")
assertEquals 'This file has invalid keys, this should return multiple errors.' 129 "$?"
compare_command_sequence 'expected_sequence' "$output" "$LINENO"
}
function test_load_module_text_invalid_files()
{
local expected
local output
expected="[ERROR]:$load_module_text_path/file_without_key: No key found."
output=$(load_module_text "$load_module_text_path/file_without_key")
assertEquals "[$LINENO]: This file has no keys, this should return an error." 126 "$?"
assertEquals "[$LINENO]: The ERROR message is not consistent with the error code or is incomplete." "$expected" "$output"
expected="[ERROR]:$load_module_text_path/file_empty: File is empty."
output=$(load_module_text "$load_module_text_path/file_empty")
assertEquals "[$LINENO]: This file is empty, this should return an error." 61 "$?"
assertEquals "[$LINENO]: The ERROR message is not consistent with the error code or is incomplete." "$expected" "$output"
}
function test_load_module_text_no_files()
{
local expected
local output
expected="[ERROR]:$load_module_text_path/file_does_not_exist_(do not create): Does not exist or is not a text file."
output=$(load_module_text "$load_module_text_path/file_does_not_exist_(do not create)")
assertEquals "[$LINENO]: This file does not exist, this should return an error." 2 "$?"
assertEquals "[$LINENO]: The ERROR message is not consistent with the error code or is incomplete." "$expected" "$output"
}
function test_load_module_text_repeated_keys()
{
local expected
local output
expected="[WARNING]:$load_module_text_path/file_repeated_keys:9: Overwriting 'Sagan' key."
output=$(load_module_text "$load_module_text_path/file_repeated_keys")
assertEquals "[$LINENO]: Although we received warnings, the function should exit with SUCCESS" 0 "$?"
assertEquals "[$LINENO]: The ERROR message is not consistent with the error code or is incomplete." "$expected" "$output"
}
invoke_shunit