3 #=======================================================================
5 # File ID: 8ca76f02-ae65-11e5-a68d-fefdb24f8e10
7 # Test suite for md-header(1).
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 #=======================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMD_BASENAME = "md-header";
28 our $CMD = "../$CMD_BASENAME";
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 my $FB = "\x7b\x7b\x7b";
124 testcmd
("$CMD md-header-files/file1.md", # {{{
126 # file1 <!-- ${FB}1 -->
130 ## Header 2 <!-- ${FB}2 -->
134 ### Header 3 <!-- ${FB}3 -->
135 ### 3 with wrong header <!-- ${FB}3 --> <!-- another comment -->
136 #### 4 here <!-- Keep this comment --> <!-- ${FB}4 -->
140 ## Another 2 <!-- ${FB}2 -->
142 And in the end, the love you take is equal to the love you make.
150 testcmd
("$CMD -r md-header-files/file2.md", # {{{
151 file_data
("md-header-files/stripped.md"),
154 "Remove folds with -r",
158 testcmd
("$CMD --remove <md-header-files/file2.md", # {{{
159 file_data
("md-header-files/stripped.md"),
162 "Use --remove and read from stdin",
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",
178 if ($Opt{'all'} || $Opt{'todo'}) {
179 diag
('Running TODO tests...'); # {{{
184 # Insert TODO tests here.
190 diag
('Testing finished.');
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;
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";
207 if (defined($Exp_stderr)) {
208 $stderr_cmd = " 2>$TMP_STDERR";
210 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
212 if (defined($Exp_stderr)) {
213 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
216 diag
("Warning: stderr not defined for '$Txt'");
218 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
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;
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";
236 if (defined($Exp_stderr)) {
237 $stderr_cmd = " 2>$TMP_STDERR";
239 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
241 if (defined($Exp_stderr)) {
242 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
245 diag
("Warning: stderr not defined for '$Txt'");
247 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
254 # Return file content as a string {{{
258 open(my $fp, '<', $File) or return undef;
267 # Print program version {{{
268 print("$progname $VERSION\n");
274 # Send the help message to stdout {{{
277 if ($Opt{'verbose'}) {
283 Usage: $progname [options]
285 Contains tests for the $CMD_BASENAME(1) program.
290 Run all tests, also TODOs.
294 Be more quiet. Can be repeated to increase silence.
296 Run only the TODO tests.
298 Increase level of verbosity. Can be repeated.
300 Print version information.
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");
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 :