Skip to content

Commit

Permalink
Run scripts, add example folder, better handling of fatal errors
Browse files Browse the repository at this point in the history
Added the ability to run scripts, not only modules from the library.
Now fatal errors when handling expressions will exit.

This commit also fixes Txt_add, which had a bug when adding (concat
to end) to a string that already exists.
  • Loading branch information
alganet committed Jul 8, 2024
1 parent 55e5d3d commit f80d998
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ _coral_ is meant to solve the script portability problem the hard way: by writin

There is no compilation or build step. Once you download it, you're good to go.

# Testing
```sh
$ ./coral example/hello_name.sh "World"
Hello, World!
```

## Testing

Run all tests locally against the default shell:

Expand All @@ -22,7 +27,7 @@ Run all tests locally against the default shell:
Run a single test. For example, the pseudoarray implementation:

```sh
sh coral tap 'test/_idiom/005-Arr.sh'
./coral tap 'test/_idiom/005-Arr.sh'
```

Run all tests inside an ephemeral docker container against all shells:
Expand Down
9 changes: 9 additions & 0 deletions example/hello_name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# When loading scripts, the main function is the same
# as the script file name.

hello_name () {
# You must declare all variables as local
local name=$1

_print "Hello, $name!\n"
}
27 changes: 19 additions & 8 deletions idiom/data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exp () {
_e="${1:-}${1:+ }$_R"
shift
;;
_[TLSA]*)
_[TLSA][0-9]*)
_e="${_e:-}${_e:+ }$_t"
;;
[A-Z][a-z]*)
Expand All @@ -44,13 +44,14 @@ exp () {
eval "_e=\"\${_e:-}\${_e:+ }\$$_t\""
;;
*)
ERROR $_t
_write "bad expression: '$_t'"
exit 1
;;
esac
done
case $_e in
_[TLSA]*) _R=$_e ;;
*) eval $_e ;;
_[TLSA][0-9]*) _R=$_e ;;
*) eval $_e ;;
esac
}

Expand All @@ -69,7 +70,7 @@ dump () {
local dump= item= len=0 count=0
case $1 in
_T*) eval dump=\"\'\$$1\'\" ;;
_[LS]*)
_[LS][0-9]*)
eval "REPLY=\"\$$1\""
case $1 in
_L*) dump="[ Lst " ;;
Expand All @@ -82,7 +83,7 @@ dump () {
done
dump="$dump]"
;;
_A*)
_A[0-9]*)
eval "len=\"\$(($1))\""
dump="[ Arr "
while test $count -lt $len
Expand All @@ -93,6 +94,10 @@ dump () {
done
dump="$dump]"
;;
*)
_write "bad data: '$1'"
exit 1
;;
esac
REPLY="$dump"
}
Expand All @@ -111,7 +116,7 @@ toenv () {
done
eval "dump=\"\${dump}$1='\$$1'\"\${__EOL__}"
;;
_A*)
_A[0-9]*)
eval dump=\"$1=\'\$$1\'\${__EOL__}\"
eval "len=\"\$(($1))\""
while test $count -lt $len
Expand All @@ -122,6 +127,10 @@ toenv () {
count=$((count + 1))
done
;;
*)
_write "bad data: '$1'"
exit 1
;;
esac
REPLY="$dump"
}
Expand All @@ -134,7 +143,9 @@ Txt () {
}

Txt_add () {
eval "$_R=\${*:-}"
_R=$1
shift
eval "$_R=\"\${$_R}\${*:-}\""
}

# The Lst pseudotype constructor
Expand Down
13 changes: 11 additions & 2 deletions idiom/wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ then
REPLY=
fi

test $# = 0 || require "$1"
shift
if test $# -gt 0
then
if ! test -f "$1"
then require "$1" # Main argument not a script, try to load a module
else
. "$1"
REPLY=${1#*\/}
REPLY=${REPLY%\.*}
fi
shift
fi
unset -f require
"${REPLY:-}" "${@:-}"
22 changes: 19 additions & 3 deletions test/_idiom/002-Txt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
test_Txt_noargs () {
Txt
eval deref=\$$_R
tap_assert "$deref" ""
tap_assert "" "$deref"
}

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

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

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

test_Txt_add_to_empty_string () {
Txt
eval deref=\$$_R
Txt_add $_R "Hi"
eval deref=\$$_R
tap_assert "Hi" "$deref"
}

0 comments on commit f80d998

Please sign in to comment.