Skip to content

Commit

Permalink
Add parameterized test
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Aug 13, 2019
1 parent d575f94 commit c9c74ea
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 41 deletions.
4 changes: 4 additions & 0 deletions lib/bootstrap.sh
Expand Up @@ -55,6 +55,10 @@ if [ "${SHELLSPEC_DEFECT_READONLY:-}" ]; then
alias readonly=''
fi

shellspec_parameters() {
"shellspec_example$SHELLSPEC_BLOCK_NO"
}

shellspec_load_requires() {
shellspec_reset_params '$1' ':'
eval "$SHELLSPEC_RESET_PARAMS"
Expand Down
45 changes: 39 additions & 6 deletions lib/core/dsl.sh
@@ -1,4 +1,4 @@
#shellcheck shell=sh
#shellcheck shell=sh disable=SC2004

SHELLSPEC_DESCRIPTION=''
SHELLSPEC_PATH_ALIAS=:
Expand All @@ -25,7 +25,10 @@ shellspec_finished() {
}

shellspec_yield() {
"shellspec_yield$SHELLSPEC_BLOCK_NO"
case $# in
0) "shellspec_yield$SHELLSPEC_BLOCK_NO" ;;
*) "shellspec_yield$SHELLSPEC_BLOCK_NO" "$@" ;;
esac
# shellcheck disable=SC2034
SHELLSPEC_LINENO=''
}
Expand All @@ -41,7 +44,7 @@ shellspec_perform() {

shellspec_end() {
# shellcheck disable=SC2034
SHELLSPEC_EXAMPLE_COUNT=$1
SHELLSPEC_EXAMPLE_COUNT=${1:-}
shellspec_output END
}

Expand All @@ -55,8 +58,23 @@ shellspec_example_group() {
shellspec_yield
}

shellspec_example_block() {
shellspec_parameters
}

shellspec_parameterized_example() {
(
case $# in
0) "shellspec_example$SHELLSPEC_BLOCK_NO" ;;
*) "shellspec_example$SHELLSPEC_BLOCK_NO" "$@" ;;
esac
)
SHELLSPEC_EXAMPLE_NO=$(($SHELLSPEC_EXAMPLE_NO + 1))
}

