Skip to content

Commit

Permalink
Improving *_add and testing more scenarios
Browse files Browse the repository at this point in the history
Lst_add had a bug when adding to existing lists that was not caught
before because it was being tested indirectly.

Arr_add had a bug on the index counter that was not caught because
it was not used anywhere else but the tests, which were incomplete.
  • Loading branch information
alganet committed Jul 8, 2024
1 parent da14b38 commit 55e5d3d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
14 changes: 4 additions & 10 deletions idiom/data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Lst () {
Lst_add () {
_R=$1
shift
eval "$_R=\"\${$_R:-}\$*\""
eval "$_R=\"\$$_R\${$_R:+\$__EOL__}\$*\""
}

# The Set pseudotype constructor
Expand All @@ -173,19 +173,13 @@ Set_add () {
Arr () {
_A=$((_A + 1))
_R=_A$_A
eval "$_R=-1"
if test $# -gt 0
then
eval "$_R=-1"
Arr_add $_R "$@"
else
eval "$_R=0"
fi
eval "$_R=0"
test $# -lt 1 || Arr_add $_R "$@"
}

Arr_add () {
_R=$1
eval "$_R=$(($_R + $#))"
eval "$_R=$(($_R + $# - 1))"
while test $# -gt 1
do shift ; eval "${_R}i$(($_R - $#))=\$1"
done
Expand Down
14 changes: 14 additions & 0 deletions test/_idiom/003-Lst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ test_Lst_simple () {
eval deref=\$$_R
tap_assert "foo${__EOL__}bar${__EOL__}baz" "$deref"
}

test_Lst_add_to_existing () {
Lst foo bar baz
Lst_add $_R qux
eval deref=\$$_R
tap_assert "foo${__EOL__}bar${__EOL__}baz${__EOL__}qux" "$deref"
}

test_Lst_add_to_existing_empty_list () {
Lst
Lst_add $_R foo bar baz
eval deref=\$$_R
tap_assert "foo${__EOL__}bar${__EOL__}baz" "$deref"
}
14 changes: 14 additions & 0 deletions test/_idiom/004-Set.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ test_Set_uniqueness_C () {
eval deref=\$$_R
tap_assert "bar${__EOL__}foo" "$deref"
}

test_Set_add_to_existing () {
Set foo bar baz
Set_add $_R baz foo qux bar qux
eval deref=\$$_R
tap_assert "foo${__EOL__}bar${__EOL__}baz${__EOL__}qux" "$deref"
}

test_Set_add_to_existing_empty_list () {
Set
Set_add $_R foo bar baz
eval deref=\$$_R
tap_assert "foo${__EOL__}bar${__EOL__}baz" "$deref"
}
30 changes: 30 additions & 0 deletions test/_idiom/005-Arr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,40 @@ test_Arr_unary_zerolength () {
Arr ""
eval deref=\$$_R
tap_assert "1" "$deref"
eval deref=\$${_R}i0
tap_assert "" "$deref"
}

test_Arr_simple () {
Arr foo bar baz
eval deref=\$$_R
tap_assert "3" "$deref"
eval deref=\$${_R}i0
tap_assert "foo" "$deref"
eval deref=\$${_R}i1
tap_assert "bar" "$deref"
eval deref=\$${_R}i2
tap_assert "baz" "$deref"
}

test_Arr_add_to_existing () {
Arr foo bar baz
Arr_add $_R qux
eval deref=\$$_R
tap_assert "4" "$deref"
eval deref=\$${_R}i3
tap_assert "qux" "$deref"
}

test_Arr_add_to_existing_empty_list () {
Arr
Arr_add $_R foo bar baz
eval deref=\$$_R
tap_assert "3" "$deref"
eval deref=\$${_R}i0
tap_assert "foo" "$deref"
eval deref=\$${_R}i1
tap_assert "bar" "$deref"
eval deref=\$${_R}i2
tap_assert "baz" "$deref"
}

0 comments on commit 55e5d3d

Please sign in to comment.