Lib/std/c/src/: Add UNUSED, compile unused code
[sunny256-utils.git] / tests / md-header.t
blob5c11ed4ab7d6823d003a2a195423b0e27a0b3f19
1 #!/usr/bin/env perl
3 #=======================================================================
4 # md-header.t
5 # File ID: 8ca76f02-ae65-11e5-a68d-fefdb24f8e10
7 # Test suite for md-header(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 $CMD_BASENAME = "md-header";
28 our $CMD = "../$CMD_BASENAME";
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 my $FB = "\x7b\x7b\x7b";
124 testcmd("$CMD md-header-files/file1.md", # {{{
125 <<END,
126 # file1 <!-- ${FB}1 -->
128 This is it.
130 ## Header 2 <!-- ${FB}2 -->
132 Indeed.
134 ### Header 3 <!-- ${FB}3 -->
135 ### 3 with wrong header <!-- ${FB}3 --> <!-- another comment -->
136 #### 4 here <!-- Keep this comment --> <!-- ${FB}4 -->
138 Gurgle
140 ## Another 2 <!-- ${FB}2 -->
142 And in the end, the love you take is equal to the love you make.
146 "Format file1.md",
149 # }}}
150 testcmd("$CMD -r md-header-files/file2.md", # {{{
151 file_data("md-header-files/stripped.md"),
154 "Remove folds with -r",
157 # }}}
158 testcmd("$CMD --remove <md-header-files/file2.md", # {{{
159 file_data("md-header-files/stripped.md"),
162 "Use --remove and read from stdin",
165 # }}}
166 testcmd("$CMD md-header-files/stripped.md | $CMD -r", # {{{
167 file_data("md-header-files/stripped.md"),
170 "Add and remove folds in a single pipe",
173 # }}}
175 todo_section:
178 if ($Opt{'all'} || $Opt{'todo'}) {
179 diag('Running TODO tests...'); # {{{
181 TODO: {
183 local $TODO = '';
184 # Insert TODO tests here.
187 # TODO tests }}}
190 diag('Testing finished.');
191 return $Retval;
192 # }}}
193 } # main()
195 sub testcmd {
196 # {{{
197 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
198 defined($descriptions{$Desc}) &&
199 BAIL_OUT("testcmd(): '$Desc' description is used twice");
200 $descriptions{$Desc} = 1;
201 my $stderr_cmd = '';
202 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
203 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
204 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
205 my $retval = 1;
207 if (defined($Exp_stderr)) {
208 $stderr_cmd = " 2>$TMP_STDERR";
210 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
211 my $ret_val = $?;
212 if (defined($Exp_stderr)) {
213 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
214 unlink($TMP_STDERR);
215 } else {
216 diag("Warning: stderr not defined for '$Txt'");
218 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
220 return $retval;
221 # }}}
222 } # testcmd()
224 sub likecmd {
225 # {{{
226 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
227 defined($descriptions{$Desc}) &&
228 BAIL_OUT("likecmd(): '$Desc' description is used twice");
229 $descriptions{$Desc} = 1;
230 my $stderr_cmd = '';
231 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
232 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
233 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
234 my $retval = 1;
236 if (defined($Exp_stderr)) {
237 $stderr_cmd = " 2>$TMP_STDERR";
239 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
240 my $ret_val = $?;
241 if (defined($Exp_stderr)) {
242 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
243 unlink($TMP_STDERR);
244 } else {
245 diag("Warning: stderr not defined for '$Txt'");
247 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
249 return $retval;
250 # }}}
251 } # likecmd()
253 sub file_data {
254 # Return file content as a string {{{
255 my $File = shift;
256 my $Txt;
258 open(my $fp, '<', $File) or return undef;
259 local $/ = undef;
260 $Txt = <$fp>;
261 close($fp);
262 return $Txt;
263 # }}}
264 } # file_data()
266 sub print_version {
267 # Print program version {{{
268 print("$progname $VERSION\n");
269 return;
270 # }}}
271 } # print_version()
273 sub usage {
274 # Send the help message to stdout {{{
275 my $Retval = shift;
277 if ($Opt{'verbose'}) {
278 print("\n");
279 print_version();
281 print(<<"END");
283 Usage: $progname [options]
285 Contains tests for the $CMD_BASENAME(1) program.
287 Options:
289 -a, --all
290 Run all tests, also TODOs.
291 -h, --help
292 Show this help.
293 -q, --quiet
294 Be more quiet. Can be repeated to increase silence.
295 -t, --todo
296 Run only the TODO tests.
297 -v, --verbose
298 Increase level of verbosity. Can be repeated.
299 --version
300 Print version information.
303 exit($Retval);
304 # }}}
305 } # usage()
307 sub msg {
308 # Print a status message to stderr based on verbosity level {{{
309 my ($verbose_level, $Txt) = @_;
311 $verbose_level > $Opt{'verbose'} && return;
312 print(STDERR "$progname: $Txt\n");
313 return;
314 # }}}
315 } # msg()
317 __END__
319 # This program is free software; you can redistribute it and/or modify
320 # it under the terms of the GNU General Public License as published by
321 # the Free Software Foundation; either version 2 of the License, or (at
322 # your option) any later version.
324 # This program is distributed in the hope that it will be useful, but
325 # WITHOUT ANY WARRANTY; without even the implied warranty of
326 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
327 # See the GNU General Public License for more details.
329 # You should have received a copy of the GNU General Public License
330 # along with this program.
331 # If not, see L<http://www.gnu.org/licenses/>.
333 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :