datefmt: Define 1 year as 365.2425 days instead of 365.25
[sunny256-utils.git] / tests / hhi.t
blobb7aeef6b4c63db44087ccd8c227fac0f365dfdc5
1 #!/usr/bin/env perl
3 #=======================================================================
4 # hhi.t
5 # File ID: 3137a138-17e1-11e1-8c10-73d289505142
7 # Test suite for hhi(1).
9 # Character set: UTF-8
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 #=======================================================================
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 = "hhi";
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 # }}}
122 diag('Testing --no-number option...');
123 testcmd("$CMD -n hhi-files/file.html", # {{{
124 <<'END',
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">
128 <!-- file.html -->
129 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
130 <head>
131 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
132 <title>file.html</title>
133 </head>
134 <body>
136 <!-- hhitoc -->
137 <ul>
138 <!-- {{{ -->
139 <li><span>Secondary header</span>
140 <ul>
141 <li><span>Tertiary header</span>
142 </li>
143 </ul>
144 </li>
145 <li><span>Another h2</span>
146 </li>
147 <li><span>Yet another h2</span>
148 <ul>
149 <li><span>Last h3</span>
150 </li>
151 </ul>
152 </li>
153 <!-- }}} -->
154 </ul>
155 <!-- /hhitoc -->
157 <h1>Top header</h1>
158 <h2>Secondary header</h2>
159 <h3>Tertiary header</h3>
160 <h2>Another h2</h2>
161 <h2>Yet another h2</h2>
162 <h3>Last h3</h3>
164 </body>
165 </html>
169 'Use -n option',
172 # }}}
174 diag("Use no options...");
175 testcmd("$CMD hhi-files/file.html", # {{{
176 <<'END',
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">
180 <!-- file.html -->
181 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
182 <head>
183 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
184 <title>file.html</title>
185 </head>
186 <body>
188 <!-- hhitoc -->
189 <ul>
190 <!-- {{{ -->
191 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
192 <ul>
193 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
194 </li>
195 </ul>
196 </li>
197 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
198 </li>
199 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
200 <ul>
201 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
202 </li>
203 </ul>
204 </li>
205 <!-- }}} -->
206 </ul>
207 <!-- /hhitoc -->
209 <h1>Top header</h1>
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>
216 </body>
217 </html>
221 'Without options',
224 # }}}
225 testcmd("$CMD hhi-files/file.html | $CMD", # {{{
226 <<'END',
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">
230 <!-- file.html -->
231 <!-- File ID: 5920dcf0-17e1-11e1-8cf3-5730346fba47 -->
232 <head>
233 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
234 <title>file.html</title>
235 </head>
236 <body>
238 <!-- hhitoc -->
239 <ul>
240 <!-- {{{ -->
241 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
242 <ul>
243 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
244 </li>
245 </ul>
246 </li>
247 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
248 </li>
249 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
250 <ul>
251 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
252 </li>
253 </ul>
254 </li>
255 <!-- }}} -->
256 </ul>
257 <!-- /hhitoc -->
259 <h1>Top header</h1>
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>
266 </body>
267 </html>
271 "Filter through an additional $CMD",
274 # }}}
275 testcmd("$CMD hhi-files/nohhi.html", # {{{
276 <<'END',
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">
280 <!-- nohhi.html -->
281 <!-- File ID: 1d10f504-17f2-11e1-8054-b5f0f6e1e052 -->
282 <head>
283 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
284 <title>nohhi.html</title>
285 </head>
286 <body>
288 <!-- hhitoc -->
289 <ul>
290 <!-- {{{ -->
291 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
292 </li>
293 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
294 </li>
295 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
296 <ul>
297 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
298 </li>
299 </ul>
300 </li>
301 <!-- }}} -->
302 </ul>
303 <!-- /hhitoc -->
305 <h1>Top header</h1>
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>
312 </body>
313 </html>
317 'Skip header marked with <!-- nohhi -->',
320 # }}}
321 testcmd("$CMD hhi-files/name.html", # {{{
322 <<'END',
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">
326 <!-- name.html -->
327 <!-- File ID: 14bbd044-17f3-11e1-9a44-712f8d2632f0 -->
328 <head>
329 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
330 <title>name.html</title>
331 </head>
332 <body>
334 <!-- hhitoc -->
335 <ul>
336 <!-- {{{ -->
337 <li><span><b><a href="#h-1">1.</a></b> Secondary header</span>
338 <ul>
339 <li><span><b><a href="#h-1.1">1.1</a></b> Tertiary header</span>
340 </li>
341 </ul>
342 </li>
343 <li><span><b><a href="#h-2">2.</a></b> Another h2</span>
344 </li>
345 <li><span><b><a href="#h-3">3.</a></b> Yet another h2</span>
346 <ul>
347 <li><span><b><a href="#h-3.1">3.1</a></b> Last h3</span>
348 </li>
349 </ul>
350 </li>
351 <!-- }}} -->
352 </ul>
353 <!-- /hhitoc -->
355 <h1>Top header</h1>
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>
362 </body>
363 </html>
367 'Replace name with id',
370 # }}}
372 todo_section:
375 if ($Opt{'all'} || $Opt{'todo'}) {
376 diag('Running TODO tests...'); # {{{
378 TODO: {
380 local $TODO = '';
381 # Insert TODO tests here.
384 # TODO tests }}}
387 diag('Testing finished.');
388 return $Retval;
389 # }}}
390 } # main()
392 sub testcmd {
393 # {{{
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;
398 my $stderr_cmd = '';
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";
402 my $retval = 1;
404 if (defined($Exp_stderr)) {
405 $stderr_cmd = " 2>$TMP_STDERR";
407 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
408 my $ret_val = $?;
409 if (defined($Exp_stderr)) {
410 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
411 unlink($TMP_STDERR);
412 } else {
413 diag("Warning: stderr not defined for '$Txt'");
415 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
417 return $retval;
418 # }}}
419 } # testcmd()
421 sub likecmd {
422 # {{{
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;
427 my $stderr_cmd = '';
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";
431 my $retval = 1;
433 if (defined($Exp_stderr)) {
434 $stderr_cmd = " 2>$TMP_STDERR";
436 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
437 my $ret_val = $?;
438 if (defined($Exp_stderr)) {
439 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
440 unlink($TMP_STDERR);
441 } else {
442 diag("Warning: stderr not defined for '$Txt'");
444 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
446 return $retval;
447 # }}}
448 } # likecmd()
450 sub file_data {
451 # Return file content as a string {{{
452 my $File = shift;
453 my $Txt;
455 open(my $fp, '<', $File) or return undef;
456 local $/ = undef;
457 $Txt = <$fp>;
458 close($fp);
459 return $Txt;
460 # }}}
461 } # file_data()
463 sub print_version {
464 # Print program version {{{
465 print("$progname $VERSION\n");
466 return;
467 # }}}
468 } # print_version()
470 sub usage {
471 # Send the help message to stdout {{{
472 my $Retval = shift;
474 if ($Opt{'verbose'}) {
475 print("\n");
476 print_version();
478 print(<<"END");
480 Usage: $progname [options]
482 Contains tests for the $CMDB(1) program.
484 Options:
486 -a, --all
487 Run all tests, also TODOs.
488 -h, --help
489 Show this help.
490 -q, --quiet
491 Be more quiet. Can be repeated to increase silence.
492 -t, --todo
493 Run only the TODO tests.
494 -v, --verbose
495 Increase level of verbosity. Can be repeated.
496 --version
497 Print version information.
500 exit($Retval);
501 # }}}
502 } # usage()
504 sub msg {
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");
510 return;
511 # }}}
512 } # msg()
514 __END__
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 :