Skip to content

Commit

Permalink
Added missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UrsaDK committed Dec 17, 2024
1 parent 94d18ac commit a892118
Show file tree
Hide file tree
Showing 9 changed files with 577 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/bats/github_17a.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load ../test_helper
export GETOPTS_LONG_TEST_BIN='getopts_long-with_extdebug'

@test "${FEATURE}: toggles, silent" {
compare '-t -t -- user_arg' \
'toggles'
}
@test "${FEATURE}: toggles, verbose" {
compare '-t -t -- user_arg' \
'toggles'
}

@test "${FEATURE}: options, silent" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'options'
}
@test "${FEATURE}: options, verbose" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'options'
}

@test "${FEATURE}: variables, silent" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'variables'
}
@test "${FEATURE}: variables, verbose" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'variables'
}
35 changes: 35 additions & 0 deletions test/bats/github_17b.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bats

load ../test_helper

# Compare in the following tests is simply used to populate
# bash_getopts and getopts_long arrays with identical results.
export GETOPTS_TEST_BIN='getopts_long-without_extdebug'
export GETOPTS_LONG_TEST_BIN='getopts_long-without_extdebug'

@test "${FEATURE}: toggles, silent" {
compare 'toggles' 'toggles'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed"
}
@test "${FEATURE}: toggles, verbose" {
compare 'toggles' 'toggles'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed"
}

@test "${FEATURE}: options, silent" {
compare 'options' 'options'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed"
}
@test "${FEATURE}: options, verbose" {
compare 'options' 'options'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed"
}

@test "${FEATURE}: variables, silent" {
compare 'variables' 'variables'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-silent: line 8: getopts_long failed"
}
@test "${FEATURE}: variables, verbose" {
compare 'variables' 'variables'
expect "${getopts_long[1]}" =~ "getopts_long-without_extdebug-verbose: line 8: getopts_long failed"
}
31 changes: 31 additions & 0 deletions test/bats/github_17c.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load ../test_helper
export GETOPTS_LONG_TEST_BIN='getopts_long-explicit_args'

@test "${FEATURE}: toggles, silent" {
compare '-t -t -- user_arg' \
'toggles'
}
@test "${FEATURE}: toggles, verbose" {
compare '-t -t -- user_arg' \
'toggles'
}

@test "${FEATURE}: options, silent" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'options'
}
@test "${FEATURE}: options, verbose" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'options'
}

@test "${FEATURE}: variables, silent" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'variables'
}
@test "${FEATURE}: variables, verbose" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'variables'
}
80 changes: 80 additions & 0 deletions test/bin/getopts_long-explicit_args-silent
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

getopts_function() {
while getopts_long ':to:v: toggle option: variable:' OPTKEY "$@"; do
case ${OPTKEY} in
't'|'toggle')
printf 'toggle triggered'
;;
'o'|'option')
printf 'option supplied'
;;
'v'|'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION"
;;
':')
printf "MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*'
done

shift $(( OPTIND - 1 ))

echo "OPTERR: ${OPTERR}"
echo "OPTKEY: ${OPTKEY}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"

local function_args=("$@")
declare -p function_args \
| sed -e 's/declare -a function_args=/$@: /'
}

proxy() {
getopts_function "$@"
}

toggles() {
getopts_function -t --toggle -- user_arg
}

options() {
getopts_function -o user_val1 --option user_val2 -- user_arg
}

variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
shopt -u extdebug
fi

: "${1:?Missing required argument -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
80 changes: 80 additions & 0 deletions test/bin/getopts_long-explicit_args-verbose
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

getopts_function() {
while getopts_long 'to:v: toggle option: variable:' OPTKEY "$@"; do
case ${OPTKEY} in
't'|'toggle')
printf 'toggle triggered'
;;
'o'|'option')
printf 'option supplied'
;;
'v'|'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION"
;;
':')
printf "MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*'
done

shift $(( OPTIND - 1 ))

echo "OPTERR: ${OPTERR}"
echo "OPTKEY: ${OPTKEY}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"

local function_args=("$@")
declare -p function_args \
| sed -e 's/declare -a function_args=/$@: /'
}

proxy() {
getopts_function "$@"
}

toggles() {
getopts_function -t --toggle -- user_arg
}

options() {
getopts_function -o user_val1 --option user_val2 -- user_arg
}

variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
shopt -u extdebug
fi

: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
80 changes: 80 additions & 0 deletions test/bin/getopts_long-with_extdebug-silent
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

getopts_function() {
while getopts_long ':to:v: toggle option: variable:' OPTKEY; do
case ${OPTKEY} in
't'|'toggle')
printf 'toggle triggered'
;;
'o'|'option')
printf 'option supplied'
;;
'v'|'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION"
;;
':')
printf "MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*'
done

shift $(( OPTIND - 1 ))

echo "OPTERR: ${OPTERR}"
echo "OPTKEY: ${OPTKEY}"
echo "OPTARG: ${OPTARG}"
echo "OPTIND: ${OPTIND}"

local function_args=("$@")
declare -p function_args \
| sed -e 's/declare -a function_args=/$@: /'
}

proxy() {
getopts_function "$@"
}

toggles() {
getopts_function -t --toggle -- user_arg
}

options() {
getopts_function -o user_val1 --option user_val2 -- user_arg
}

variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

disable_extdebug='false'
if ! shopt -q extdebug; then
disable_extdebug='true'
shopt -s extdebug
fi

: "${1:?Missing required argument -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${disable_extdebug}; then
shopt -u extdebug
fi
Loading

0 comments on commit a892118

Please sign in to comment.