datefmt: Define 1 year as 365.2425 days instead of 365.25
[sunny256-utils.git] / tests / jday.t
blobfedc52b10b3501e0a7421a4e42d3d5ef6e2acff7
1 #!/usr/bin/env perl
3 #=======================================================================
4 # jday.t
5 # File ID: 311590da-a9db-11e5-8a79-fefdb24f8e10
7 # Test suite for jday(1).
9 # Character set: UTF-8
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for 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 = "jday";
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 # {{{
70 my $Retval = 0;
72 diag(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
76 goto todo_section;
79 =pod
81 testcmd("$CMD command", # {{{
82 <<'END',
83 [expected stdout]
84 END
85 '',
87 'description',
90 # }}}
92 =cut
94 diag('Testing -h (--help) option...');
95 likecmd("$CMD -h", # {{{
96 '/ Show this help/i',
97 '/^$/',
99 'Option -h prints help screen',
102 # }}}
103 diag('Testing -v (--verbose) option...');
104 likecmd("$CMD -h -v", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
106 '/^$/',
108 'Option -v with -h returns version number and help screen',
111 # }}}
112 diag('Testing --version option...');
113 likecmd("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
115 '/^$/',
117 'Option --version returns version number',
120 # }}}
121 likecmd($CMD, '/24[5-9]\d{4}\.\d+/', '/^$/', 0,
122 "Display current Julian Day");
123 testcmd("$CMD 2003-01-32 12:00:00", "\n", "", 0,
124 "Receive invalid date");
125 testcmd("$CMD 2014-05-11 12:18:00", "2456789.0125\n", "", 0,
126 "2014-05-11 12:18:00 returns 2456789.0125");
127 testcmd("$CMD 0763-09-18 12:00", "2000000.0\n", "", 0,
128 "Parse 0763-09-18 12:00 (JD 2000000.0)");
129 testcmd("$CMD -- -1975-10-21 12:00", "1000000.0\n", "", 0,
130 "Parse -1975-10-21 12:00 (JD 1000000.0)");
131 testcmd("$CMD -- -4713-11-24 12:00:00", "0.0\n", "", 0,
132 "Parse -4713-11-24 12:00:00 (JD 0)");
133 testcmd("$CMD 2015-12-24 01:32:34", "2457380.56428241\n", "", 0,
134 "Parse 2015-12-24 01:32:34");
135 testcmd("$CMD 1980-12-09 03:50", "2444582.65972222\n", "", 0,
136 "Parse 1980-12-09 03:50");
137 testcmd("$CMD 1600-03-18 12:34:56", "2305525.02425926\n", "", 0,
138 "Parse 1600-03-18 12:34:56");
139 testcmd("$CMD 3768-01-11 22:13:19", "3097304.42591435\n", "", 0,
140 "Parse 3768-01-11 22:13:19");
141 testcmd("$CMD -- -1600-03-18 12:34:56", "1136749.02425926\n", "", 0,
142 "Parse -1600-03-18 12:34:56 (negative year)");
144 todo_section:
147 if ($Opt{'all'} || $Opt{'todo'}) {
148 diag('Running TODO tests...'); # {{{
150 TODO: {
152 local $TODO = '';
153 # Insert TODO tests here.
156 # TODO tests }}}
159 diag('Testing finished.');
160 return $Retval;
161 # }}}
162 } # main()
164 sub testcmd {
165 # {{{
166 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
167 defined($descriptions{$Desc}) &&
168 BAIL_OUT("testcmd(): '$Desc' description is used twice");
169 $descriptions{$Desc} = 1;
170 my $stderr_cmd = '';
171 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
172 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
173 my $TMP_STDERR = "$CMDB-stderr.tmp";
174 my $retval = 1;
176 if (defined($Exp_stderr)) {
177 $stderr_cmd = " 2>$TMP_STDERR";
179 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
180 my $ret_val = $?;
181 if (defined($Exp_stderr)) {
182 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
183 unlink($TMP_STDERR);
184 } else {
185 diag("Warning: stderr not defined for '$Txt'");
187 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
189 return $retval;
190 # }}}
191 } # testcmd()
193 sub likecmd {
194 # {{{
195 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
196 defined($descriptions{$Desc}) &&
197 BAIL_OUT("likecmd(): '$Desc' description is used twice");
198 $descriptions{$Desc} = 1;
199 my $stderr_cmd = '';
200 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
201 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
202 my $TMP_STDERR = "$CMDB-stderr.tmp";
203 my $retval = 1;
205 if (defined($Exp_stderr)) {
206 $stderr_cmd = " 2>$TMP_STDERR";
208 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
209 my $ret_val = $?;
210 if (defined($Exp_stderr)) {
211 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
212 unlink($TMP_STDERR);
213 } else {
214 diag("Warning: stderr not defined for '$Txt'");
216 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
218 return $retval;
219 # }}}
220 } # likecmd()
222 sub file_data {
223 # Return file content as a string {{{
224 my $File = shift;
225 my $Txt;
227 open(my $fp, '<', $File) or return undef;
228 local $/ = undef;
229 $Txt = <$fp>;
230 close($fp);
231 return $Txt;
232 # }}}
233 } # file_data()
235 sub print_version {
236 # Print program version {{{
237 print("$progname $VERSION\n");
238 return;
239 # }}}
240 } # print_version()
242 sub usage {
243 # Send the help message to stdout {{{
244 my $Retval = shift;
246 if ($Opt{'verbose'}) {
247 print("\n");
248 print_version();
250 print(<<"END");
252 Usage: $progname [options]
254 Contains tests for the $CMDB(1) program.
256 Options:
258 -a, --all
259 Run all tests, also TODOs.
260 -h, --help
261 Show this help.
262 -q, --quiet
263 Be more quiet. Can be repeated to increase silence.
264 -t, --todo
265 Run only the TODO tests.
266 -v, --verbose
267 Increase level of verbosity. Can be repeated.
268 --version
269 Print version information.
272 exit($Retval);
273 # }}}
274 } # usage()
276 sub msg {
277 # Print a status message to stderr based on verbosity level {{{
278 my ($verbose_level, $Txt) = @_;
280 $verbose_level > $Opt{'verbose'} && return;
281 print(STDERR "$progname: $Txt\n");
282 return;
283 # }}}
284 } # msg()
286 __END__
288 # This program is free software; you can redistribute it and/or modify
289 # it under the terms of the GNU General Public License as published by
290 # the Free Software Foundation; either version 2 of the License, or (at
291 # your option) any later version.
293 # This program is distributed in the hope that it will be useful, but
294 # WITHOUT ANY WARRANTY; without even the implied warranty of
295 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
296 # See the GNU General Public License for more details.
298 # You should have received a copy of the GNU General Public License
299 # along with this program.
300 # If not, see L<http://www.gnu.org/licenses/>.
302 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :