3 #=======================================================================
5 # File ID: af9a9f46-f988-11dd-a1c1-000475e441b9
7 # Test suite for zeropad(1).
10 # ©opyleft 2008– Ø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
};
27 our $CMDB = "zeropad";
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 -hv", # {{{
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 testcmd
("echo 1 5 12 156 1024 | $CMD", # {{{
122 "0001 0005 0012 0156 1024\n",
125 '1 5 12 156 1024 with LF',
129 testcmd
("echo -n 1 5 12 156 1024 | $CMD", # {{{
130 '0001 0005 0012 0156 1024',
137 testcmd
("echo -n 0 jada 1234567 e 345 - 23477 æøå | $CMD", # {{{
138 '0000000 jada 1234567 e 0000345 - 0023477 æøå',
141 'Contains non-numbers',
145 testcmd
("echo -n 0 12345 -4 1000 | $CMD", # {{{
146 '00000 12345 -00004 01000',
149 'Contains negative number',
153 testcmd
("echo -n 0 jada 143022551321802064700594651108964515733 e 345 - 23477 æøå | $CMD", # {{{
154 '000000000000000000000000000000000000000 jada 143022551321802064700594651108964515733 e 000000000000000000000000000000000000345 - 000000000000000000000000000000000023477 æøå',
157 'Manage large numbers',
161 diag
('Testing -s/--size option...');
162 testcmd
("echo -n 1 234444 5 2312 233333333156 1024 | $CMD -s 4", # {{{
163 '0001 234444 0005 2312 233333333156 1024',
166 'Pad decimal numbers to four digits, -s 4',
170 testcmd
("echo -n 0 jada 1234567 e 345 - 23477 æøå | $CMD -x --size 5", # {{{
171 '00000 j00ada 1234567 0000e 00345 - 23477 æøå',
174 'Pad to five hex digits with --size 5',
178 diag
('Testing -x/--hex option...');
179 testcmd
("echo 1 5 12 156 1024 | $CMD -x", # {{{
180 "0001 0005 0012 0156 1024\n",
183 '-x: 1 5 12 156 1024 with LF',
187 testcmd
("echo -n 1 5 12 156 1024 | $CMD -x", # {{{
188 '0001 0005 0012 0156 1024',
191 '-x: No terminating LF',
195 testcmd
("echo -n 0 jada 1234567 e 345 - 23477 æøå | $CMD -x", # {{{
196 '0000000 j0000ada 1234567 000000e 0000345 - 0023477 æøå',
199 '-x: Contains non-numbers',
203 testcmd
("echo -n 0 12345 -a4 1000 | $CMD -x", # {{{
204 '00000 12345 -000a4 01000',
207 '-x: Contains negative number',
211 testcmd
("echo -n 0 jada 143022551321802064700594651108964515733 e 345 - 23477 æøå | $CMD -x", # {{{
212 '000000000000000000000000000000000000000 j000000000000000000000000000000000000ada 143022551321802064700594651108964515733 00000000000000000000000000000000000000e 000000000000000000000000000000000000345 - 000000000000000000000000000000000023477 æøå',
215 '-x: Manage large numbers',
219 testcmd
("echo 76abCD ffFFf f F yyyabcyyy | $CMD --hex", # {{{
220 "76abCD 0ffFFf 00000f 00000F yyy000abcyyy\n",
223 '--hex: Upper/lower case and "abc" inside word',
231 if ($Opt{'all'} || $Opt{'todo'}) {
232 diag
('Running TODO tests...'); # {{{
237 # Insert TODO tests here.
243 diag
('Testing finished.');
250 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
251 defined($descriptions{$Desc}) &&
252 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
253 $descriptions{$Desc} = 1;
255 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
256 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
257 my $TMP_STDERR = "$CMDB-stderr.tmp";
260 if (defined($Exp_stderr)) {
261 $stderr_cmd = " 2>$TMP_STDERR";
263 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
265 if (defined($Exp_stderr)) {
266 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
269 diag
("Warning: stderr not defined for '$Txt'");
271 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
279 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
280 defined($descriptions{$Desc}) &&
281 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
282 $descriptions{$Desc} = 1;
284 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
285 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
286 my $TMP_STDERR = "$CMDB-stderr.tmp";
289 if (defined($Exp_stderr)) {
290 $stderr_cmd = " 2>$TMP_STDERR";
292 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
294 if (defined($Exp_stderr)) {
295 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
298 diag
("Warning: stderr not defined for '$Txt'");
300 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
307 # Return file content as a string {{{
311 open(my $fp, '<', $File) or return undef;
320 # Print program version {{{
321 print("$progname $VERSION\n");
327 # Send the help message to stdout {{{
330 if ($Opt{'verbose'}) {
336 Usage: $progname [options]
338 Contains tests for the $CMDB(1) program.
343 Run all tests, also TODOs.
347 Be more quiet. Can be repeated to increase silence.
349 Run only the TODO tests.
351 Increase level of verbosity. Can be repeated.
353 Print version information.
361 # Print a status message to stderr based on verbosity level {{{
362 my ($verbose_level, $Txt) = @_;
364 $verbose_level > $Opt{'verbose'} && return;
365 print(STDERR
"$progname: $Txt\n");
372 # This program is free software; you can redistribute it and/or modify
373 # it under the terms of the GNU General Public License as published by
374 # the Free Software Foundation; either version 2 of the License, or (at
375 # your option) any later version.
377 # This program is distributed in the hope that it will be useful, but
378 # WITHOUT ANY WARRANTY; without even the implied warranty of
379 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
380 # See the GNU General Public License for more details.
382 # You should have received a copy of the GNU General Public License
383 # along with this program.
384 # If not, see L<http://www.gnu.org/licenses/>.
386 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :