summaryrefslogtreecommitdiffstats
path: root/tests/parse.sh
blob: eadd2cf6aa749e8647a12a5671e533a19df30a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Tests @'s date/time parsing
#
# Copyright (C) 2022  P. J. McDermott
#
# This file is part of @
#
# @ is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# @ 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with @.  If not, see <http://www.gnu.org/licenses/>.

set -eu

. "${TOP_SRCDIR}/tests/aux/tap-functions.sh"

now='1969-12-31 23:59:30'
tgt='1970-01-01 00:00:00'

fmts="$(TZ=UTC0 "${TOP_BUILDDIR}/atsign" -d "${tgt}" -F)"
time_fmts="$(printf '%s\n' "${fmts}" | \
	sed -n '/^Time formats:$/,/^Date formats:$/s/^  \* //p')"
date_fmts="$(printf '%s\n' "${fmts}" | \
	sed -n '/^Date formats:$/,/^Additional formats:$/s/^  \* //p')"
misc_fmts="$(printf '%s\n' "${fmts}" | \
	sed -n '/^Additional formats:$/,$s/^  \* //p')"

plan_ $((\
	$(printf '%s\n' "${time_fmts}" | wc -l) + \
	$(printf '%s\n' "${time_fmts}" | wc -l) * \
	$(printf '%s\n' "${date_fmts}" | wc -l) * \
	2 + \
	$(printf '%s\n' "${misc_fmts}" | wc -l) \
	))

IFS='
'

for fmt in $(printf '%s\n' "${fmts}"); do
	diag_ "${fmt}"
done

test_fmt()
{
	now="${1}"
	tgt="${2}"
	fmt="${3}"
	shift 3

	got="$(TZ=UTC0 "${TOP_BUILDDIR}/atsign" -d "${now}" "${fmt}" 2>&1)" || :
	if [ x"${got}" = x"${tgt}" ]; then
		ok_ -- "${fmt}"
	else
		not_ok_ -- "${fmt}"
		diag_ " Failed test '${fmt}'"
		diag_ "         got: '${got}'"
		diag_ "    expected: '${tgt}'"
	fi
}

for time_fmt in ${time_fmts}; do
	test_fmt "${now}" "${tgt}" "${time_fmt}"
	for date_fmt in ${date_fmts}; do
		test_fmt "${now}" "${tgt}" "${date_fmt} ${time_fmt}"
		test_fmt "${now}" "${tgt}" "${time_fmt} ${date_fmt}"
	done
done
for misc_fmt in ${misc_fmts}; do
	test_fmt "${now}" "${tgt}" "${misc_fmt}"
done