Files
shell-scripting-templates/test/strings.bats
Nathaniel Landau 8bb89541e8 Squashed commit of the following:
commit 61bf734812cb62ba6e0ec224bc15f7928705a8a2
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Oct 21 15:44:21 2021 -0400

    Major overhaul continued

     - rename templates
     - add checks utilities
     - add new array utilities
     - rename files
     - add assorted utilities
     - improve documentation

commit 546178fff3b526f492eb0eeffc63f79537e75de3
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 16:31:14 2021 -0400

    Update conventions

commit f6d0642f85518efda9c5d8472b99d1c14163e381
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 09:47:09 2021 -0400

    minor formatting changes

commit 2217612b55e3f9faf803a2d0c937ea2261206505
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 17:59:09 2021 -0400

    add new functions

commit 347ba7aa738dcd6a5ad9d70886b38da3a17dc89e
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 12:06:44 2021 -0400

    major overhaul

    - Add standaloneTemplate.sh
    - Rework README
    - Refactor inline documentation
    - Enforce coding standards
    - Remove CSV utilities
    - Add new array utilities
    - add _useGNUutils_
    - more ...

commit cd8e0d49aef25eeaf6b3e71a3c9e1f29ab9b06f5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 17 09:56:08 2021 -0400

    Add debug functions

commit f7c5c0a3d19815dcc6ba80b5f5a2ebb77ef88b07
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sat Oct 16 21:10:01 2021 -0400

    add new array functions

    _joinArray_, _isEmptyArray_, _sortArray_, _reverseSortArray_, and _mergearrays_

commit d8bc3d8cabdbcee3c479f97b43a45bdfe3bdafe0
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 15 17:27:12 2021 -0400

    add _columnize_

commit 2fd2ae9435f476bc3968c3eb0d793db4bf1d9eaf
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Oct 11 22:17:45 2021 -0400

    _progressBar_: Fix unbound variable

commit e8933d15fc955a1acc665e9a081f131e681855d5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 10 11:50:42 2021 -0400

    _alert_: header now underlined

commit c9ce894361dec7d3513c038794a155519baf26bc
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:49:42 2021 -0400

    _alert_: line numbers to gray

commit 4aaddd336ce613f629a7e6a62ef3b27ffc24d22d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 8 15:05:20 2021 -0400

    _usage_ to stdout

commit e2372fc3122ec1f20acc27f04d29b3785f014e25
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:38:26 2021 -0400

    _setPATH_: remove unneeded logic

commit e60c75b6c954ac4bd146e2758252168027b9a43d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:25:38 2021 -0400

    _findSource_: bugfix

commit 0e84912e1ccd7203e5beff9f8737f8374f4aa5d8
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 16:29:25 2021 -0400

    add requirements to documentation

commit 2c24843e3ada591e1868a94416e40b5ac0aa4994
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 15:34:10 2021 -0400

    _uniqueFilename_: improve extension handling

commit 08bc2dfdcc8632efee9179e9c960a574fc17cf0c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 27 15:13:53 2021 -0400

    improve hooks script

commit 641918f1559d3b3aa38a9bbdf418938b2b81c176
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 24 08:16:52 2021 -0400

    _inArry_: case insensitivity

commit eae10f170680540fdb4a1222add7e54f8785ea63
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:31:44 2021 -0400

    clean up alerting

commit 700acd56f57fd57db84ef0e232ef41cdd7aee43c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:22:11 2021 -0400

    refactor _execute_

commit d893f86900a9fed9d91a0c9cc06c13b6b34d9926
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:19:18 2021 -0400

    'fatal' replaces 'die'

commit 3326857bf127bef36cd9982246aa5b826d796d0a
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 17 08:29:50 2021 -0400

    _execute_: ensure quiet and verbose work together
2021-10-21 16:03:27 -04:00

288 lines
6.6 KiB
Bash
Executable File