shellspec_example() {
shellspec_description "example" "${1:-}"
[ $# -gt 0 ] && shift

if [ "$SHELLSPEC_ENABLED" ] && [ "$SHELLSPEC_FILTER" ]; then
if [ "$SHELLSPEC_DRYRUN" ]; then
Expand All @@ -67,11 +85,23 @@ shellspec_example() {
case $- in
*e*)
set +e
(set -e; shellspec_invoke_example )
(
set -e
case $# in
0) shellspec_invoke_example ;;
*) shellspec_invoke_example "$@" ;;
esac
)
set -e -- $?
;;
*)
(set -e; shellspec_invoke_example )
(
set -e
case $# in
0) shellspec_invoke_example ;;
*) shellspec_invoke_example "$@" ;;
esac
)
set -- $?
esac
if [ "$1" -ne 0 ]; then
Expand Down Expand Up @@ -100,7 +130,10 @@ shellspec_invoke_example() {
return 0
fi
shellspec_output_if PENDING ||:
shellspec_yield
case $# in
0) shellspec_yield ;;
*) shellspec_yield "$@" ;;
esac
if ! shellspec_call_after_hooks; then
SHELLSPEC_LINENO=$SHELLSPEC_LINENO_BEGIN-$SHELLSPEC_LINENO_END
shellspec_output FAILED_AFTER_HOOK
Expand Down
4 changes: 2 additions & 2 deletions lib/core/outputs.sh
Expand Up @@ -19,8 +19,8 @@ shellspec_output_END() {

shellspec_output_EXAMPLE() {
shellspec_output_example "id:${SHELLSPEC_EXAMPLE_ID:-}" \
"block_no:${SHELLSPEC_BLOCK_NO:-}" "focused:${SHELLSPEC_FOCUSED:-}" \
"description:$SHELLSPEC_DESCRIPTION"
"block_no:${SHELLSPEC_BLOCK_NO:-}" "example_no:${SHELLSPEC_EXAMPLE_NO:-}" \
"focused:${SHELLSPEC_FOCUSED:-}" "description:$SHELLSPEC_DESCRIPTION"
}

shellspec_output_EVALUATION() {
Expand Down
1 change: 1 addition & 0 deletions lib/libexec/grammar.sh
Expand Up @@ -65,6 +65,7 @@ mapping() {
Data ) data raw "$2" ;;
Data:raw ) data raw "$2" ;;
Data:expand ) data expand "$2" ;;
Parameters ) parameters "$2" ;;
Include ) include "$2" ;;
%text ) text_begin raw "$2" ;;
%text:raw ) text_begin raw "$2" ;;
Expand Down
22 changes: 14 additions & 8 deletions lib/libexec/reporter/documentation_formatter.sh
Expand Up @@ -21,14 +21,20 @@ documentation_each() {
result)
_id=$_last_id _current_id=$field_id
_description=$field_description _indent='' _last_id=$field_id
while [ "${_id%%-*}" = "${_current_id%%-*}" ]; do
_id=${_id#*-} _current_id=${_current_id#*-}
_description=${_description#*$VT} _indent="${_indent} "
done
until case $_description in (*$VT*) false; esac; do
documentation '+=' "${_indent}${_description%%$VT*}${LF}"
_description=${_description#*$VT} _indent="${_indent} "
done
if [ "$_id" = "$_current_id" ]; then
until case $_description in (*$VT*) false; esac; do
_description=${_description#*$VT} _indent="${_indent} "
done
else
while [ "${_id%%-*}" = "${_current_id%%-*}" ]; do
_id=${_id#*-} _current_id=${_current_id#*-}
_description=${_description#*$VT} _indent="${_indent} "
done
until case $_description in (*$VT*) false; esac; do
documentation '+=' "${_indent}${_description%%$VT*}${LF}"
_description=${_description#*$VT} _indent="${_indent} "
done
fi

set -- "${_indent}${field_color}${_description}"
[ "$example_index" ] && set -- "$@" "($field_note - $example_index)"
Expand Down
39 changes: 32 additions & 7 deletions lib/libexec/translator.sh
Expand Up @@ -6,8 +6,8 @@ use constants trim match
load grammar

initialize() {
lineno=0 block_no=0 example_no=0 skip_id=0 error='' focused=''
_block_no=0 _block_no_stack=''
lineno=0 block_no=0 example_no=1 skip_id=0 error='' focused=''
_block_no=0 _block_no_stack='' _parameter_count_stack=''
}

finalize() {
Expand Down Expand Up @@ -88,6 +88,7 @@ block_example_group() {
eval trans block_example_group ${1+'"$@"'}

_block_no_stack="$_block_no_stack $_block_no" filter=''
_parameter_count_stack="$_parameter_count_stack $parameter_count"
}

block_example() {
Expand All @@ -104,14 +105,16 @@ block_example() {
check_filter "$1" && filter=1

increase_example_id
_block_no=$(($_block_no + 1)) example_no=$(($example_no + 1))
_block_no=$(($_block_no + 1))
block_no=$_block_no lineno_begin=$lineno
eval "block_lineno_begin${block_no}=$lineno"

eval trans block_example ${1+'"$@"'}

_block_no_stack="$_block_no_stack $_block_no" filter=''
inside_of_example="yes"
_block_no_stack="$_block_no_stack $_block_no"
example_no=$(($example_no + $parameter_count))
_parameter_count_stack="$_parameter_count_stack $parameter_count"
filter='' inside_of_example="yes"
}

block_end() {
Expand All @@ -133,7 +136,9 @@ block_end() {
eval trans block_end ${1+'"$@"'}
enabled=''

_block_no_stack="${_block_no_stack% *}"
_block_no_stack=${_block_no_stack% *}
parameter_count=${_parameter_count_stack##* }
_parameter_count_stack=${_parameter_count_stack% *}
inside_of_example=""
}

Expand Down Expand Up @@ -217,6 +222,26 @@ out() {
eval trans out ${1+'"$@"'}
}

parameters() {
trans parameters_begin
parameter_count=0
while IFS= read -r line || [ "$line" ]; do
lineno=$(($lineno + 1))
trim line "$line"
case $line in
End | End\ * ) break ;;
*)
trans parameter "shellspec_parameterized_example $line"
parameter_count=$(($parameter_count + 1))
until case $line in (*\\) false; esac; do
IFS= read -r line
eval trans parameter "$line"
done
esac
done
trans parameters_end "$parameter_count"
}

constant() {
if [ "$_block_no_stack" ]; then
syntax_error "Constant should be defined outside of Example Group/Example"
Expand Down Expand Up @@ -280,7 +305,7 @@ remove_from_ranges() {
}

translate() {
example_id='' inside_of_example='' inside_of_text=''
example_id='' inside_of_example='' inside_of_text='' parameter_count=1
while IFS= read -r line || [ "$line" ]; do
lineno=$(($lineno + 1)) work=''
trim work "$line"
Expand Down
13 changes: 7 additions & 6 deletions libexec/shellspec-list.sh
Expand Up @@ -24,7 +24,7 @@ trans_block_example() {
putsn "block${block_no}() { "
putsn "LINENO_BEGIN=$lineno_begin" "EXAMPLE_ID=$example_id"
putsn "FILTER=\${FILTER}${filter:-0}" "ENABLED=\${ENABLED}${enabled:-0}"
putsn "yield"
putsn "yield $parameter_count"
}

trans_block_end() {
Expand All @@ -36,7 +36,7 @@ trans_block_end() {
}

yield() {
case $ENABLED in (*1*) case $FILTER in (*1*) proc; esac; esac
case $ENABLED in (*1*) case $FILTER in (*1*) proc "$1"; esac; esac
}

syntax_error() {
Expand Down Expand Up @@ -69,12 +69,13 @@ case ${SHELLSPEC_LIST%:*} in
specfile() { putsn "$2"; }; ;;
examples | '')
specfile() { list_code eval "$@"; }
example() { examples=$(($examples + 1)); }
example() { examples=$(($examples + $1)); }
# shellcheck disable=SC2153
case ${SHELLSPEC_LIST#examples:} in
id | examples) proc() { example; putsn "$SPECFILE:@$EXAMPLE_ID"; }; ;;
lineno) proc() { example; putsn "$SPECFILE:$LINENO_BEGIN"; }; ;;
'') proc() { example; }; ;;
id ) proc() { example "$1"; putsn "$SPECFILE:@$EXAMPLE_ID"; }; ;;
examples) proc() { example "$1"; putsn "$SPECFILE:@$EXAMPLE_ID"; }; ;;
lineno) proc() { example "$1"; putsn "$SPECFILE:$LINENO_BEGIN"; }; ;;
'') proc() { example "$1"; }; ;;
esac
esac

Expand Down
15 changes: 8 additions & 7 deletions libexec/shellspec-reporter.sh
Expand Up @@ -22,8 +22,8 @@ color_constants "${SHELLSPEC_COLOR:-}"
exit_status=0 found_focus='' no_examples='' aborted=1 coverage_failed=''
fail_fast='' fail_fast_count=${SHELLSPEC_FAIL_FAST_COUNT:-}
current_example_index=0 example_index=''
last_block_no='' last_skip_id='' not_enough_examples=''
field_type='' field_tag='' field_block_no='' field_focused=''
last_example_no='' last_skip_id='' not_enough_examples=''
field_type='' field_tag='' field_example_no='' field_focused=''
field_conditional='' field_skipid='' field_pending=''

# shellcheck disable=SC2034
Expand Down Expand Up @@ -68,7 +68,7 @@ parse_fields() {
each_line() {
case $field_type in
begin)
field_example_count='' last_block_no=0 last_skip_id=''
field_example_count='' last_example_no=0 last_skip_id=''
inc specfile_count
# shellcheck disable=SC2034
example_count_per_file=0 succeeded_count_per_file=0 \
Expand All @@ -78,12 +78,13 @@ each_line() {
example)
# shellcheck disable=SC2034
field_evaluation='' field_pending=''
if [ "$field_block_no" -le "$last_block_no" ]; then
abort "Illegal executed the same block (that in a loop?)" \
"in ${field_specfile:-}" "line ${field_lineno_range:-}"
if [ "$field_example_no" -le "$last_example_no" ]; then
abort "${LF}Illegal executed the same example" \
"(did you execute in a loop?) in ${field_specfile:-}" \
"line ${field_lineno_range:-}"
fi
[ "$field_focused" = "focus" ] && found_focus=1
example_index='' last_block_no=$field_block_no
example_index='' last_example_no=$field_example_no
eval "profiler_line$example_count=\$field_specfile:\$field_lineno_range"
;;
statement)
Expand Down
28 changes: 23 additions & 5 deletions libexec/shellspec-translate.sh
Expand Up @@ -39,7 +39,10 @@ trans_block_example() {
[ "$focused" ] && putsn "SHELLSPEC_FOCUSED=$focused"
[ "$filter" ] && putsn "SHELLSPEC_FILTER=$filter"
[ "$enabled" ] && putsn "SHELLSPEC_ENABLED=$enabled"
putsn "shellspec_example $1"
putsn "shellspec_example_block"
putsn "}; shellspec_example${block_no}() { "
putsn "if [ \$# -eq 0 ]; then shellspec_example $1"
putsn "else shellspec_example $1 \"\$@\"; fi"
putsn "}; shellspec_yield${block_no}() { :;"
}

Expand Down Expand Up @@ -128,6 +131,18 @@ trans_out() {
esac
}

trans_parameters_begin() {
putsn "shellspec_parameters() {"
}

trans_parameter() {
putsn "$1"
}

trans_parameters_end() {
putsn "}; PARAMETER_COUNT=$1"
}

trans_constant() {
( eval "putsn $1=\\'$2\\'" ) ||:
}
Expand Down Expand Up @@ -179,10 +194,10 @@ putsn "shellspec_metadata $metadata"

specfile() {
(
specfile=$2 ranges="${3:-}" example_count=''
specfile=$2 ranges=${3:-} run_all=''
escape_quote specfile
[ "$ranges" ] && enabled='' || enabled=1
[ "${enabled}" ] && [ "${filter}" ] && example_count=0
[ "$enabled" ] && [ "$filter" ] && run_all=1

putsn "shellspec_marker '$specfile' ---"
putsn "(shellspec_begin '$specfile' '$spec_no'"
Expand All @@ -193,8 +208,11 @@ specfile() {
translate < "$2"
putsn "shellspec_marker '$specfile' EOF"
finalize
[ "$example_count" ] && example_count=$example_no
putsn "shellspec_end '$example_count')"
if [ "$run_all" ]; then
putsn "shellspec_end $(($example_no - 1)))"
else
putsn "shellspec_end)"
fi
)
spec_no=$(($spec_no + 1))
}
Expand Down

0 comments on commit c9c74ea

Please sign in to comment.