-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.sh
158 lines (123 loc) · 4.73 KB
/
helpers.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
#! /bin/bash
# Helper function tests
#
# Author: DonTseTse
############# Configuration
#
############# Preparation
# Refuse symlinks and get the absolute path of the commons directory (this file lies in ./tests/.), load dependancies
if [ -h "${BASH_SOURCE[0]}" ]; then echo "Error: called through symlink. Please call directly. Aborting..."; exit 1; fi
commons_path="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && dirname "$(pwd)")"
set -e
. "$commons_path/testing.sh"
. "$commons_path/helpers.sh"
set +e
initialize_test_session "helpers.sh functions"
############# Tests
echo "*** capture() ***"
echo "Since capture() is used in test(), successful tests imply it runs as intended"
configure_test 0 "A string"
test echo "A string"
echo "Test capture() stderr feature - running $> STDERR=1 capture mkdir /proc/test"
STDERR=1 capture mkdir /proc/test
configure_test 0 ""
[ "$stderr" = "mkdir: cannot create directory ‘/proc/test’: No such file or directory" ]
check_test_results "[ \"\$stderr\" = \"mkdir: cannot create directory ‘/proc/test’: No such file or directory\" ]" $? ""
echo "Test capture() variable name prefixes - running $> PREFIX=\"echo\" capture echo \"test\""
VARNAME="echo" capture echo "test"
configure_test 0 "test"
check_test_results "#Checking \$echo_return and \$echo_stdout" "$echo_return" "$echo_stdout"
echo "*** execute_working_directory_dependant_command() ***"
configure_test 0 "/tmp"
test execute_working_directory_dependant_command "/tmp" "pwd"
configure_test 1 ""
test execute_working_directory_dependant_command "/unexistant" "pwd"
configure_test 127 ""
test execute_working_directory_dependant_command "/tmp" "unknown"
echo "*** conditional_exit() ***"
configure_test 22 "Dead!"
stdout="$(conditional_exit 1 "Dead!" 22)"
check_test_results "\$(conditional_exit 1 \"Dead!\" 22)" $? "$stdout"
###
echo "*** is_variable_defined() ***"
declare -A associative_array_var && associative_array_var[index]="value"
echo " - \$> declare associative_array_var && associative_array_var[index]=\"value\""
configure_test 0 ""
test is_variable_defined "associative_array_var"
###
echo "*** is_array_index() ***"
numeric_array_var=("1st" "2nd")
echo " - \$> numeric_array_var=(\"1st\" \"2nd\")"
configure_test 0 ""
test is_array_index "numeric_array_var" 0
configure_test 1 ""
test is_array_index "numeric_array_var" 100
configure_test 1 ""
test is_array_index "numeric_array_var" "string"
configure_test 0 ""
test is_array_index "associative_array_var" "index"
configure_test 1 ""
test is_array_index "associative_array_var" "undefined"
configure_test 1 ""
test is_array_index "associative_array_var" 0
#echo "associative_array_var element @ index 'index': $(get_array_element "associative_array_var" "index")"
#echo "associative_array_var element @ index 'undefined': $(get_array_element "associative_array_var" "undefined")"
#echo "associative_array_var element @ index '0': $(get_array_element "associative_array_var" "0")"
echo "*** get_array_element() ***"
arr=("idx0" "idx1" "idx2")
echo " - \$> arr=(\"idx0\" \"idx1\" \"idx2\")"
configure_test 0 "idx1"
test get_array_element "arr" 1
configure_test 1 ""
test get_array_element "unexistant" 1
configure_test 2 ""
test get_array_element "arr" 5
configure_test 2 ""
test get_array_element "arr" "string"
configure_test 3 ""
test get_array_element ""
configure_test 4 ""
test get_array_element "arr"
configure_test 0 "value"
test get_array_element "associative_array_var" "index"
configure_test 2 ""
test get_array_element "associative_array_var" "undefined"
configure_test 2 ""
test get_array_element "associative_array_var" 0
###
echo "*** calculate() ***"
echo "If no precision is specified, calculate() defaults to 3 decimals"
configure_test 0 "2.527"
test calculate "(2.5 * 2 * 4) / 10 + 0.52728888888"
configure_test 0 "2.527289"
test calculate "(2.5 * 2 * 4) / 10 + 0.52728888888" 6
echo "Unsignificant decimals are removed (otherwise the call below would return 5.000)"
configure_test 0 "5"
test calculate "(250 * 2) / 100"
echo "This applies likewise if an amount of decimals is specified"
configure_test 0 "5"
test calculate "(250 * 2) / 100" 10
configure_test 0 "5.72"
test calculate "(250 * 2) / 100 + 0.72" 10
echo "A number of decimals (2nd parameter) set to 0 or int returns a integer"
configure_test 0 "5"
test calculate "(250 * 2 + 0.22278999921) / 100" 0
configure_test 0 "5"
test calculate "(250 * 2 + 0.22278999921) / 100" "int"
configure_test 0 "0.000"
test calculate ""
###
echo "*** is_globbing_enabled() ***"
[ -z "$(echo $- | grep f)" ]
prev_glob_status=$?
set -f
echo " - Globbing disabled"
configure_test 1 ""
test is_globbing_enabled
set +f
echo " - Globbing enabled"
configure_test 0 ""
test is_globbing_enabled
[ $prev_glob_status -eq 1 ] && set -f
echo " - Globbing reset"
conclude_test_session