Skip to content

Commit 97e0b02

Browse files
committed
test: Add tests for 'set-*' type checking
1 parent 38bbad4 commit 97e0b02

File tree

3 files changed

+274
-25
lines changed

3 files changed

+274
-25
lines changed

pkg/lib/traverse-set.sh

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# shellcheck shell=bash
22

33
bash_object.traverse-set() {
4+
# TODO: rename zerocopy, as it's misleading - ex. 'zerocopy' object would be slower
5+
local flag_zerocopy='no'
6+
47
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
58
stdtrace.log 0 ''
69
stdtrace.log 0 "CALL: bash_object.traverse-set: $*"
@@ -12,66 +15,81 @@ bash_object.traverse-set() {
1215
local final_value="$4"
1316

1417
if (( $# != 4)); then
15-
bash_object.util.die 'ERROR_INVALID_ARGS' "Incorrect arguments for subcommand 'set-$final_value_type'"
18+
bash_object.util.die 'ERROR_INVALID_ARGS' "Expected '4' arguments, but received '$#'"
1619
return
1720
fi
1821

19-
# TODO test
22+
# TODO: zerocopy check
2023
# Ensure parameters are not empty
21-
local variable=
22-
for variable_name in final_value_type root_object_name filter; do
23-
local -n variable="$variable_name"
24-
25-
if [ -z "$variable" ]; then
26-
bash_object.util.die 'ERROR_INVALID_ARGS' "Variable '$variable' is empty. Please check passed parameters"
24+
if [ "$flag_zerocopy" = no ]; then
25+
for ((i=1; i<=$#; i++)); do
26+
if [ -z "${!i}" ]; then
27+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '$i' is empty. Please check passed parameters"
28+
return
29+
fi
30+
done
31+
elif [ "$flag_zerocopy" = yes ]; then
32+
# If we are zerocopying, it means the string or array is passed on the
33+
# command line; we don't check it since the string or the first element
34+
# of array or string could be empty
35+
for ((i=1; i<=$#-1; i++)); do
36+
if [ -z "${!i}" ]; then
37+
bash_object.util.die 'ERROR_INVALID_ARGS' "Positional parameter '$i' is empty. Please check passed parameters"
2738
return
2839
fi
29-
done
40+
done
41+
else
42+
bash_object.util.die 'ERROR_INTERNAL_MISCELLANEOUS' "Unexpected final_value_type '$final_value_type $actual_final_value_type'"
43+
return
44+
fi
3045

3146
if [ -n "${VERIFY_BASH_OBJECT+x}" ]; then
32-
# TODO: test
33-
# Check 'root_object_name'
47+
# Ensure the root object exists, and is an associative array
3448
local root_object_type=
3549
if root_object_type="$(declare -p "$root_object_name" 2>/dev/null)"; then :; else
36-
bash_object.util.die 'ERROR_INVALID_ARGS' "The final value of '$root_object_name' does not exist"
50+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The associative array '$root_object_name' does not exist"
3751
return
3852
fi
3953
root_object_type="${root_object_type#declare -}"
4054
if [ "${root_object_type::1}" != 'A' ]; then
41-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "The root object must have a type of 'object'"
55+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "The 'root object' must be an associative array"
4256
return
4357
fi
4458

45-
# TODO: test
46-
# Check 'final_value' for type correctness
47-
if [ "$final_value_type" != string ]; then
59+
# TODO: dont' do this in zerocopy mode
60+
# Ensure the 'final_value' is the same type as specified by the user
61+
if [ "$final_value_type" != string ]; then # remove this conditional when consistency and zerocopy mode
4862
local actual_final_value_type=
4963
if ! actual_final_value_type="$(declare -p "$final_value" 2>/dev/null)"; then
50-
bash_object.util.die 'ERROR_INVALID_ARGS' "The final value of '$final_value' does not exist"
64+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The variable '$final_value' does not exist"
5165
return
5266
fi
5367
actual_final_value_type="${actual_final_value_type#declare -}"
5468
case "${actual_final_value_type::1}" in
5569
A) actual_final_value_type='object' ;;
5670
a) actual_final_value_type='array' ;;
57-
i) actual_final_value_type='integer' ;;
5871
-) actual_final_value_type='string' ;;
59-
*) actual_final_value_type='unknown' ;;
72+
*) actual_final_value_type='other' ;;
6073
esac
6174

6275
if [ "$final_value_type" == object ]; then
6376
if [ "$actual_final_value_type" != object ]; then
64-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "The type of the final value was expected to be '$final_value_type', but was actually '$actual_final_value_type'"
77+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
6578
return
6679
fi
6780
elif [ "$final_value_type" == array ]; then
6881
if [ "$actual_final_value_type" != array ]; then
69-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "The type of the final value was expected to be '$final_value_type', but was actually '$actual_final_value_type'"
82+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
83+
return
84+
fi
85+
# TODO: currently extraneous, but needed after 'zerocopy' implementation
86+
elif [ "$final_value_type" == string ]; then
87+
if [ "$actual_final_value_type" != string ]; then
88+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' "Argument 'set-$final_value_type' was specified, but a variable with type '$actual_final_value_type' was passed"
7089
return
7190
fi
7291
else
73-
# case 'string' is handled above
74-
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type $actual_final_value_type'"
92+
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
7593
return
7694
fi
7795
fi

tests/e2e.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ load './util/init.sh'
3737
run bobject "$subcmd" 'OBJECT' '.zulu.yankee' 'xray' 'invalid'
3838

3939
assert_failure
40-
assert_line -p "Incorrect arguments for subcommand '$subcmd'"
40+
assert_line -p "Expected '4' arguments, but received '5'"
4141
done
4242
}
4343

@@ -50,7 +50,7 @@ load './util/init.sh'
5050
run bobject "$subcmd" 'OBJECT' '.zulu'
5151

5252
assert_failure
53-
assert_line -p "Incorrect arguments for subcommand '$subcmd'"
53+
assert_line -p "Expected '4' arguments, but received '3'"
5454
done
5555
}
5656

