Skip to content

Commit ce70f07

Browse files
committed
test: Test validating correct string type
This is possible, now that we pass in the variable that contains the string, rather than the string itself (by default)
1 parent 97e0b02 commit ce70f07

File tree

4 files changed

+104
-76
lines changed

4 files changed

+104
-76
lines changed

pkg/lib/traverse-set.sh

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,37 @@ bash_object.traverse-set() {
5858

5959
# TODO: dont' do this in zerocopy mode
6060
# 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
62-
local actual_final_value_type=
63-
if ! actual_final_value_type="$(declare -p "$final_value" 2>/dev/null)"; then
64-
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The variable '$final_value' does not exist"
61+
local actual_final_value_type=
62+
if ! actual_final_value_type="$(declare -p "$final_value" 2>/dev/null)"; then
63+
bash_object.util.die 'ERROR_VALUE_NOT_FOUND' "The variable '$final_value' does not exist"
64+
return
65+
fi
66+
actual_final_value_type="${actual_final_value_type#declare -}"
67+
case "${actual_final_value_type::1}" in
68+
A) actual_final_value_type='object' ;;
69+
a) actual_final_value_type='array' ;;
70+
-) actual_final_value_type='string' ;;
71+
*) actual_final_value_type='other' ;;
72+
esac
73+
74+
if [ "$final_value_type" == object ]; then
75+
if [ "$actual_final_value_type" != object ]; then
76+
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"
6577
return
6678
fi
67-
actual_final_value_type="${actual_final_value_type#declare -}"
68-
case "${actual_final_value_type::1}" in
69-
A) actual_final_value_type='object' ;;
70-
a) actual_final_value_type='array' ;;
71-
-) actual_final_value_type='string' ;;
72-
*) actual_final_value_type='other' ;;
73-
esac
74-
75-
if [ "$final_value_type" == object ]; then
76-
if [ "$actual_final_value_type" != object ]; then
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"
78-
return
79-
fi
80-
elif [ "$final_value_type" == array ]; then
81-
if [ "$actual_final_value_type" != array ]; then
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"
89-
return
90-
fi
91-
else
92-
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
79+
elif [ "$final_value_type" == array ]; then
80+
if [ "$actual_final_value_type" != array ]; then
81+
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"
82+
return
83+
fi
84+
elif [ "$final_value_type" == string ]; then
85+
if [ "$actual_final_value_type" != string ]; then
86+
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"
9387
return
9488
fi
89+
else
90+
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
91+
return
9592
fi
9693
fi
9794

