diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-05-25 22:10:09 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-25 22:10:09 +0100 |
| commit | 2a3fa5b8c749e631d2d7ed1bb01300f023c4b9ea (patch) | |
| tree | 46761b18297bc50e70fb61bef6031b33d5e31db5 /lib/assert.source.sh | |
| parent | b7489882bc82e4266e9cb9845604acdf20857c23 (diff) | |
rename packages to lib1.0.0
Diffstat (limited to 'lib/assert.source.sh')
| -rw-r--r-- | lib/assert.source.sh | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/assert.source.sh b/lib/assert.source.sh new file mode 100644 index 0000000..d53a728 --- /dev/null +++ b/lib/assert.source.sh @@ -0,0 +1,59 @@ +# Unit test for whether 2 given strings equal. +assert::equals () { + local -r result="$1"; shift + local -r expected="$1"; shift + local -r callee=${FUNCNAME[1]} + + if [[ "$result" != "$expected" ]]; then + cat <<ERROR | log::pipe ERROR +In $callee expected + '$expected' +But got + '$result' +ERROR + exit 2 + fi + + log VERBOSE "Result in $callee as expected: '$expected'" +} + +# Unit test for whether a given string is not empty. +assert::not_empty () { + local -r name="$1"; shift + local -r content="$1"; shift + local -r callee=${FUNCNAME[1]} + + if [ -z "$content" ]; then + log ERROR "In $callee expected '$name' not to be empty!" + exit 2 + fi + + log VERBOSE "Result in $callee as expected not empty" +} + +# Unit test for whether a given string matches a regex. +assert::matches () { + local -r name="$1"; shift + local -r content="$1"; shift + local -r regex="$1"; shift + local -r callee=${FUNCNAME[1]} + + if ! $GREP -q -E "$regex" <<< "$content"; then + log ERROR "In $callee expected '$name' to match '$regex'" + exit 2 + fi + + log VERBOSE "Matching in $callee as expected" +} + +# Checks if all the Bash scripts here are good. +assert::shellcheck () { + set -e + shellcheck \ + --norc \ + --external-sources \ + --check-sourced \ + --exclude=SC2155,SC2010,SC2154,SC1090,SC2012 \ + ./"$0" + set +e +} |
