3 #==============================================================================
5 # File ID: cbc683c8-a45e-11ea-8d29-4f45262dc9b5
7 # Test suite for datefmt(1).
10 # ©opyleft 2020– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
13 #==============================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMDB = "datefmt";
28 our $CMD = "../$CMDB";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
50 'all|a' => \
$Opt{'all'},
51 'help|h' => \
$Opt{'help'},
52 'quiet|q+' => \
$Opt{'quiet'},
53 'todo|t' => \
$Opt{'todo'},
54 'verbose|v+' => \
$Opt{'verbose'},
55 'version' => \
$Opt{'version'},
57 ) || die("$progname: Option error. Use -h for help.\n");
59 $Opt{'verbose'} -= $Opt{'quiet'};
60 $Opt{'help'} && usage
(0);
61 if ($Opt{'version'}) {
71 diag
(sprintf('========== Executing %s v%s ==========',
72 $progname, $VERSION));
74 if ($Opt{'todo'} && !$Opt{'all'}) {
78 test_standard_options
();
84 if ($Opt{'all'} || $Opt{'todo'}) {
85 diag
('Running TODO tests...');
88 # Insert TODO tests here.
92 diag
('Testing finished.');
111 3600 => '1h:00m:00s',
112 3601 => '1h:00m:01s',
113 65536 => '18h:12m:16s',
114 86399 => '23h:59m:59s',
115 86400 => '1d:00h:00m:00s',
116 86401 => '1d:00h:00m:01s',
117 86460 => '1d:00h:01m:00s',
118 864000 => '10d:00h:00m:00s',
119 8640000 => '100d:00h:00m:00s',
120 31556951 => '365d:05h:49m:11s', # 86400 * 365.2425 - 1
121 31556952 => '1y:0d:00h:00m:00s', # 86400 * 365.2425
122 31643352 => '1y:1d:00h:00m:00s', # 86400 * 366.2425
123 31740797 => '1y:2d:03h:04m:05s', # 86400 * (365.2425 + 2 + 3/24 + 4/1440 + 5/86400)
124 315569519 => '9y:365d:05h:49m:11s', # 86400 * 10 * 365.2425 - 1
125 315569520 => '10y:0d:00h:00m:00s', # 86400 * 10 * 365.2425
126 3155695200 => '100y:0d:00h:00m:00s', # 86400 * 100 * 365.2425
127 31556952000 => '1000y:0d:00h:00m:00s', # 86400 * 1000 * 365.2425
128 38946142857 => '1234y:56d:07h:08m:09s', # 86400 * (1234 * 365.2425 + 56 + 7/24 + 8/1440 + 9/86400)
129 31556951999999 => '999999y:365d:05h:49m:11s', # 86400 * 1000000 * 365.2425 - 1
130 31556952000000 => '1000000y:0d:00h:00m:00s', # 86400 * 1000000 * 365.2425
131 31556952000001 => '1000000y:0d:00h:00m:01s', # 86400 * 1000000 * 365.2425 + 1
132 31556952065536 => '1000000y:0d:18h:12m:16s', # 86400 * 1000000 * 365.2425 + 65536
133 31556952086400 => '1000000y:1d:00h:00m:00s', # 86400 * (1000000 * 365.2425 + 1)
134 31556960596801 => '1000000y:99d:12h:00m:01s', # 86400 * (1000000 * 365.2425 + 99 + 12/24 + 1/86400)
135 315569520000000 => '10000000y:0d:00h:00m:00s', # 86400 * 10000000 * 365.2425
138 for my $f (sort { $a <=> $b } keys %s) {
139 checkval
($f, $s{$f});
142 checkval
("nonumber", "nonumber");
143 testcmd
("echo nonumber | $CMD",
146 testcmd
("echo 9999 with text | $CMD",
147 "2h:46m:39s with text\n", "", 0,
148 "\"9999 with text\"");
153 diag
("$in => \"$out\"");
154 testcmd
("$CMD $in", "$out\n", "", 0, "arg $in = \"$out\"");
155 testcmd
("echo $in | $CMD", "$out\n", "", 0, "stdin $in = \"$out\"");
156 testcmd
("$CMD -- -$in", "-$out\n", "", 0, "arg -$in = \"-$out\"");
157 testcmd
("echo -$in | $CMD", "-$out\n", "", 0, "stdin -$in = \"-$out\"");
160 sub test_standard_options
{
161 diag
('Testing -h (--help) option...');
163 '/ Show this help/i',
166 'Option -h prints help screen');
168 diag
('Testing -v (--verbose) option...');
170 '/^\n\S+ \d+\.\d+\.\d+/s',
173 'Option -v with -h returns version number and help screen');
175 diag
('Testing --version option...');
176 likecmd
("$CMD --version",
177 '/^\S+ \d+\.\d+\.\d+/',
180 'Option --version returns version number');
185 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
186 defined($descriptions{$Desc}) &&
187 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
188 $descriptions{$Desc} = 1;
190 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
191 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
192 my $TMP_STDERR = "$CMDB-stderr.tmp";
195 if (defined($Exp_stderr)) {
196 $stderr_cmd = " 2>$TMP_STDERR";
198 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
200 if (defined($Exp_stderr)) {
201 $retval &= is
(file_data
($TMP_STDERR),
202 $Exp_stderr, "$Txt (stderr)");
205 diag
("Warning: stderr not defined for '$Txt'");
207 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
213 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
214 defined($descriptions{$Desc}) &&
215 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
216 $descriptions{$Desc} = 1;
218 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
219 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
220 my $TMP_STDERR = "$CMDB-stderr.tmp";
223 if (defined($Exp_stderr)) {
224 $stderr_cmd = " 2>$TMP_STDERR";
226 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
228 if (defined($Exp_stderr)) {
229 $retval &= like
(file_data
($TMP_STDERR),
230 $Exp_stderr, "$Txt (stderr)");
233 diag
("Warning: stderr not defined for '$Txt'");
235 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
241 # Return file content as a string
245 open(my $fp, '<', $File) or return undef;
253 # Create new file and fill it with data
254 my ($file, $text) = @_;
257 open(my $fp, ">$file") or return 0;
260 $retval = is
(file_data
($file), $text,
261 "$file was successfully created");
263 return $retval; # 0 if error, 1 if ok
267 # Print program version
268 print("$progname $VERSION\n");
273 # Send the help message to stdout
276 if ($Opt{'verbose'}) {
282 Usage: $progname [options]
284 Contains tests for the $CMDB(1) program.
289 Run all tests, also TODOs.
293 Be more quiet. Can be repeated to increase silence.
295 Run only the TODO tests.
297 Increase level of verbosity. Can be repeated.
299 Print version information.
306 # Print a status message to stderr based on verbosity level
307 my ($verbose_level, $Txt) = @_;
309 $verbose_level > $Opt{'verbose'} && return;
310 print(STDERR
"$progname: $Txt\n");
316 # This program is free software; you can redistribute it and/or modify it under
317 # the terms of the GNU General Public License as published by the Free Software
318 # Foundation; either version 2 of the License, or (at your option) any later
321 # This program is distributed in the hope that it will be useful, but WITHOUT
322 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
323 # FOR A PARTICULAR PURPOSE.
324 # See the GNU General Public License for more details.
326 # You should have received a copy of the GNU General Public License along with
328 # If not, see L<http://www.gnu.org/licenses/>.
330 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :