mob: Add rcfile support + add `default_si`
[sunny256-utils.git] / tests / datefmt.t
blobdc2982e64a1df117fd2d612a4e8859f9ac9f442e
1 #!/usr/bin/env perl
3 #==============================================================================
4 # datefmt.t
5 # File ID: cbc683c8-a45e-11ea-8d29-4f45262dc9b5
7 # Test suite for datefmt(1).
9 # Character set: UTF-8
10 # ©opyleft 2020– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
18 BEGIN {
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 local $| = 1;
27 our $CMDB = "datefmt";
28 our $CMD = "../$CMDB";
30 our %Opt = (
32 'all' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'todo' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt::Long::Configure('bundling');
48 GetOptions(
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'}) {
62 print_version();
63 exit(0);
66 exit(main());
68 sub main {
69 my $Retval = 0;
71 diag(sprintf('========== Executing %s v%s ==========',
72 $progname, $VERSION));
74 if ($Opt{'todo'} && !$Opt{'all'}) {
75 goto todo_section;
78 test_standard_options();
79 test_executable();
81 todo_section:
84 if ($Opt{'all'} || $Opt{'todo'}) {
85 diag('Running TODO tests...');
86 TODO: {
87 local $TODO = '';
88 # Insert TODO tests here.
92 diag('Testing finished.');
94 return $Retval;
97 sub test_executable {
98 my %s = (
100 0 => '0s',
101 1 => '1s',
102 5 => '5s',
103 10 => '10s',
104 59 => '59s',
105 60 => '1m:00s',
106 61 => '1m:01s',
107 119 => '1m:59s',
108 120 => '2m:00s',
109 121 => '2m:01s',
110 3599 => '59m:59s',
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});
141 checkval("''", "");
142 checkval("nonumber", "nonumber");
143 testcmd("echo nonumber | $CMD",
144 "nonumber\n", "", 0,
145 "\"nonumber\"");
146 testcmd("echo 9999 with text | $CMD",
147 "2h:46m:39s with text\n", "", 0,
148 "\"9999 with text\"");
151 sub checkval {
152 my ($in, $out) = @_;
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...');
162 likecmd("$CMD -h",
163 '/ Show this help/i',
164 '/^$/',
166 'Option -h prints help screen');
168 diag('Testing -v (--verbose) option...');
169 likecmd("$CMD -hv",
170 '/^\n\S+ \d+\.\d+\.\d+/s',
171 '/^$/',
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+/',
178 '/^$/',
180 'Option --version returns version number');
181 return;
184 sub testcmd {
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;
189 my $stderr_cmd = '';
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";
193 my $retval = 1;
195 if (defined($Exp_stderr)) {
196 $stderr_cmd = " 2>$TMP_STDERR";
198 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
199 my $ret_val = $?;
200 if (defined($Exp_stderr)) {
201 $retval &= is(file_data($TMP_STDERR),
202 $Exp_stderr, "$Txt (stderr)");
203 unlink($TMP_STDERR);
204 } else {
205 diag("Warning: stderr not defined for '$Txt'");
207 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
209 return $retval;
212 sub likecmd {
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;
217 my $stderr_cmd = '';
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";
221 my $retval = 1;
223 if (defined($Exp_stderr)) {
224 $stderr_cmd = " 2>$TMP_STDERR";
226 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
227 my $ret_val = $?;
228 if (defined($Exp_stderr)) {
229 $retval &= like(file_data($TMP_STDERR),
230 $Exp_stderr, "$Txt (stderr)");
231 unlink($TMP_STDERR);
232 } else {
233 diag("Warning: stderr not defined for '$Txt'");
235 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
237 return $retval;
240 sub file_data {
241 # Return file content as a string
242 my $File = shift;
243 my $Txt;
245 open(my $fp, '<', $File) or return undef;
246 local $/ = undef;
247 $Txt = <$fp>;
248 close($fp);
249 return $Txt;
252 sub create_file {
253 # Create new file and fill it with data
254 my ($file, $text) = @_;
255 my $retval = 0;
257 open(my $fp, ">$file") or return 0;
258 print($fp $text);
259 close($fp);
260 $retval = is(file_data($file), $text,
261 "$file was successfully created");
263 return $retval; # 0 if error, 1 if ok
266 sub print_version {
267 # Print program version
268 print("$progname $VERSION\n");
269 return;
272 sub usage {
273 # Send the help message to stdout
274 my $Retval = shift;
276 if ($Opt{'verbose'}) {
277 print("\n");
278 print_version();
280 print(<<"END");
282 Usage: $progname [options]
284 Contains tests for the $CMDB(1) program.
286 Options:
288 -a, --all
289 Run all tests, also TODOs.
290 -h, --help
291 Show this help.
292 -q, --quiet
293 Be more quiet. Can be repeated to increase silence.
294 -t, --todo
295 Run only the TODO tests.
296 -v, --verbose
297 Increase level of verbosity. Can be repeated.
298 --version
299 Print version information.
302 exit($Retval);
305 sub msg {
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");
311 return;
314 __END__
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
319 # version.
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
327 # this program.
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 :