@@ -193,7 +190,9 @@ bash_object.traverse-set() {
193190
# shellcheck disable=SC2034
194191
global_array=("${array_to_copy_from[@]}")
195192
elif [ "$final_value_type" = string ]; then
196-
current_object["$key"]="$final_value"
193+
# TODO: ensure correct type
194+
local -n string_to_copy_from="$final_value"
195+
current_object["$key"]="$string_to_copy_from"
197196
else
198197
bash_object.util.die 'ERROR_INTERNAL_INVALID_PARAM' "Unexpected final_value_type '$final_value_type'"
199198
return
@@ -220,18 +219,18 @@ bash_object.traverse-set() {
220219
:
221220
elif ((i+1 == ${#REPLIES[@]})); then
222221
case "$vmd_dtype" in
223-
object)
224-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' 'Was going to set-string, but found existing object'
225-
return
226-
;;
227-
array)
228-
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' 'Was going to set-string, but found existing array'
229-
return
230-
;;
231-
*)
232-
bash_object.util.die 'ERROR_INTERNAL_INVALID_VOBJ' "Unexpected vmd_dtype '$vmd_dtype'"
233-
return
234-
;;
222+
object)
223+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' 'Was going to set-string, but found existing object'
224+
return
225+
;;
226+
array)
227+
bash_object.util.die 'ERROR_VALUE_INCORRECT_TYPE' 'Was going to set-string, but found existing array'
228+
return
229+
;;
230+
*)
231+
bash_object.util.die 'ERROR_INTERNAL_INVALID_VOBJ' "Unexpected vmd_dtype '$vmd_dtype'"
232+
return
233+
;;
235234
esac
236235
fi
237236
# Otherwise, 'key_value' is a string
@@ -246,7 +245,9 @@ bash_object.traverse-set() {
246245
return
247246
:
248247
elif ((i+1 == ${#REPLIES[@]})); then
249-
current_object["$key"]="$final_value"
248+
# TODO: ensure correct type
249+
local -n string_to_copy_from="$final_value"
250+
current_object["$key"]="$string_to_copy_from"
250251
fi
251252
fi
252253
fi

tests/e2e.bats

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ load './util/init.sh'
5757
@test "get-string simple parser" {
5858
declare -A OBJECT=()
5959
declare -A subobj=()
60+
str='MEOW'
6061

6162
bobject set-object 'OBJECT' '.zulu' subobj
62-
bobject set-string 'OBJECT' '.zulu.yankee' 'MEOW'
63+
bobject set-string 'OBJECT' '.zulu.yankee' str
6364
bobject get-string 'OBJECT' '.zulu.yankee'
6465

6566
assert [ "$REPLY" = 'MEOW' ]
@@ -68,9 +69,10 @@ load './util/init.sh'
6869
@test "get-string advanced parser" {
6970
declare -A OBJECT=()
7071
declare -A subobj=()
72+
str='MEOW'
7173

7274
bobject set-object 'OBJECT' '.zulu' subobj
73-
bobject set-string 'OBJECT' '.["zulu"].["yankee"]' 'MEOW'
75+
bobject set-string 'OBJECT' '.["zulu"].["yankee"]' str
7476
bobject get-string 'OBJECT' '.["zulu"].["yankee"]'
7577

7678
assert [ "$REPLY" = 'MEOW' ]

tests/set-errors.bats

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
11
#!/usr/bin/env bats
22
load './util/init.sh'
33

4-
@test "ERROR_INVALID_ARGS with \$# of 1" {
4+
@test "Error with \$# of 1" {
55
run bash_object.traverse-set string
66

77
assert_failure
88
assert_line -p "ERROR_INVALID_ARGS"
99
assert_line -p ", but received '1'"
1010
}
1111

12-
@test "ERROR_INVALID_ARGS with \$# of 2" {
12+
@test "Error with \$# of 2" {
1313
run bash_object.traverse-set string 'OBJECT'
1414

1515
assert_failure
1616
assert_line -p "ERROR_INVALID_ARGS"
1717
assert_line -p ", but received '2'"
1818
}
1919

20-
@test "ERROR_INVALID_ARGS with \$# of 3" {
20+
@test "Error with \$# of 3" {
2121
run bash_object.traverse-set string 'OBJECT' '.obj'
2222

2323
assert_failure
2424
assert_line -p "ERROR_INVALID_ARGS"
2525
assert_line -p ", but received '3'"
2626
}
2727

28-
@test "ERROR_INVALID_ARGS with \$# of 5" {
28+
@test "Error with \$# of 5" {
2929
run bash_object.traverse-set string 'OBJECT' '.obj' obj extraneous
3030

3131
assert_failure
3232
assert_line -p "ERROR_INVALID_ARGS"
3333
assert_line -p ", but received '5'"
3434
}
3535

36-
@test "ERROR_INVALID_ARGS on empty \$1" {
36+
@test "Error on empty \$1" {
3737
run bash_object.traverse-set "" 'OBJECT' '.obj' obj
3838

3939
assert_failure
4040
assert_line -p "ERROR_INVALID_ARGS"
4141
assert_line -p "'1' is empty"
4242
}
4343

44-
@test "ERROR_INVALID_ARGS on empty \$2" {
44+
@test "Error on empty \$2" {
4545
run bash_object.traverse-set string "" '.obj' obj
4646

4747
assert_failure
4848
assert_line -p "ERROR_INVALID_ARGS"
4949
assert_line -p "'2' is empty"
5050
}
5151

52-
@test "ERROR_INVALID_ARGS on empty \$3" {
52+
@test "Error on empty \$3" {
5353
run bash_object.traverse-set string 'OBJECT' "" obj
5454

5555
assert_failure
5656
assert_line -p "ERROR_INVALID_ARGS"
5757
assert_line -p "'3' is empty"
5858
}
5959

60-
@test "ERROR_INVALID_ARGS on empty \$4" {
60+
@test "Error on empty \$4" {
6161
run bash_object.traverse-set string 'OBJECT' '.obj' ""
6262

6363
assert_failure
@@ -204,28 +204,50 @@ load './util/init.sh'
204204
assert_line -p ", but a variable with type 'other' was passed"
205205
}
206206

207-
# TODO: complete string
207+
@test "Error if final_value_type is 'string', but is actually nonexistent" {
208+
export VERIFY_BASH_OBJECT=
209+
declare -A OBJECT=()
210+
unset str
211+
212+
run bash_object.traverse-set string 'OBJECT' '.obj' str
213+
214+
assert_failure
215+
assert_line -p "ERROR_VALUE_NOT_FOUND"
216+
assert_line -p "The variable 'str' does not exist"
217+
}
218+
219+
@test "Error if final_value_type is 'string', but is really 'object'" {
220+
export VERIFY_BASH_OBJECT=
221+
declare -A OBJECT=()
222+
declare -A obj=()
223+
224+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
225+
226+
assert_failure
227+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
228+
assert_line -p ", but a variable with type 'object' was passed"
229+
}
208230

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
231+
@test "Error if final_value_type is 'string', but is really 'array'" {
232+
export VERIFY_BASH_OBJECT=
233+
declare -A OBJECT=()
234+
declare -a obj=()
213235

214-
# run bash_object.traverse-set string 'OBJECT' '.obj' str
236+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
215237

216-
# assert_failure
217-
# assert_line -p "ERROR_VALUE_NOT_FOUND"
218-
# assert_line -p "The variable 'str' does not exist"
219-
# }
238+
assert_failure
239+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
240+
assert_line -p ", but a variable with type 'array' was passed"
241+
}
220242

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=()
243+
@test "Error if final_value_type is 'string', but is really 'other'" {
244+
export VERIFY_BASH_OBJECT=
245+
declare -A OBJECT=()
246+
declare -i obj=
225247

226-
# run bash_object.traverse-set string 'OBJECT' '.obj' obj
248+
run bash_object.traverse-set string 'OBJECT' '.obj' obj
227249

228-
# assert_failure
229-
# assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
230-
# assert_line -p ", but a varaible with type 'object' was passed"
231-
# }
250+
assert_failure
251+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
252+
assert_line -p ", but a variable with type 'other' was passed"
253+
}

tests/set-string.bats

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ load './util/init.sh'
4949

5050
@test "correctly sets string at root" {
5151
declare -A OBJECT=()
52+
str='my_value'
5253

53-
bash_object.traverse-set string 'OBJECT' '.my_key' 'my_value'
54+
bash_object.traverse-set string 'OBJECT' '.my_key' str
5455
assert [ "${OBJECT[my_key]}" = 'my_value' ]
5556

5657
bash_object.traverse-get string 'OBJECT' '.my_key'
@@ -60,19 +61,21 @@ load './util/init.sh'
6061
@test "correctly sets string in object" {
6162
declare -A SUB_OBJECT=()
6263
declare -A OBJECT=([tau]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
64+
str='phi'
6365

64-
bash_object.traverse-set string 'OBJECT' '.tau.upsilon' 'phi'
66+
bash_object.traverse-set string 'OBJECT' '.tau.upsilon' str
6567
bash_object.traverse-get string 'OBJECT' '.tau.upsilon'
6668
assert [ "$REPLY" = 'phi' ]
6769
}
6870

6971
@test "correctly sets string in object 2" {
7072
declare -A obj=()
7173
declare -A OBJECT=()
74+
str='phi'
7275

7376
bash_object.traverse-set object 'OBJECT' '.tau' obj
7477

75-
bash_object.traverse-set string 'OBJECT' '.tau.upsilon' 'phi'
78+
bash_object.traverse-set string 'OBJECT' '.tau.upsilon' str
7679
bash_object.traverse-get string 'OBJECT' '.tau.upsilon'
7780
assert [ "$REPLY" = 'phi' ]
7881
}

0 commit comments

Comments
 (0)