Files
shell-scripting-templates/test/dates.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

492 lines
14 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/dates.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)"
######## DEFAULT FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=OFF
VERBOSE=true
FORCE=false
DRYRUN=false
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_dateUnixTimestamp_" {
run _dateUnixTimestamp_
assert_success
assert_output --regexp "^[0-9]+$"
}
@test "_convertToUnixTimestamp_" {
run _convertToUnixTimestamp_ "2020-07-07 18:38"
assert_success
assert_output "1594161480"
}
@test "_readableUnixTimestamp_: Default Format" {
run _readableUnixTimestamp_ "1591554426"
assert_success
assert_output "2020-06-07 14:27:06"
}
@test "_readableUnixTimestamp_: Custom format" {
run _readableUnixTimestamp_ "1591554426" "%Y-%m-%d"
assert_success
assert_output "2020-06-07"
}
@test "_monthToNumber_: 1" {
run _monthToNumber_ "dec"
assert_success
assert_output "12"
}
@test "_monthToNumber_: 2" {
run _monthToNumber_ "MARCH"
assert_success
assert_output "3"
}
@test "_monthToNumber_: Fail" {
run _monthToNumber_ "somethingthatbreaks"
assert_failure
}
@test "_numberToMonth_: 1" {
run _numberToMonth_ "1"
assert_success
assert_output "January"
}
@test "_numberToMonth_: 2" {
run _numberToMonth_ "02"
assert_success
assert_output "February"
}
@test "_numberToMonth_: Fail" {
run _numberToMonth_ "13"
assert_failure
}
@test "_parseDate_: YYYY MM DD 1" {
run _parseDate_ "2019 06 01"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +2019 06 01"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +June"
assert_output --regexp "PARSE_DATE_MONTH: +6"
}
@test "_parseDate_: YYYY MM DD 2" {
run _parseDate_ "this is text 2019-06-01 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +2019-06-01"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +June"
assert_output --regexp "PARSE_DATE_MONTH: +6"
assert_output --regexp "PARSE_DATE_DAY: +1"
}
@test "_parseDate_: YYYY MM DD fail 1" {
run _parseDate_ "this is text 2019-99-01 and more text"
assert_failure
}
@test "_parseDate_: YYYY MM DD fail 2" {
run _parseDate_ "this is text 2019-06-99 and more text"
assert_failure
}
@test "_parseDate_: Month DD, YYYY" {
run _parseDate_ "this is text Oct 22, 2019 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +Oct 22, +2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: Month DD YYYY" {
run _parseDate_ "Oct 22 2019"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +Oct 22 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: Month DD, YY" {
run _parseDate_ "this is text Oct 22, 19 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +Oct 22, 19"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: Month DD YY" {
run _parseDate_ "Oct 22 19"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +Oct 22 19"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: DD Month, YYYY" {
run _parseDate_ "22 June, 2019 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +22 June, 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +June"
assert_output --regexp "PARSE_DATE_MONTH: +6"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: DD Month YYYY" {
run _parseDate_ "some text66-here-22 June 2019 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +22 June 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +June"
assert_output --regexp "PARSE_DATE_MONTH: +6"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: MM DD YYYY 1" {
run _parseDate_ "this is text 12 22 2019 and more text"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +12 22 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +December"
assert_output --regexp "PARSE_DATE_MONTH: +12"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: MM DD YYYY 2" {
run _parseDate_ "12 01 2019"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +12 01 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +December"
assert_output --regexp "PARSE_DATE_MONTH: +12"
assert_output --regexp "PARSE_DATE_DAY: +1"
}
@test "_parseDate_: MM DD YYYY 3" {
run _parseDate_ "a-test-01-12-2019-is here"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +01-12-2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +12"
}
@test "_parseDate_: DD MM YYYY 1 " {
run _parseDate_ "a-test-22/12/2019-is here"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +22/12/2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +December"
assert_output --regexp "PARSE_DATE_MONTH: +12"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: DD MM YYYY 2 " {
run _parseDate_ "a-test-32/12/2019-is here"
assert_failure
}
@test "_parseDate_: DD MM YY" {
run _parseDate_ "a-test-22-12-19-is here"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +22-12-19"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +December"
assert_output --regexp "PARSE_DATE_MONTH: +12"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: MM DD YY 1 " {
run _parseDate_ "a-test-12/22/19-is here"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +12/22/19"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +December"
assert_output --regexp "PARSE_DATE_MONTH: +12"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: MM DD YY 2 " {
run _parseDate_ "6 8 19"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +6 8 19"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +June"
assert_output --regexp "PARSE_DATE_MONTH: +6"
assert_output --regexp "PARSE_DATE_DAY: +8"
}
@test "_parseDate_: MM DD YY 3 " {
run _parseDate_ "6 8 191"
assert_failure
}
@test "_parseDate_: MM DD YY 4 " {
run _parseDate_ "6 34 19"
assert_failure
}
@test "_parseDate_: MM DD YY 5 " {
run _parseDate_ "34 12 19"
assert_failure
}
@test "_parseDate_: Month, YYYY 1 " {
run _parseDate_ "a-test-January, 2019-is here"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +January, 2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +1"
}
@test "_parseDate_: Month, YYYY 2 " {
run _parseDate_ "mar-2019"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +mar-2019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +March"
assert_output --regexp "PARSE_DATE_MONTH: +3"
assert_output --regexp "PARSE_DATE_DAY: +1"
}
@test "_parseDate_: YYYYMMDDHHMM 1" {
run _parseDate_ "201901220228"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +201901220228"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +22"
assert_output --regexp "PARSE_DATE_HOUR: +2"
assert_output --regexp "PARSE_DATE_MINUTE: +28"
}
@test "_parseDate_: YYYYMMDDHHMM 2" {
run _parseDate_ "asdf 201901220228asdf "
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +201901220228"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +22"
assert_output --regexp "PARSE_DATE_HOUR: +2"
assert_output --regexp "PARSE_DATE_MINUTE: +28"
}
@test "_parseDate_: YYYYMMDDHH 1" {
run _parseDate_ "asdf 2019012212asdf "
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +2019012212"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +22"
assert_output --regexp "PARSE_DATE_HOUR: +12"
assert_output --regexp "PARSE_DATE_MINUTE: +00"
}
@test "_parseDate_: YYYYMMDDHH 2" {
run _parseDate_ "2019012212"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +2019012212"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +22"
assert_output --regexp "PARSE_DATE_HOUR: +12"
assert_output --regexp "PARSE_DATE_MINUTE: +00"
}
@test "_parseDate_: MMDDYYYY 1" {
run _parseDate_ "01222019"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +01222019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: MMDDYYYY 2" {
run _parseDate_ "asdf 11222019 asdf"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +11222019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +November"
assert_output --regexp "PARSE_DATE_MONTH: +11"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: DDMMYYYY 1" {
run _parseDate_ "16012019"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +16012019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +January"
assert_output --regexp "PARSE_DATE_MONTH: +1"
assert_output --regexp "PARSE_DATE_DAY: +16"
}
@test "_parseDate_: DDMMYYYY 2" {
run _parseDate_ "asdf 16112019 asdf"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +16112019"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +November"
assert_output --regexp "PARSE_DATE_MONTH: +11"
assert_output --regexp "PARSE_DATE_DAY: +16"
}
@test "_parseDate_: YYYYDDMM " {
run _parseDate_ "20192210"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +20192210"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: YYYYMMDD 1" {
run _parseDate_ "20191022"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +20191022"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +22"
}
@test "_parseDate_: YYYYMMDD 2" {
run _parseDate_ "20191010"
assert_success
assert_output --regexp "PARSE_DATE_FOUND: +20191010"
assert_output --regexp "PARSE_DATE_YEAR: +2019"
assert_output --regexp "PARSE_DATE_MONTH_NAME: +October"
assert_output --regexp "PARSE_DATE_MONTH: +10"
assert_output --regexp "PARSE_DATE_DAY: +10"
}
@test "_parseDate_: YYYYMMDD fail" {
run _parseDate_ "20199910"
assert_failure
}
@test "_parseDate_: fail - no input" {
run _parseDate_
assert_failure
}
@test "_parseDate_: fail - no date" {
run _parseDate_ "a string with some numbers 1234567"
assert_failure
}
@test "_formatDate_: default" {
run _formatDate_ "jan 21, 2019"
assert_success
assert_output "2019-01-21"
}
@test "_formatDate_: custom format " {
run _formatDate_ "2019-12-27" "+%m %d, %Y"
assert_success
assert_output "12 27, 2019"
}
@test "_convertSecs_: Seconds to human readable" {
run _fromSeconds_ "9255"
assert_success
assert_output "02:34:15"
}
@test "_toSeconds_: HH MM SS to Seconds" {
run _toSeconds_ 12 3 33
assert_success
assert_output "43413"
}
@test "_countdown_: custom message, default wait" {
run _countdown_ 10 0 "something"
assert_line --index 0 --partial "something 10"
assert_line --index 9 --partial "something 1"
}
@test "_countdown_: default message, custom wait" {
run _countdown_ 5 0
assert_line --index 0 --partial "... 5"
assert_line --index 4 --partial "... 1"
}
@test "_countdown_: all defaults" {
run _countdown_
assert_line --index 0 --partial "... 10"
assert_line --index 9 --partial "... 1"
}