Skip to content
Snippets Groups Projects
Commit 9fa2dceb authored by Andreas Unterkircher's avatar Andreas Unterkircher
Browse files

add tests/

parent 0ea40cad
Branches
Tags upstream/1.0
No related merge requests found
#!/bin/bash
. common.sh
assert_func in_array $TEST_FAIL $TEST_EMPTY
assert_func in_array $TEST_FAIL $TEST_EMPTY bla bla
assert_func in_array $TEST_FAIL $TEST_EMPTY ^bla$ ^bla$
declare -a -g TEST_ARY=()
assert_func in_array $TEST_FAIL $TEST_EMPTY TEST_ARY bla
assert_func in_array $TEST_FAIL $TEST_EMPTY TEST_ARY ^bla$
TEST_ARY+=( 'bla' )
assert_func in_array $TEST_OK $TEST_EMPTY TEST_ARY bla
assert_func in_array $TEST_OK $TEST_EMPTY TEST_ARY ^bla$
unset -v TEST_ARY
#!/bin/bash
. common.sh
assert_func is_array $TEST_FAIL $TEST_EMPTY
assert_func is_array $TEST_FAIL $TEST_EMPTY bla
declare -g FOO=bar
assert_func is_array $TEST_FAIL $TEST_EMPTY FOO
assert_func is_array $TEST_FAIL $TEST_EMPTY bar
unset -v FOO
declare -a -g FOO=()
assert_func is_array $TEST_FAIL $TEST_EMPTY BAR
assert_func is_array $TEST_OK $TEST_EMPTY FOO
FOO+=( 'bar' )
assert_func is_array $TEST_OK $TEST_EMPTY FOO
assert_func is_array $TEST_FAIL $TEST_EMPTY BAR
unset -v TEST_ARY
#!/bin/bash
. common.sh
assert_func is_empty $TEST_FAIL $TEST_EMPTY
assert_func is_empty $TEST_OK $TEST_EMPTY ''
assert_func is_empty $TEST_OK $TEST_EMPTY ""
assert_func is_empty $TEST_FAIL $TEST_EMPTY "foo"
#!/bin/bash
. common.sh
assert_func is_allowed_tag $TEST_FAIL $TEST_EMPTY
assert_func is_allowed_tag $TEST_FAIL $TEST_EMPTY foobar
assert_func is_allowed_tag $TEST_OK $TEST_EMPTY function
assert_func is_allowed_tag $TEST_FAIL $TEST_EMPTY param
assert_func is_allowed_tag $TEST_OK $TEST_EMPTY param1
assert_func is_allowed_tag $TEST_OK $TEST_EMPTY param123
#!/bin/bash
. common.sh
unset -v DEBUG
assert_func is_debug $TEST_FAIL $TEST_EMPTY
DEBUG=true
assert_func is_debug $TEST_OK $TEST_EMPTY
unset -v DEBUG
#!/bin/bash
. common.sh
unset -v DEBUG
assert_func debug $TEST_OK $TEST_EMPTY
assert_func debug $TEST_OK $TEST_EMPTY foo
DEBUG=true
assert_func debug $TEST_FAIL $TEST_EMPTY
assert_func debug $TEST_OK ^assert_func\\\(\\\[[[:digit:]]+\\\]\\\):[[:blank:]]+foo foo
unset -v DEBUG
#!/bin/bash
. common.sh
assert_func write $TEST_FAIL $TEST_EMPTY
assert_func write $TEST_OK $TEST_EMPTY foo
assert_func write $TEST_FAIL $TEST_EMPTY foo bar
#!/bin/bash
. common.sh
assert_func read_file $TEST_FAIL $TEST_EMPTY
assert_func read_file $TEST_FAIL $TEST_EMPTY bla
assert_func read_file $TEST_OK $TEST_EMPTY testfile_valid.sh
assert_func read_file $TEST_FAIL $TEST_EMPTY testfile_valid.sh bla
#!/bin/bash
. common.sh
assert_func parse_input $TEST_FAIL 'No input data found'
read_file testfile.sh
assert_func parse_input $TEST_FAIL 'No input data found'
read_file testfile_valid.sh
[[ ! -v "DEBUG" ]] || { \
ORIG_DEBUG="${DEBUG}"; \
unset -v DEBUG; \
}
assert_func parse_input $TEST_OK '^Parsing[[:blank:]]tags[[:print:]]+...'
read_file testfile_invalid.sh
assert_func parse_input $TEST_FAIL 'Please check if you have defined the same function tags twice'
[[ ! -v "ORIG_DEBUG" ]] || { \
DEBUG="${ORIG_DEBUG}"; \
unset -v ORIG_DEBUG; \
}
#!/bin/bash
. common.sh
assert_func formated_output $TEST_FAIL $TEST_EMPTY
assert_func formated_output $TEST_FAIL $TEST_EMPTY foo
declare -A FOOBAR=()
assert_func formated_output $TEST_FAIL $TEST_EMPTY FOOBAR
FOOBAR+=( ['function']="bla" )
assert_func formated_output $TEST_OK $TEST_EMPTY FOOBAR
# Makefile for shell-docs automated testing.
###############################################################################
# This file is part of shell-docs.
#
# shell-docs, a documentation generator for shell sciprts.
#
# # Copyright (C) 2017, Andreas Unterkircher <unki@netshadow.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
###############################################################################
script_args := -a-n -atestfile_valid.sh -atestout.md
rparts_args := --report
ifdef v
rparts_args += --verbose
endif
ifdef d
script_args := -a--debug $(script_args)
rparts_args += --verbose
endif
.PHONY: all
all:
@rm -f testout.md
@run-parts $(rparts_args) --exit-on-error --regex '^1[[:digit:]][[:digit:]]_' $(script_args) 1xx
@rm -f testout.md
# Automated Testing Suite for shell-docs-gen.
## Test-Structure
* 1xx ... function tests
# Execution
## Simple Run
make
## Verbose Run
make v=1
## Debug Run
make d=1
#!/bin/bash
#
# include shell-docs-gen.sh
#
[ -e ../shell-docs-gen.sh ] || \
{ echo "Unable to load ../shell-docs-gen.sh! Exiting..."; exit 1; }
source ../shell-docs-gen.sh $@
[ "x${?}" == "x0" ] || \
{ echo "Failed to load ../shell-docs-gen.sh! Exiting..."; exit 1; }
#
# restore some settings that have been made by function.sh
#
set +e +o pipefail
#
# a counter for numbering each test that gets executed.
#
declare -p "TEST_NUM" &>/dev/null || declare -g TEST_NUM=0
export TEST_NUM
#readonly CWD=$(dirname $(realpath $0))
# we use a place holder to signalize no output is desired.
readonly TEST_EMPTY="xxxxxxLANGWEILIGxxxxxx"
# boolean exit states
readonly TEST_TRUE=0
readonly TEST_FALSE=1
# regular exit states
readonly TEST_OK=0
readonly TEST_FAIL=1
readonly TEST_WARNING=1
readonly TEST_CRITICAL=2
readonly TEST_UNKNOWN=3
declare TEST_DEBUG=false
if [ $# -gt 0 ] && [ "x${1}" == "x--debug" ]; then
TEST_DEBUG=true
fi
do_debug ()
{
${TEST_DEBUG} || return 0
echo "$@"
}
assert_equals ()
{
local PARAM1="${1}"
local PARAM2="${2}"
local EXPECT="0"
if [ $# -eq 3 ]; then
[ ! -z "${3}" ] && EXPECT=${3}
fi
TEST_NUM=$((TEST_NUM + 1))
do_debug -n "Test ${TEST_NUM}: "
[[ ${PARAM1} =~ ${PARAM2} ]]
RETVAL=$?
if [[ ${RETVAL} =~ ${EXPECT} ]]; then
do_debug "PASS"
return 0
fi
echo "NUM: ${TEST_NUM} (${FUNCNAME[0]}: ${@})"
echo "FAIL: expected ${EXPECT} but got ${RETVAL}!"
echo
exit 1
}
assert_limits ()
{
local RETVAL= RESULT= EXPECT_TEXT=
local EXPECT_CODE="${1}"; shift
case "${EXPECT_CODE}" in
0) EXPECT_TEXT="OK" ;;
1) EXPECT_TEXT="WARNING" ;;
2) EXPECT_TEXT="CRITICAL" ;;
*) EXPECT_TEXT="UNKNOWN" ;;
esac
assert_func eval_limits $EXPECT_CODE $EXPECT_TEXT "$@"
return $?
}
assert_func ()
{
local RETVAL= RESULT=
local FUNC="${1}"; shift
local EXPECT_CODE="${1}"; shift
local EXPECT_TEXT="${1}"; shift
local FAIL_TEXT= FAIL_ON_EXIT= FAIL_ON_TEXT=
TEST_NUM=$((TEST_NUM + 1))
do_debug -n "Test ${TEST_NUM}: "
RESULT=$($FUNC "${@}" 2>&1)
RETVAL=$?
if [[ ${RETVAL} =~ ${EXPECT_CODE} ]] && \
( ( [ "${EXPECT_TEXT}" == "${TEST_EMPTY}" ] && [ -z "${RESULT}" ] ) || \
[[ "${RESULT}" =~ ${EXPECT_TEXT} ]] ); then
do_debug "PASS"
return 0
fi
if ! [[ ${RETVAL} =~ ${EXPECT_CODE} ]]; then
FAIL_TEXT+="Expected exit-code '${EXPECT_CODE}', but got '${RETVAL}'!\n\t"
fi
if [ "${EXPECT_TEXT}" != "${TEST_EMPTY}" ] && ! [[ "${RESULT}" =~ ${EXPECT_TEXT} ]]; then
FAIL_TEXT+="Expected output '${EXPECT_TEXT}', but got '${RESULT//[^a-zA-Z0-9_[:blank:][:punct:]]/}'!\n\t"
fi
if [ "${EXPECT_TEXT}" == "${TEST_EMPTY}" ] && [ ! -z "${RESULT}" ]; then
FAIL_TEXT+="Expected no output, but got '${RESULT//[^a-zA-Z0-9_[:blank:][:punct:]]/}'!\n\t"
fi
echo "FAILED"; echo
echo -e "TEST:\tNo.${TEST_NUM} (${FUNCNAME[0]}:${BASH_LINENO[0]})"
echo -e "PARAM:\t${@}"
[ ! -z "${FAIL_TEXT}" ] && echo -e "ERR:\t${FAIL_TEXT:0:-4}"
echo
exit 1
}
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment