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
|
# In this example, we want to test a script which depends on
# the external world (here, the current time). To do so,
# we decide to adapt the script by extracting the time
# querying in a dedicated function named current_hour.
# That way, we can fake the current hour in the test.
# Unfortunately, we also need to adapt the script under
# test so that, when the current_time function is already
# defined, the script does not override this definition.
# Otherwise, the fake created in the test would never be
# used.
# See ../hello_timed
test_can_say_good_morning() {
it_is_o_clock 10
assert_equals "Good morning World!" "$(../hello_timed)"
}
test_can_say_good_evening() {
it_is_o_clock 18
assert_equals "Good evening World!" "$(../hello_timed)"
}
test_can_say_hello() {
it_is_o_clock 12
assert_equals "Hello World!" "$(../hello_timed)"
}
it_is_o_clock() {
local hour="$1"
fake current_hour << EOF
$hour
EOF
}
|