3 #=======================================================================
5 # File ID: 311590da-a9db-11e5-8a79-fefdb24f8e10
7 # Test suite for jday(1).
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 #=======================================================================
19 use Test
::More
qw{no_plan
};
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'}) {
72 diag
(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
81 testcmd("$CMD command", # {{{
94 diag
('Testing -h (--help) option...');
95 likecmd
("$CMD -h", # {{{
99 'Option -h prints help screen',
103 diag
('Testing -v (--verbose) option...');
104 likecmd
("$CMD -h -v", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
108 'Option -v with -h returns version number and help screen',
112 diag
('Testing --version option...');
113 likecmd
("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
117 'Option --version returns version number',
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)");
147 if ($Opt{'all'} || $Opt{'todo'}) {
148 diag
('Running TODO tests...'); # {{{
153 # Insert TODO tests here.
159 diag
('Testing finished.');
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;
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";
176 if (defined($Exp_stderr)) {
177 $stderr_cmd = " 2>$TMP_STDERR";
179 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
181 if (defined($Exp_stderr)) {
182 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
185 diag
("Warning: stderr not defined for '$Txt'");
187 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
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;
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";
205 if (defined($Exp_stderr)) {
206 $stderr_cmd = " 2>$TMP_STDERR";
208 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
210 if (defined($Exp_stderr)) {
211 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
214 diag
("Warning: stderr not defined for '$Txt'");
216 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
223 # Return file content as a string {{{
227 open(my $fp, '<', $File) or return undef;
236 # Print program version {{{
237 print("$progname $VERSION\n");
243 # Send the help message to stdout {{{
246 if ($Opt{'verbose'}) {
252 Usage: $progname [options]
254 Contains tests for the $CMDB(1) program.
259 Run all tests, also TODOs.
263 Be more quiet. Can be repeated to increase silence.
265 Run only the TODO tests.
267 Increase level of verbosity. Can be repeated.
269 Print version information.
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");
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 :