Skip to content

Commit

Permalink
exp, Map, toenv, tap_tap usability, examples and test improvements
Browse files Browse the repository at this point in the history
exp is now able to create Txt entries on the fly using ="value",
removing the need for intermediate user variables for it.

tap_tap had a bug that prevented failed tests with no output from
reporting, this was fixed

toenv was not properly serializing all references to a Map, this
was fixed

A mini how-to on working with datastructures is now on the examples
folder.
  • Loading branch information
alganet committed Jul 9, 2024
1 parent bf82653 commit a82abe7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 36 deletions.
25 changes: 25 additions & 0 deletions example/data_structures.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This example is subject to change

data_structures () {
local breakfast= fruit_list= fruits_iterator= fruit_reference= fruit=

# Declaring and working with pseudotypes
val fruit_list = [ Lst ="banana" ="apple" ="orange" ="grape" ]
val breakfast = [ Map :beverage ="coffee" :fruit $fruit_list ]

# Prints the internal data structure
toenv $breakfast
_print "## Internals\n$REPLY"

# Dumps it in a human readable way
dump $breakfast
_print "## Dumped\n$REPLY\n\n"

# How to iterate over a traversable type
val fruits_iterator =@ $fruit_list
for fruit_reference in $fruits_iterator
do
val fruit =@ $fruit_reference
_print "Fruit was served: $fruit\n"
done
}
5 changes: 5 additions & 0 deletions idiom/data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ exp () {
Txt|Lst|Set|Arr|Map)
_e="${_e:-}${_e:+ }$_t"
;;
=*)
Txt "${_t#\=}"
_e="${_e:-}${_e:+ }$_R"
;;
*)
_write "bad expression: '$_t'"
exit 1
Expand Down Expand Up @@ -145,6 +149,7 @@ toenv () {
do
eval "toenv \$$1i$item"
eval "dump=\"\${dump}$1i$item=\$$1i$item\"\${__EOL__}"
dump="$dump$REPLY${__EOL__}"
done
;;
*)
Expand Down
8 changes: 3 additions & 5 deletions module/tap/tap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ tap_tap () {
val test_name =@ $1
val test_line =@ $2
test_count=$((test_count + 1))
errors="$(set +x; $test_name 2>&1 || :)"
errors="$(set +x; $test_name 2>&1 || :)" || :

if test -z "$errors"
then _print "ok ${test_count} - $test_name\n"
else
fail_count=$((fail_count + 1))

_print \
"not ok ${test_count} - $test_name" \
"${errors}" \
" # at: $file:$test_line${__TAB__}\n\n"
errors="${errors}${__EOL__} # at: $file:$test_line${__TAB__}${__EOL__}${__EOL__}"
_write "not ok ${test_count} - $test_name${__EOL__}${errors}"
fi

done
Expand Down
24 changes: 12 additions & 12 deletions test/_idiom/001-stdout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
# SPDX-License-Identifier: ISC

test_write () {
tap_assert '' "$(_write)"
tap_assert '\0101' "$(_write '\0101')"
tap_assert 'x\\x' "$(_write 'x\\x')"
tap_assert 'x\nx' "$(_write 'x\nx')"
tap_assert 'x\tx' "$(_write 'x\tx')"
tap_assert "123123" "$(_write 123; _write 123)"
tap_assert '' "$(_write)"
tap_assert '\0101' "$(_write '\0101')"
tap_assert 'x\\x' "$(_write 'x\\x')"
tap_assert 'x\nx' "$(_write 'x\nx')"
tap_assert 'x\tx' "$(_write 'x\tx')"
tap_assert "123123" "$(_write 123; _write 123)"
}

test_print () {
tap_assert '' "$(_print)"
tap_assert 'A' "$(_print '\0101')"
tap_assert 'x\x' "$(_print 'x\\x')"
tap_assert "x${__EOL__}x" "$(_print 'x\nx')"
tap_assert "x${__TAB__}x" "$(_print 'x\tx')"
tap_assert "123123" "$(_print 123; _print 123)"
tap_assert '' "$(_print)"
tap_assert 'A' "$(_print '\0101')"
tap_assert 'x\x' "$(_print 'x\\x')"
tap_assert "x${__EOL__}x" "$(_print 'x\nx')"
tap_assert "x${__TAB__}x" "$(_print 'x\tx')"
tap_assert "123123" "$(_print 123; _print 123)"
}

test_inline_unary_zerolength () {
Expand Down
24 changes: 12 additions & 12 deletions test/_idiom/002-Txt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@

test_Txt_noargs () {
Txt
eval deref=\$$_R
tap_assert "" "$deref"
dump $_R
tap_assert "''" "$REPLY"
}

test_Txt_unary_zerolength () {
Txt ""
eval deref=\$$_R
tap_assert "" "$deref"
dump $_R
tap_assert "''" "$REPLY"
}

test_Txt_simple () {
Txt "Some text"
eval deref=\$$_R
tap_assert "Some text" "$deref"
dump $_R
tap_assert "'Some text'" "$REPLY"
}

test_Txt_add () {
Txt "Some text"
eval deref=\$$_R
dump $_R
Txt_add $_R " and some more"
eval deref=\$$_R
tap_assert "Some text and some more" "$deref"
dump $_R
tap_assert "'Some text and some more'" "$REPLY"
}

test_Txt_add_to_empty_string () {
Txt
eval deref=\$$_R
dump $_R
Txt_add $_R "Hi"
eval deref=\$$_R
tap_assert "Hi" "$deref"
dump $_R
tap_assert "'Hi'" "$REPLY"
}
19 changes: 13 additions & 6 deletions test/_idiom/007-val.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
test_val_noargs () {
local my_variable=
val my_variable =
eval deref=\$$my_variable
tap_assert "" "$deref"
dump $my_variable
tap_assert "''" "$REPLY"
}

test_val_unary_zerolength () {
local my_variable=
val my_variable = ""
eval deref=\$$my_variable
tap_assert "" "$deref"
dump $my_variable
tap_assert "''" "$REPLY"
}

test_val_simple () {
local my_variable=
val my_variable = "Some text"
eval deref=\$$my_variable
tap_assert "Some text" "$deref"
dump $my_variable
tap_assert "'Some text'" "$REPLY"
}

test_val_exp_Txt_creation () {
local my_variable=
val my_variable = [ Lst ="foo" ="bar" ]
dump $my_variable
tap_assert "[ Lst 'foo' 'bar' ]" "$REPLY"
}
2 changes: 1 addition & 1 deletion test/_idiom/008-dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ test_toenv_Map () {
val profile = [ Map :fname $first_name :lname $last_name ]
local r3=$_R
toenv $profile
tap_assert "$r3='fname${__EOL__}lname'${__EOL__}${r3}ifname=$r1${__EOL__}${r3}ilname=$r2${__EOL__}" "$REPLY"
tap_assert "${r3}='fname${__EOL__}lname'${__EOL__}${r3}ifname=$r1${__EOL__}$r1='Lorem'${__EOL__}${r3}ilname=$r2${__EOL__}$r2='Ipsum'${__EOL__}" "$REPLY"
}

0 comments on commit a82abe7

Please sign in to comment.