/
tests
executable file
·314 lines (265 loc) · 7.51 KB
/
tests
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
#!/bin/bash
#
# This file contains tests for the countdown-status app.
# Author: Jacek Mikrut (jacekmikrut.software at gmail.com)
#
# Copyright (c) 2012 Jacek Mikrut
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Test framework:
number_of_assertions=0
failed_assertion_messages=()
function precondition() {
if ! eval "$1"; then
echo "Precondition failed."
echo "Line ${BASH_LINENO[1]}: `sed -n \"${BASH_LINENO[1]}p\" ${BASH_SOURCE[0]}`"
exit 1
fi
}
function assert_match() {
((number_of_assertions++))
if [[ "$1" =~ $2 ]]; then
echo -n "."
else
echo -n "E"
failed_assertion_messages[${#failed_assertion_messages[@]}]="${BASH_LINENO[1]}: expected '$1' to match '$2'"
fi
}
function assert_true() {
((number_of_assertions++))
if eval "$1"; then
echo -n "."
else
echo -n "E"
failed_assertion_messages[${#failed_assertion_messages[@]}]="${BASH_LINENO[1]}: expected $1 to be true, but wasn't."
fi
}
function execute() {
test
after_each
}
function echo_failed_assertion_messages() {
if [[ ${#failed_assertion_messages[@]} -eq 0 ]]; then return; fi
echo
echo "Failed assertions:"
for message in "${failed_assertion_messages[@]}"; do
echo $message
done
}
function echo_summary() {
echo
echo "$number_of_assertions assertions, ${#failed_assertion_messages[@]} failures."
}
# Test helper methods
function before_all() {
this_script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOME="$this_script_dir"
default_data_file_path="$HOME/.countdown-status-data"
custom_data_file_path="$HOME/.countdown-status-data-custom"
}
function after_all() {
echo
echo_failed_assertion_messages
echo_summary
}
function remove_file_if_exists() {
[[ -e "$1" ]] && rm "$1"
}
function after_each {
remove_file_if_exists "$default_data_file_path"
remove_file_if_exists "$custom_data_file_path"
}
function when_the_default_data_file_does_not_exist() {
precondition "[[ ! -e $default_data_file_path ]]"
}
function when_the_custom_data_file_does_not_exist() {
precondition "[[ ! -e $custom_data_file_path ]]"
}
function when_app_executed_without_arguments() {
output=`"$this_script_dir/countdown-status"`
}
function when_app_executed_with() {
output=`"$this_script_dir/countdown-status" "$@"`
}
function when_the_data_file_has_not_the_proper_header() {
touch "$default_data_file_path"
}
function its_output_should_match() {
assert_match "$output" "$1"
}
function it_should_not_create_the_default_data_file() {
assert_true "[[ ! -e $default_data_file_path ]]"
}
function it_should_not_create_the_custom_data_file() {
assert_true "[[ ! -e $custom_data_file_path ]]"
}
function it_should_create_the_default_data_file() {
assert_true "[[ -e $default_data_file_path ]]"
}
function it_should_create_the_custom_data_file() {
assert_true "[[ -e $custom_data_file_path ]]"
}
# Tests:
before_all
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_without_arguments
its_output_should_match "OFF"
it_should_not_create_the_default_data_file
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_with -s 9
its_output_should_match "0:09|0:08"
it_should_create_the_default_data_file
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_with -m 5
its_output_should_match "5:00|4:59"
it_should_create_the_default_data_file
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_with -h 3
its_output_should_match "3:00:00|2:59:59"
it_should_create_the_default_data_file
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_with -h 1 -m 2 -s 4
its_output_should_match "1:02:04|1:02:03"
it_should_create_the_default_data_file
}
execute
function test() {
when_app_executed_with -m 5
when_app_executed_with -s 9
its_output_should_match "0:09|0:08"
}
execute
function test() {
when_app_executed_with -m 5
when_app_executed_without_arguments
its_output_should_match "5:00|4:59"
}
execute
function test() {
when_app_executed_with -m 5
sleep 2
when_app_executed_without_arguments
its_output_should_match "4:58|4:57"
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_the_custom_data_file_does_not_exist
when_app_executed_with -f $custom_data_file_path
its_output_should_match "OFF"
it_should_not_create_the_default_data_file
it_should_not_create_the_custom_data_file
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_the_custom_data_file_does_not_exist
when_app_executed_with -f $custom_data_file_path -m 10
its_output_should_match "10:00|9:59"
it_should_not_create_the_default_data_file
it_should_create_the_custom_data_file
}
execute
function test() {
when_app_executed_with -f $custom_data_file_path -m 10
when_app_executed_with -f $custom_data_file_path
its_output_should_match "10:00|9:59"
}
execute
function test() {
when_the_data_file_has_not_the_proper_header
when_app_executed_without_arguments
its_output_should_match "doesn't contain the proper header."
}
execute
function test() {
when_app_executed_with -s 0
its_output_should_match "Time's up!"
}
execute
function test() {
when_app_executed_with -s 0 -u "Time's up, relax!"
when_app_executed_without_arguments
its_output_should_match "Time's up, relax!"
}
execute
function test() {
when_app_executed_with -s 0
when_app_executed_with -u "Time's up, relax!"
when_app_executed_without_arguments
its_output_should_match "Time's up, relax!"
}
execute
function test() {
when_app_executed_with -m 30 -b "Time left: "
when_app_executed_without_arguments
its_output_should_match "Time left: (30:00|29:59)"
}
execute
function test() {
when_app_executed_with -m 30 -a " left."
when_app_executed_without_arguments
its_output_should_match "(30:00|29:59) left."
}
execute
function test() {
when_app_executed_with -m 30 -b "before" -a "after"
when_app_executed_with -b "" -a ""
when_app_executed_without_arguments
its_output_should_match "^(30:00|29:59)$"
}
execute
function test() {
when_the_default_data_file_does_not_exist
when_app_executed_with -o "Turned off."
its_output_should_match "Turned off."
it_should_not_create_the_default_data_file
}
execute
function test() {
when_app_executed_with --help
its_output_should_match "Usage:"
}
execute
function test() {
when_app_executed_with --version
its_output_should_match "version"
}
execute
function test() {
when_app_executed_with -v
its_output_should_match "version"
}
execute
after_all
exit 0