#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-assert/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/strings.bash"
BASEHELPERS="${ROOTDIR}/utilities/misc.bash"
ALERTS="${ROOTDIR}/utilities/alerts.bash"
if test -f "${SOURCEFILE}" >&2; then
source "${SOURCEFILE}"
else
echo "Sourcefile not found: ${SOURCEFILE}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
if test -f "${ALERTS}" >&2; then
source "${ALERTS}"
_setColors_ #Set color constants
else
echo "Sourcefile not found: ${ALERTS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
if test -f "${BASEHELPERS}" >&2; then
source "${BASEHELPERS}"
else
echo "Sourcefile not found: ${BASEHELPERS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() {
TESTDIR="$(temp_make)"
curPath="${PWD}"
BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}"
BATSLIB_FILE_PATH_ADD='<temp>'
pushd "${TESTDIR}" &>/dev/null
######## DEFAULT FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=OFF
VERBOSE=false
FORCE=false
DRYRUN=false
set -o errtrace
set -o nounset
set -o pipefail
}
teardown() {
set +o nounset
set +o errtrace
set +o pipefail
popd &>/dev/null
temp_del "${TESTDIR}"
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_splitString_" {
run _splitString_ "a,b,cd" ","
assert_success
assert_line --index 0 "a"
assert_line --index 1 "b"
assert_line --index 2 "cd"
}
@test "_stringContains_: success" {
run _stringContains_ "hello world!" "lo"
assert_success
}
@test "_stringContains_: failure" {
run _stringContains_ "hello world!" "zebra"
assert_failure
}
@test "_stringRegex_: success" {
run _stringRegex_ "hello world!" "[a-z].*!$"
assert_success
}
@test "_stringRegex_: failure" {
run _stringRegex_ "hello world!" "^.*[0-9]+"
assert_failure
}
_testCleanString_() {
@test "_cleanString_: fail" {
run _cleanString_
assert_failure
}
@test "_cleanString_: lowercase" {
run _cleanString_ -l "I AM IN CAPS"
assert_success
assert_output "i am in caps"
}
@test "_cleanString_: uppercase" {
run _cleanString_ -u "i am in caps"
assert_success
assert_output "I AM IN CAPS"
}
@test "_cleanString_: remove white space" {
run _cleanString_ -u " i am in caps "
assert_success
assert_output "I AM IN CAPS"
}
@test "_cleanString_: remove spaces before/after dashes" {
run _cleanString_ "word - another- word -another-word"
assert_success
assert_output "word-another-word-another-word"
}
@test "_cleanString_: remove spaces before/after underscores" {
run _cleanString_ "word _ another_ word _another_word"
assert_success
assert_output "word_another_word_another_word"
}
@test "_cleanString_: alnum" {
run _cleanString_ -a " !@#$%^%& i am in caps 12345 == "
assert_success
assert_output "i am in caps 12345"
}
@test "_cleanString_: alnum w/ spaces" {
run _cleanString_ -as "this(is)a[string]"
assert_success
assert_output "this is a string"
}
@test "_cleanString_: alnum w/ spaces and dashes" {
run _cleanString_ -as "this(is)a-string"
assert_success
assert_output "this is a-string"
}
@test "_cleanString_: alnum w/ spaces and dashes and regex replace" {
run _cleanString_ -asp "-|_|st, " "th_is(is)a-string"
assert_success
assert_output "th is is a ring"
}
@test "_cleanString_: user replacement" {
run _cleanString_ -p "e,g" "there should be a lot of e's in this sentence"
assert_success
assert_output "thgrg should bg a lot of g's in this sgntgncg"
}
@test "_cleanString_: remove specified characters" {
run _cleanString_ "there should be a lot of e's in this sentence" "e"
assert_success
assert_output "thr should b a lot of 's in this sntnc"
}
@test "_cleanString_: compound test 1" {
run _cleanString_ -p "2,4" -au " @#$%[]{} clean a compound command ==23---- " "e"
assert_success
assert_output "CLAN A COMPOUND COMMAND 43-"
}
}
_testCleanString_
_testStopWords_() {
@test "_stripStopwords_: success" {
run _stripStopwords_ "A string to be parsed"
assert_success
assert_output "string parsed"
}
@test "_stripStopwords_: success w/ user terms" {
run _stripStopwords_ "A string to be parsed to help pass this test being performed by bats" "bats,string"
assert_success
assert_output "parsed pass performed"
}
@test "_stripStopwords_: No changes" {
run _stripStopwords_ "string parsed pass performed"
assert_success
assert_output "string parsed pass performed"
}
@test "_stripStopwords_: fail" {
run _stripStopwords_
assert_failure
}
}
_testStopWords_
@test "_escapeString_" {
run _escapeString_ "Here is some / text to & be - escaped"
assert_success
assert_output "Here\ is\ some\ /\ text\ to\ &\ be\ -\ escaped"
}
@test "_encodeHTML_" {
run _encodeHTML_ "Here's some text& to > be h?t/M(l• en™code磧¶d"
assert_success
assert_output "Here's some text&amp; to &gt; be h?t/M(l&bull; en&trade;code&ccedil;&pound;&sect;&para;d"
}
@test "_decodeHTML_" {
run _decodeHTML_ "&clubs;Here's some text &amp; to &gt; be h?t/M(l&bull; en&trade;code&ccedil;&pound;&sect;&para;d"
assert_success
assert_output "♣Here's some text & to > be h?t/M(l• en™code磧¶d"
}
@test "_lower" {
local text="$(echo "MAKE THIS LOWERCASE" | _lower_)"
run echo "$text"
assert_output "make this lowercase"
}
@test "_ltrim_" {
local text=$(_ltrim_ <<<" some text")
run echo "$text"
assert_output "some text"
}
@test "_rtrim_" {
local text=$(_rtrim_ <<<"some text ")
run echo "$text"
assert_output "some text"
}
@test "_upper_" {
local text="$(echo "make this uppercase" | _upper_)"
run echo "$text"
assert_output "MAKE THIS UPPERCASE"
}
@test "_encodeURL_" {
run _encodeURL_ "Here's some.text%that&needs_to-be~encoded+a*few@more(characters)"
assert_success
assert_output "Here%27s%20some.text%25that%26needs_to-be~encoded%2Ba%2Afew%40more%28characters%29"
}
@test "_decodeURL_" {
run _decodeURL_ "Here%27s%20some.text%25that%26needs_to-be~encoded%2Ba%2Afew%40more%28characters%29"
assert_success
assert_output "Here's some.text%that&needs_to-be~encoded+a*few@more(characters)"
}
@test "_regexCapture_: success" {
run _regexCapture_ "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$' || echo "no match found"
assert_success
assert_output "#FFFFFF"
}
@test "_regexCapture_: failure" {
run _regexCapture_ "gggggg" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
assert_failure
}