installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / tests / zeropad.t
blob6a62932b1427533aac61597b1e6a68faaa27c62d
1 #!/usr/bin/env perl
3 #=======================================================================
4 # zeropad.t
5 # File ID: af9a9f46-f988-11dd-a1c1-000475e441b9
7 # Test suite for zeropad(1).
9 # Character set: UTF-8
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 #=======================================================================
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 = "zeropad";
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 -hv", # {{{
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 testcmd("echo 1 5 12 156 1024 | $CMD", # {{{
122 "0001 0005 0012 0156 1024\n",
125 '1 5 12 156 1024 with LF',
128 # }}}
129 testcmd("echo -n 1 5 12 156 1024 | $CMD", # {{{
130 '0001 0005 0012 0156 1024',
133 'No terminating LF',
136 # }}}
137 testcmd("echo -n 0 jada 1234567 e 345 - 23477 æøå | $CMD", # {{{
138 '0000000 jada 1234567 e 0000345 - 0023477 æøå',
141 'Contains non-numbers',
144 # }}}
145 testcmd("echo -n 0 12345 -4 1000 | $CMD", # {{{
146 '00000 12345 -00004 01000',
149 'Contains negative number',
152 # }}}
153 testcmd("echo -n 0 jada 143022551321802064700594651108964515733 e 345 - 23477 æøå | $CMD", # {{{
154 '000000000000000000000000000000000000000 jada 143022551321802064700594651108964515733 e 000000000000000000000000000000000000345 - 000000000000000000000000000000000023477 æøå',
157 'Manage large numbers',
160 # }}}
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',
169 # }}}
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',
177 # }}}
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',
186 # }}}
187 testcmd("echo -n 1 5 12 156 1024 | $CMD -x", # {{{
188 '0001 0005 0012 0156 1024',
191 '-x: No terminating LF',
194 # }}}
195 testcmd("echo -n 0 jada 1234567 e 345 - 23477 æøå | $CMD -x", # {{{
196 '0000000 j0000ada 1234567 000000e 0000345 - 0023477 æøå',
199 '-x: Contains non-numbers',
202 # }}}
203 testcmd("echo -n 0 12345 -a4 1000 | $CMD -x", # {{{
204 '00000 12345 -000a4 01000',
207 '-x: Contains negative number',
210 # }}}
211 testcmd("echo -n 0 jada 143022551321802064700594651108964515733 e 345 - 23477 æøå | $CMD -x", # {{{
212 '000000000000000000000000000000000000000 j000000000000000000000000000000000000ada 143022551321802064700594651108964515733 00000000000000000000000000000000000000e 000000000000000000000000000000000000345 - 000000000000000000000000000000000023477 æøå',
215 '-x: Manage large numbers',
218 # }}}
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',
226 # }}}
228 todo_section:
231 if ($Opt{'all'} || $Opt{'todo'}) {
232 diag('Running TODO tests...'); # {{{
234 TODO: {
236 local $TODO = '';
237 # Insert TODO tests here.
240 # TODO tests }}}
243 diag('Testing finished.');
244 return $Retval;
245 # }}}
246 } # main()
248 sub testcmd {
249 # {{{
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;
254 my $stderr_cmd = '';
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";
258 my $retval = 1;
260 if (defined($Exp_stderr)) {
261 $stderr_cmd = " 2>$TMP_STDERR";
263 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
264 my $ret_val = $?;
265 if (defined($Exp_stderr)) {
266 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
267 unlink($TMP_STDERR);
268 } else {
269 diag("Warning: stderr not defined for '$Txt'");
271 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
273 return $retval;
274 # }}}
275 } # testcmd()
277 sub likecmd {
278 # {{{
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;
283 my $stderr_cmd = '';
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";
287 my $retval = 1;
289 if (defined($Exp_stderr)) {
290 $stderr_cmd = " 2>$TMP_STDERR";
292 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
293 my $ret_val = $?;
294 if (defined($Exp_stderr)) {
295 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
296 unlink($TMP_STDERR);
297 } else {
298 diag("Warning: stderr not defined for '$Txt'");
300 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
302 return $retval;
303 # }}}
304 } # likecmd()
306 sub file_data {
307 # Return file content as a string {{{
308 my $File = shift;
309 my $Txt;
311 open(my $fp, '<', $File) or return undef;
312 local $/ = undef;
313 $Txt = <$fp>;
314 close($fp);
315 return $Txt;
316 # }}}
317 } # file_data()
319 sub print_version {
320 # Print program version {{{
321 print("$progname $VERSION\n");
322 return;
323 # }}}
324 } # print_version()
326 sub usage {
327 # Send the help message to stdout {{{
328 my $Retval = shift;
330 if ($Opt{'verbose'}) {
331 print("\n");
332 print_version();
334 print(<<"END");
336 Usage: $progname [options]
338 Contains tests for the $CMDB(1) program.
340 Options:
342 -a, --all
343 Run all tests, also TODOs.
344 -h, --help
345 Show this help.
346 -q, --quiet
347 Be more quiet. Can be repeated to increase silence.
348 -t, --todo
349 Run only the TODO tests.
350 -v, --verbose
351 Increase level of verbosity. Can be repeated.
352 --version
353 Print version information.
356 exit($Retval);
357 # }}}
358 } # usage()
360 sub msg {
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");
366 return;
367 # }}}
368 } # msg()
370 __END__
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 :