tests/set-errors.bats

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
#!/usr/bin/env bats
2+
load './util/init.sh'
3+
4+
@test "ERROR_INVALID_ARGS with \$# of 1" {
5+
run bash_object.traverse-set string
6+
7+
assert_failure
8+
assert_line -p "ERROR_INVALID_ARGS"
9+
assert_line -p ", but received '1'"
10+
}
11+
12+
@test "ERROR_INVALID_ARGS with \$# of 2" {
13+
run bash_object.traverse-set string 'OBJECT'
14+
15+
assert_failure
16+
assert_line -p "ERROR_INVALID_ARGS"
17+
assert_line -p ", but received '2'"
18+
}
19+
20+
@test "ERROR_INVALID_ARGS with \$# of 3" {
21+
run bash_object.traverse-set string 'OBJECT' '.obj'
22+
23+
assert_failure
24+
assert_line -p "ERROR_INVALID_ARGS"
25+
assert_line -p ", but received '3'"
26+
}
27+
28+
@test "ERROR_INVALID_ARGS with \$# of 5" {
29+
run bash_object.traverse-set string 'OBJECT' '.obj' obj extraneous
30+
31+
assert_failure
32+
assert_line -p "ERROR_INVALID_ARGS"
33+
assert_line -p ", but received '5'"
34+
}
35+
36+
@test "ERROR_INVALID_ARGS on empty \$1" {
37+
run bash_object.traverse-set "" 'OBJECT' '.obj' obj
38+
39+
assert_failure
40+
assert_line -p "ERROR_INVALID_ARGS"
41+
assert_line -p "'1' is empty"
42+
}
43+
44+
@test "ERROR_INVALID_ARGS on empty \$2" {
45+
run bash_object.traverse-set string "" '.obj' obj
46+
47+
assert_failure
48+
assert_line -p "ERROR_INVALID_ARGS"
49+
assert_line -p "'2' is empty"
50+
}
51+
52+
@test "ERROR_INVALID_ARGS on empty \$3" {
53+
run bash_object.traverse-set string 'OBJECT' "" obj
54+
55+
assert_failure
56+
assert_line -p "ERROR_INVALID_ARGS"
57+
assert_line -p "'3' is empty"
58+
}
59+
60+
@test "ERROR_INVALID_ARGS on empty \$4" {
61+
run bash_object.traverse-set string 'OBJECT' '.obj' ""
62+
63+
assert_failure
64+
assert_line -p "ERROR_INVALID_ARGS"
65+
assert_line -p "'4' is empty"
66+
}
67+
68+
@test "Error if root object does not exist" {
69+
export VERIFY_BASH_OBJECT=
70+
71+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
72+
73+
assert_failure
74+
assert_line -p "ERROR_VALUE_NOT_FOUND"
75+
assert_line -p "The associative array 'OBJECT' does not exist"
76+
}
77+
78+
@test "Error if root object is an array" {
79+
export VERIFY_BASH_OBJECT=
80+
declare -a OBJECT=()
81+
82+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
83+
84+
assert_failure
85+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
86+
assert_line -p "The 'root object' must be an associative array"
87+
}
88+
89+
@test "Error if root object is a string" {
90+
export VERIFY_BASH_OBJECT=
91+
declare OBJECT=
92+
93+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
94+
95+
assert_failure
96+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
97+
assert_line -p "The 'root object' must be an associative array"
98+
}
99+
100+
@test "Error if root object is a string 2" {
101+
export VERIFY_BASH_OBJECT=
102+
OBJECT=
103+
104+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
105+
106+
assert_failure
107+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
108+
assert_line -p "The 'root object' must be an associative array"
109+
}
110+
111+
@test "Error if final_value_type is 'object', but is actually nonexistent" {
112+
export VERIFY_BASH_OBJECT=
113+
declare -A OBJECT=()
114+
unset str
115+
116+
run bash_object.traverse-set object 'OBJECT' '.obj' str
117+
118+
assert_failure
119+
assert_line -p "ERROR_VALUE_NOT_FOUND"
120+
assert_line -p "The variable 'str' does not exist"
121+
}
122+
123+
@test "Error if final_value_type is 'object', but is really 'array'" {
124+
export VERIFY_BASH_OBJECT=
125+
declare -A OBJECT=()
126+
declare -a obj=()
127+
128+
run bash_object.traverse-set object 'OBJECT' '.obj' obj
129+
130+
assert_failure
131+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
132+
assert_line -p ", but a variable with type 'array' was passed"
133+
}
134+
135+
@test "Error if final_value_type is 'object', but is really 'string'" {
136+
export VERIFY_BASH_OBJECT=
137+
declare -A OBJECT=()
138+
declare obj=
139+
140+
run bash_object.traverse-set object 'OBJECT' '.obj' obj
141+
142+
assert_failure
143+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
144+
assert_line -p ", but a variable with type 'string' was passed"
145+
}
146+
147+
@test "Error if final_value_type is 'object', but is really 'other'" {
148+
export VERIFY_BASH_OBJECT=
149+
declare -A OBJECT=()
150+
declare -i obj=
151+
152+
run bash_object.traverse-set object 'OBJECT' '.obj' obj
153+
154+
assert_failure
155+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
156+
assert_line -p ", but a variable with type 'other' was passed"
157+
}
158+
159+
@test "Error if final_value_type is 'array', but is actually nonexistent" {
160+
export VERIFY_BASH_OBJECT=
161+
declare -A OBJECT=()
162+
unset str
163+
164+
run bash_object.traverse-set array 'OBJECT' '.obj' str
165+
166+
assert_failure
167+
assert_line -p "ERROR_VALUE_NOT_FOUND"
168+
assert_line -p "The variable 'str' does not exist"
169+
}
170+
171+
@test "Error if final_value_type is 'array', but is really 'object'" {
172+
export VERIFY_BASH_OBJECT=
173+
declare -A OBJECT=()
174+
declare -A obj=()
175+
176+
run bash_object.traverse-set array 'OBJECT' '.obj' obj
177+
178+
assert_failure
179+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
180+
assert_line -p ", but a variable with type 'object' was passed"
181+
}
182+
183+
@test "Error if final_value_type is 'array', but is really 'string'" {
184+
export VERIFY_BASH_OBJECT=
185+
declare -A OBJECT=()
186+
declare obj=
187+
188+
run bash_object.traverse-set array 'OBJECT' '.obj' obj
189+
190+
assert_failure
191+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
192+
assert_line -p ", but a variable with type 'string' was passed"
193+
}
194+
195+
@test "Error if final_value_type is 'array', but is really 'other'" {
196+
export VERIFY_BASH_OBJECT=
197+
declare -A OBJECT=()
198+
declare -i obj=
199+
200+
run bash_object.traverse-set array 'OBJECT' '.obj' obj
201+
202+
assert_failure
203+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
204+
assert_line -p ", but a variable with type 'other' was passed"
205+
}
206+
207+
# TODO: complete string
208+
209+
# @test "Error if 'final_value_type' is 'string', but is actually nonexistent" {
210+
# export VERIFY_BASH_OBJECT=
211+
# declare -A OBJECT=()
212+
# unset str
213+
214+
# run bash_object.traverse-set string 'OBJECT' '.obj' str
215+
216+
# assert_failure
217+
# assert_line -p "ERROR_VALUE_NOT_FOUND"
218+
# assert_line -p "The variable 'str' does not exist"
219+
# }
220+
221+
# @test "Error if 'final_value_type' is 'string', but is really 'object'" {
222+
# export VERIFY_BASH_OBJECT=
223+
# declare -A OBJECT=()
224+
# declare -A obj=()
225+
226+
# run bash_object.traverse-set string 'OBJECT' '.obj' obj
227+
228+
# assert_failure
229+
# assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
230+
# assert_line -p ", but a varaible with type 'object' was passed"
231+
# }

0 commit comments

Comments
 (0)