-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patharray_test.bats
220 lines (185 loc) · 5.85 KB
/
array_test.bats
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
#!/usr/bin/env bats
load test_helper
set -e
source lib/array.sh
source lib/is.sh
source lib/util.sh
source lib/user.sh
set +e
@test "array.from.command" {
set -e
command="echo one; echo 'twenty two'; echo three; echo; echo four"
array.from.command my_array "${command}"
[[ ${#my_array[@]} -eq 4 ]] &&
[[ ${my_array[0]} == "one" ]] &&
[[ ${my_array[1]} == "twenty two" ]] &&
[[ ${my_array[2]} == "three" ]] &&
[[ ${my_array[3]} == "four" ]]
}
@test "array.min/max positive" {
declare -a array=(1 495 -2 435 12 0 hello)
local max="$(array.max "${array[@]}")"
local min="$(array.min "${array[@]}")"
set -e
[[ "${max}" -eq 495 && "${min}" -eq -2 ]]
}
@test "array.min/max negative" {
declare -a array=(-10 -24 -1 shit)
local max="$(array.max "${array[@]}")"
local min="$(array.min "${array[@]}")"
set -e
[[ "${max}" -eq -1 && "${min}" -eq -24 ]]
}
@test "array.sort" {
declare -a array=(hello begin again again)
local sorted="$(array.sort "${array[@]}")"
set -e
[[ "${sorted}" == "again again begin hello" ]]
}
@test "array.sort-numeric" {
declare -a array=(10 12 0 1 1)
local sorted="$(array.sort "${array[@]}")"
set -e
[[ "${sorted}" == "0 1 1 10 12" ]]
}
@test "array.uniq" {
declare -a array=(begin hello begin again again)
local sorted="$(array.uniq "${array[@]}")"
set -e
[[ "${sorted}" == "again begin hello" ]]
}
@test "array.eval-in-groups-of" {
declare -a array=(asciidoc asciidoctor autoconf automake awscli bash zsh)
local out
set -e
out="$(array.eval.in-groups-of 2 echo "${array[@]}" | tr '\n' '|')"
[[ "${out}" == "asciidoc asciidoctor|autoconf automake|awscli bash|zsh|" ]]
out="$(array.eval.in-groups-of 3 echo "${array[@]}" | tr '\n' '|')"
[[ "${out}" == "asciidoc asciidoctor autoconf|automake awscli bash|zsh|" ]]
}
@test "array.join with a pipe" {
set -e
declare -a array=("a string" "test2000" "hello" "one")
result=$(array.join '|' "${array[@]}")
echo ${result}
[[ "${result}" == "a string|test2000|hello|one" ]]
}
@test "array.join with comma" {
set -e
unset result array code status
declare -a array=(uno dos tres quatro cinco seis)
set -e
result=$(array.join ', ' "${array[@]}")
[[ ${result} == "uno, dos, tres, quatro, cinco, seis" ]]
[[ $status -eq 0 ]]
}
@test "array.to.piped-list" {
set -e
declare -a array=(orange yellow red)
[[ $(array.to.piped-list "${array[@]}") == "orange | yellow | red" ]]
[[ $status -eq 0 ]]
}
versions_array() {
printf "%s" "11 10 9.6 9.5 9.4 "
}
@test "array.includes() an existing floating point element" {
set -e
declare -a array=($(versions_array))
array.includes 11 "${array[@]}" && \
array.includes 10 "${array[@]}" && \
array.includes 9.6 "${array[@]}" && \
array.includes 9.5 "${array[@]}" && \
array.includes 9.4 "${array[@]}"
}
@test "array.includes() with non-existing floating point element" {
declare -a array=(11 10 9.6 9.5 9.4)
set +e
array.includes 1.1 "${array[@]}"; code=$?
set -e
[[ ${code} -ne 0 ]]
}
@test "array.includes() when one element exists" {
declare -a array=("")
set +e
array.includes test2000 "${array[@]}"; code=$?
set -e
[[ ${code} -ne 0 ]]
}
@test "array.includes() when another element exists" {
declare -a array=("a string" "test2000" "hello" "one")
set -e
array.includes "one" "${array[@]}"
}
@test "array.includes() when element does not exist" {
declare -a array=("a string" "test2000" "hello" "one")
set +e
array.includes "two" "${array[@]}"; local code=$?
set -e
[[ ${code} -eq 1 ]]
}
@test "array.has-element() when element exists using return value" {
declare -a array=("a string" "test2000" "hello" "one")
array.has-element test2000 "${array[@]}" && true
}
@test "array.has-element() when element exists and has a space using return value" {
declare -a array=("a string" "test2000" "hello" "one")
array.has-element "a string" "${array[@]}" && true
}
@test "array.has-element() when element exists, using return value" {
declare -a array=("a string" "test2000" "hello" "one")
set +e
array.has-element test2000 "${array[@]}"; code=$?
set -e
[[ ${code} -eq 0 ]]
}
@test "array.has-element() when element exists using output" {
declare -a array=("a string" "test2000" "hello" "one")
[[ $(array.has-element hello "${array[@]}") == "true" ]]
}
@test "array.has-element() when element is a substring of an existing element using output" {
declare -a array=("a string" "test2000" "hello" "one")
[[ $(array.has-element hell "${array[@]}") == "false" ]]
}
@test "array.has-element when element does not exist using output" {
declare -a array=("a string" "test2000" "hello" "one")
[[ $(array.has-element 123 "${array[@]}") == "false" ]]
}
@test "array.has-element when element does not exist and is a space using output" {
declare -a array=("a string" "test2000" "hello" "one")
[[ $(array.has-element ' ' "${array[@]}") == "false" ]]
}
@test "array.to.bullet-list" {
declare -a array=(kig pig)
tmp=$(mktemp)
array.to.bullet-list "${array[@]}" > ${tmp}
lines=$(cat "${tmp}" | wc -l | tr -d ' ')
echo "${tmp}"
result="$(cat "${tmp}")"
[[ ${lines} -eq 2 ]] &&
[[ $(egrep ' • kig' -c ${tmp}) -eq 1 ]] &&
[[ $(egrep ' • pig' -c ${tmp}) -eq 1 ]]
}
@test "array.force-range > outside the range > less than min" {
local number=10
local n=$(array.force-range ${number} 100 150 120 200 1000)
set -e
[[ ${n} -eq 100 ]]
}
@test "array.force-range > outside the range > greater than max" {
local number=2400
local n=$(array.force-range ${number} 100 150 120 200 1000)
set -e
[[ ${n} -eq 1000 ]]
}
@test "array.force-range > within the range " {
local number=101
local n=$(array.force-range ${number} 100 150 120 200 1000)
set -e
[[ ${n} -eq 101 ]]
}
@test "array.force-range > within the range > equal to a boundary " {
local number=100
local n=$(array.force-range ${number} 100 150 120 200 1000)
set -e
[[ ${n} -eq 100 ]]
}