installed_progs.t: Python checks stdout too, 150 ok
[sunny256-utils.git] / tests / findhex.t
blobe3ce5219d19cf4e85f015f98a919b9571bacd617
1 #!/usr/bin/env perl
3 #=======================================================================
4 # findhex.t
5 # File ID: 0d6b92c8-284a-11e5-8487-000df06acc56
7 # Test suite for findhex(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 = "findhex";
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 my $stdtxt = 'asBAdcjkbw abdFF 2349.kjdc3211a abd 2349jk';
122 diag('No options (except -vvv)...');
123 testcmd("echo $stdtxt | $CMD -vvv", # {{{
124 <<'END',
129 2349
130 dc3211a
132 2349
134 "findhex: minlen = '1', maxlen = '0'\n",
136 'Find all hexadecimal numbers',
139 # }}}
140 diag('Testing -d/--decimal option...');
141 testcmd("echo $stdtxt | $CMD -vvv -d", # {{{
142 <<'END',
143 2349
144 3211
145 2349
147 "findhex: minlen = '1', maxlen = '0'\n",
149 'Find all decimal numbers',
152 # }}}
153 diag('Testing -i/--ignore-case option...');
154 testcmd("echo $stdtxt | $CMD -i", # {{{
155 <<'END',
157 badc
159 abdff
160 2349
161 dc3211a
163 2349
167 'Find all hex numbers, regardless of case',
170 # }}}
171 diag('Testing -l/--length option...');
172 testcmd("echo $stdtxt | $CMD -l 4 -vvv", # {{{
173 <<'END',
174 2349
175 2349
177 "findhex: minlen = '4', maxlen = '4'\n",
179 'Find all hexadecimal with length of four chars',
182 # }}}
183 testcmd("echo $stdtxt | $CMD -l 4- -vvv", # {{{
184 <<'END',
185 2349
186 dc3211a
187 2349
189 "findhex: minlen = '4', maxlen = '0'\n",
191 'Find all hex four or more in length',
194 # }}}
195 testcmd("echo $stdtxt | $CMD -l -3 -vvv", # {{{
196 <<'END',
203 "findhex: minlen = '1', maxlen = '3'\n",
205 'Up to three chars in length',
208 # }}}
209 testcmd("echo $stdtxt | $CMD -l 3-4 -vvv", # {{{
210 <<'END',
212 2349
214 2349
216 "findhex: minlen = '3', maxlen = '4'\n",
218 'Three or four chars',
221 # }}}
223 diag('Test various predefined units...');
224 unit_test('arj', 8);
225 unit_test('byte', 2);
226 unit_test('crc16', 4);
227 unit_test('crc32', 8);
228 unit_test('git', 40);
229 unit_test('gzip', 8);
230 unit_test('hg', 40);
231 unit_test('md2', 32);
232 unit_test('md4', 32);
233 unit_test('md5', 32);
234 unit_test('sha0', 40);
235 unit_test('sha1', 40);
236 unit_test('sha224', 56);
237 unit_test('sha256', 64);
238 unit_test('sha384', 96);
239 unit_test('sha512', 128);
240 unit_test('skein256', 64);
241 unit_test('skein384', 96);
242 unit_test('skein512', 128);
243 unit_test('zip', 8);
245 testcmd("$CMD -l yaman", # {{{
247 "findhex: yaman: Unknown length unit\n",
249 'Unknown value',
252 # }}}
253 diag('Testing -u/--unique option...');
254 testcmd("echo $stdtxt | $CMD -u", # {{{
255 <<'END',
260 2349
261 dc3211a
265 'Don\'t print same value twice with -u',
268 # }}}
269 testcmd("echo $stdtxt badc AbD | $CMD -u -i", # {{{
270 <<'END',
272 badc
274 abdff
275 2349
276 dc3211a
281 'Unique with upper/lower case',
284 # }}}
285 diag('Various option combinations...');
286 testcmd("echo $stdtxt | $CMD -vvv -d -l -3", # {{{
288 "findhex: minlen = '1', maxlen = '3'\n",
290 'No decimal-only with length 3 or less',
293 # }}}
294 testcmd("echo $stdtxt | $CMD --decimal --unique", # {{{
295 <<'END',
296 2349
297 3211
301 'Unique decimal',
304 # }}}
305 testcmd("echo $stdtxt | $CMD -i -l -3 --verbose -v -v", # {{{
306 <<'END',
311 "findhex: minlen = '1', maxlen = '3'\n",
313 'Upper/lower case, max length 3',
316 # }}}
317 testcmd("echo $stdtxt | $CMD -i --length 4-5 --verbose -q --quiet -vvvv", # {{{
318 <<'END',
319 badc
320 abdff
321 2349
322 2349
324 "findhex: minlen = '4', maxlen = '5'\n",
326 'Length 4-5, upper/lower case, -v and -q',
329 # }}}
330 todo_section:
333 if ($Opt{'all'} || $Opt{'todo'}) {
334 diag('Running TODO tests...'); # {{{
336 TODO: {
338 local $TODO = '';
339 # Insert TODO tests here.
342 # TODO tests }}}
345 diag('Testing finished.');
346 return $Retval;
347 # }}}
348 } # main()
350 sub unit_test {
351 # {{{
352 my ($name, $size) = @_;
353 return(testcmd("$CMD -vvv -l $name /dev/null",
355 "findhex: minlen = '$size', maxlen = '$size'\n",
357 "Unit: $name, size: $size",
359 # }}}
360 } # unit_test()
362 sub testcmd {
363 # {{{
364 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
365 defined($descriptions{$Desc}) &&
366 BAIL_OUT("testcmd(): '$Desc' description is used twice");
367 $descriptions{$Desc} = 1;
368 my $stderr_cmd = '';
369 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
370 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
371 my $TMP_STDERR = "$CMDB-stderr.tmp";
372 my $retval = 1;
374 if (defined($Exp_stderr)) {
375 $stderr_cmd = " 2>$TMP_STDERR";
377 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
378 my $ret_val = $?;
379 if (defined($Exp_stderr)) {
380 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
381 unlink($TMP_STDERR);
382 } else {
383 diag("Warning: stderr not defined for '$Txt'");
385 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
387 return $retval;
388 # }}}
389 } # testcmd()
391 sub likecmd {
392 # {{{
393 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
394 defined($descriptions{$Desc}) &&
395 BAIL_OUT("likecmd(): '$Desc' description is used twice");
396 $descriptions{$Desc} = 1;
397 my $stderr_cmd = '';
398 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
399 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
400 my $TMP_STDERR = "$CMDB-stderr.tmp";
401 my $retval = 1;
403 if (defined($Exp_stderr)) {
404 $stderr_cmd = " 2>$TMP_STDERR";
406 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
407 my $ret_val = $?;
408 if (defined($Exp_stderr)) {
409 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
410 unlink($TMP_STDERR);
411 } else {
412 diag("Warning: stderr not defined for '$Txt'");
414 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
416 return $retval;
417 # }}}
418 } # likecmd()
420 sub file_data {
421 # Return file content as a string {{{
422 my $File = shift;
423 my $Txt;
425 open(my $fp, '<', $File) or return undef;
426 local $/ = undef;
427 $Txt = <$fp>;
428 close($fp);
429 return $Txt;
430 # }}}
431 } # file_data()
433 sub print_version {
434 # Print program version {{{
435 print("$progname $VERSION\n");
436 return;
437 # }}}
438 } # print_version()
440 sub usage {
441 # Send the help message to stdout {{{
442 my $Retval = shift;
444 if ($Opt{'verbose'}) {
445 print("\n");
446 print_version();
448 print(<<"END");
450 Usage: $progname [options]
452 Contains tests for the $CMDB(1) program.
454 Options:
456 -a, --all
457 Run all tests, also TODOs.
458 -h, --help
459 Show this help.
460 -q, --quiet
461 Be more quiet. Can be repeated to increase silence.
462 -t, --todo
463 Run only the TODO tests.
464 -v, --verbose
465 Increase level of verbosity. Can be repeated.
466 --version
467 Print version information.
470 exit($Retval);
471 # }}}
472 } # usage()
474 sub msg {
475 # Print a status message to stderr based on verbosity level {{{
476 my ($verbose_level, $Txt) = @_;
478 $verbose_level > $Opt{'verbose'} && return;
479 print(STDERR "$progname: $Txt\n");
480 return;
481 # }}}
482 } # msg()
484 __END__
486 # This program is free software; you can redistribute it and/or modify
487 # it under the terms of the GNU General Public License as published by
488 # the Free Software Foundation; either version 2 of the License, or (at
489 # your option) any later version.
491 # This program is distributed in the hope that it will be useful, but
492 # WITHOUT ANY WARRANTY; without even the implied warranty of
493 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
494 # See the GNU General Public License for more details.
496 # You should have received a copy of the GNU General Public License
497 # along with this program.
498 # If not, see L<http://www.gnu.org/licenses/>.
500 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :