3 #=======================================================================
5 # File ID: 3137a138-17e1-11e1-8c10-73d289505142
7 # Test suite for hhi(1).
10 # ©opyleft 2011– Ø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 -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',
122 diag
('Testing --no-number option...');
123 testcmd
("$CMD -n hhi-files/file.html", # {{{
125 <?xml version="1.0" encoding="UTF-8"?>
126 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
127 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
129 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
131 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
132 <title>file.html</title>
139 <li><span>Secondary header</span>
141 <li><span>Tertiary header</span>
145 <li><span>Another h2</span>
147 <li><span>Yet another h2</span>
149 <li><span>Last h3</span>
158 <h2>Secondary header</h2>
159 <h3>Tertiary header</h3>
161 <h2>Yet another h2</h2>
174 diag
("Use no options...");
175 testcmd
("$CMD hhi-files/file.html", # {{{
177 <?xml version="1.0" encoding="UTF-8"?>
178 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
179 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
181 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
183 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
184 <title>file.html</title>
191 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
193 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
197 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
199 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
201 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
210 <h2><a id="h-1">1.</a> Secondary header</h2>
211 <h3><a id="h-1.1">1.1</a> Tertiary header</h3>
212 <h2><a id="h-2">2.</a> Another h2</h2>
213 <h2><a id="h-3">3.</a> Yet another h2</h2>
214 <h3><a id="h-3.1">3.1</a> Last h3</h3>
225 testcmd
("$CMD hhi-files/file.html | $CMD", # {{{
227 <?xml version="1.0" encoding="UTF-8"?>
228 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
229 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
231 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
233 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
234 <title>file.html</title>
241 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
243 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
247 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
249 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
251 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
260 <h2><a id="h-1">1.</a> Secondary header</h2>
261 <h3><a id="h-1.1">1.1</a> Tertiary header</h3>
262 <h2><a id="h-2">2.</a> Another h2</h2>
263 <h2><a id="h-3">3.</a> Yet another h2</h2>
264 <h3><a id="h-3.1">3.1</a> Last h3</h3>
271 "Filter through an additional $CMD",
275 testcmd
("$CMD hhi-files/nohhi.html", # {{{
277 <?xml version="1.0" encoding="UTF-8"?>
278 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
279 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
281 <!-- File ID: 1d10f504-17f2-11e1-8054-b5f0f6e1e052 -->
283 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
284 <title>nohhi.html</title>
291 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
293 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
295 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
297 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
306 <h2><a id="h-1">1.</a> Secondary header</h2>
307 <h3>Tertiary header</h3> <!-- nohhi -->
308 <h2><a id="h-2">2.</a> Another h2</h2>
309 <h2><a id="h-3">3.</a> Yet another h2</h2>
310 <h3><a id="h-3.1">3.1</a> Last h3</h3>
317 'Skip header marked with <!-- nohhi -->',
321 testcmd
("$CMD hhi-files/name.html", # {{{
323 <?xml version="1.0" encoding="UTF-8"?>
324 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
325 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no">
327 <!-- File ID: 14bbd044-17f3-11e1-9a44-712f8d2632f0 -->
329 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
330 <title>name.html</title>
337 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
339 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
343 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
345 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
347 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
356 <h2><a id="h-1">1.</a> Secondary header</h2>
357 <h3><a id="h-1.1">1.1</a> Tertiary header</h3>
358 <h2><a id="h-2">2.</a> Another h2</h2>
359 <h2><a id="h-3">3.</a> Yet another h2</h2>
360 <h3><a id="h-3.1">3.1</a> Last h3</h3>
367 'Replace name with id',
375 if ($Opt{'all'} || $Opt{'todo'}) {
376 diag
('Running TODO tests...'); # {{{
381 # Insert TODO tests here.
387 diag
('Testing finished.');
394 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
395 defined($descriptions{$Desc}) &&
396 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
397 $descriptions{$Desc} = 1;
399 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
400 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
401 my $TMP_STDERR = "$CMDB-stderr.tmp";
404 if (defined($Exp_stderr)) {
405 $stderr_cmd = " 2>$TMP_STDERR";
407 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
409 if (defined($Exp_stderr)) {
410 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
413 diag
("Warning: stderr not defined for '$Txt'");
415 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
423 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
424 defined($descriptions{$Desc}) &&
425 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
426 $descriptions{$Desc} = 1;
428 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
429 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
430 my $TMP_STDERR = "$CMDB-stderr.tmp";
433 if (defined($Exp_stderr)) {
434 $stderr_cmd = " 2>$TMP_STDERR";
436 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
438 if (defined($Exp_stderr)) {
439 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
442 diag
("Warning: stderr not defined for '$Txt'");
444 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
451 # Return file content as a string {{{
455 open(my $fp, '<', $File) or return undef;
464 # Print program version {{{
465 print("$progname $VERSION\n");
471 # Send the help message to stdout {{{
474 if ($Opt{'verbose'}) {
480 Usage: $progname [options]
482 Contains tests for the $CMDB(1) program.
487 Run all tests, also TODOs.
491 Be more quiet. Can be repeated to increase silence.
493 Run only the TODO tests.
495 Increase level of verbosity. Can be repeated.
497 Print version information.
505 # Print a status message to stderr based on verbosity level {{{
506 my ($verbose_level, $Txt) = @_;
508 $verbose_level > $Opt{'verbose'} && return;
509 print(STDERR
"$progname: $Txt\n");
516 # This program is free software; you can redistribute it and/or modify
517 # it under the terms of the GNU General Public License as published by
518 # the Free Software Foundation; either version 2 of the License, or (at
519 # your option) any later version.
521 # This program is distributed in the hope that it will be useful, but
522 # WITHOUT ANY WARRANTY; without even the implied warranty of
523 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
524 # See the GNU General Public License for more details.
526 # You should have received a copy of the GNU General Public License
527 # along with this program.
528 # If not, see L<http://www.gnu.org/licenses/>.
530 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :