File tree Expand file tree Collapse file tree 4 files changed +83
-0
lines changed
Expand file tree Collapse file tree 4 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+
3+ main () {
4+ hello " $1 "
5+ }
6+
7+ hello () {
8+ case " $1 " in
9+ fr) echo Bonjour le monde
10+ ;;
11+ * ) echo we only speak french for the moment, sorry >&2
12+ ;;
13+ esac
14+ }
15+
16+ # do not run main when sourcing the script
17+ [[ " $0 " == " ${BASH_SOURCE[0]} " ]] && main " $@ " || true
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+
3+ declare -F current_hour > /dev/null || current_hour () {
4+ date +%H
5+ }
6+
7+ if (( "$(current_hour)" < 11 ))
8+ then
9+ echo " Good morning World!"
10+ else if (( "$(current_hour)" > 17 ))
11+ then
12+ echo " Good evening World!"
13+ else
14+ echo " Hello World!"
15+ fi ; fi
Original file line number Diff line number Diff line change 1+ # In this example, we want to test the specific functions of a script.
2+ # So we source this script in setup_suite.
3+ # However, we don't want the script to be executed when sourcing it,
4+ # we are only interested in having access to the functions to tests
5+ # them.
6+ # To do so, we adapt the script under test so that it's main code
7+ # is not executed when sourcing it.
8+ # See ../hello_i18n how we define a main function to encapsulate
9+ # all the main code and the main function is only called when the
10+ # script itself is launched, not when it is sourced
11+
12+ test_can_say_hi_in_french() {
13+ assert_equals "Bonjour le monde" "$(hello fr)"
14+ }
15+
16+ setup_suite() {
17+ source ../hello_i18n
18+ }
19+
Original file line number Diff line number Diff line change 1+ # In this example, we want to test a script which depends on
2+ # the external world (here, the current time). To do so,
3+ # we decide to adapt the script by extracting the time
4+ # querying in a dedicated function named current_hour.
5+ # That way, we can fake the current hour in the test.
6+ # Unfortunately, we also need to adapt the script under
7+ # test so that, when the current_time function is already
8+ # defined, it does not override it. Otherwise, the fake created
9+ # in the test would never be used. See ../hello_timed
10+
11+
12+ test_can_say_good_morning() {
13+ it_is_o_clock 10
14+ assert_equals "Good morning World!" "$(../hello_timed)"
15+ }
16+
17+ test_can_say_good_evening() {
18+ it_is_o_clock 18
19+ assert_equals "Good evening World!" "$(../hello_timed)"
20+ }
21+
22+ test_can_say_hello() {
23+ it_is_o_clock 12
24+ assert_equals "Hello World!" "$(../hello_timed)"
25+ }
26+
27+ it_is_o_clock() {
28+ local hour="$1"
29+ fake current_hour << EOF
30+ $hour
31+ EOF
32+ }
You can’t perform that action at this time.
0 commit comments