3 #=======================================================================
5 # File ID: 01540956-06f4-11e5-bc56-000df06acc56
7 # Test suite for ustr(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 = "ustr";
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 likecmd
("$CMD --help", # {{{
115 'Strikethrough example in usage screen is displayed correctly',
119 diag
('Testing -v (--verbose) option...');
120 likecmd
("$CMD -hv", # {{{
121 '/^\n\S+ \d+\.\d+\.\d+/s',
124 'Option -v with -h returns version number and help screen',
128 diag
('Testing --version option...');
129 likecmd
("$CMD --version", # {{{
130 '/^\S+ \d+\.\d+\.\d+/',
133 'Option --version returns version number',
138 testcmd
("echo Dødens pølse ☠ | $CMD", # {{{
140 D̲ø̲d̲e̲n̲s̲ ̲p̲ø̲l̲s̲e̲ ̲☠̲
148 testcmd
("echo Sausage of death ☠ | $CMD -s", # {{{
150 S̶a̶u̶s̶a̶g̶e̶ ̶o̶f̶ ̶d̶e̶a̶t̶h̶ ̶☠̶
154 'Use -s (strikethrough)',
158 testcmd
("echo Dødens pølse ☠ | $CMD --strikethrough", # {{{
160 D̶ø̶d̶e̶n̶s̶ ̶p̶ø̶l̶s̶e̶ ̶☠̶
164 'Use --strikethrough',
172 if ($Opt{'all'} || $Opt{'todo'}) {
173 diag
('Running TODO tests...'); # {{{
178 # Insert TODO tests here.
184 diag
('Testing finished.');
191 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
192 defined($descriptions{$Desc}) &&
193 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
194 $descriptions{$Desc} = 1;
196 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
197 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
198 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
201 if (defined($Exp_stderr)) {
202 $stderr_cmd = " 2>$TMP_STDERR";
204 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
206 if (defined($Exp_stderr)) {
207 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
210 diag
("Warning: stderr not defined for '$Txt'");
212 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
220 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
221 defined($descriptions{$Desc}) &&
222 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
223 $descriptions{$Desc} = 1;
225 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
226 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
227 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
230 if (defined($Exp_stderr)) {
231 $stderr_cmd = " 2>$TMP_STDERR";
233 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
235 if (defined($Exp_stderr)) {
236 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
239 diag
("Warning: stderr not defined for '$Txt'");
241 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
248 # Return file content as a string {{{
252 open(my $fp, '<', $File) or return undef;
261 # Print program version {{{
262 print("$progname $VERSION\n");
268 # Send the help message to stdout {{{
271 if ($Opt{'verbose'}) {
277 Usage: $progname [options]
279 Contains tests for the $CMD_BASENAME(1) program.
284 Run all tests, also TODOs.
288 Be more quiet. Can be repeated to increase silence.
290 Run only the TODO tests.
292 Increase level of verbosity. Can be repeated.
294 Print version information.
302 # Print a status message to stderr based on verbosity level {{{
303 my ($verbose_level, $Txt) = @_;
305 $verbose_level > $Opt{'verbose'} && return;
306 print(STDERR
"$progname: $Txt\n");
313 # This program is free software; you can redistribute it and/or modify
314 # it under the terms of the GNU General Public License as published by
315 # the Free Software Foundation; either version 2 of the License, or (at
316 # your option) any later version.
318 # This program is distributed in the hope that it will be useful, but
319 # WITHOUT ANY WARRANTY; without even the implied warranty of
320 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
321 # See the GNU General Public License for more details.
323 # You should have received a copy of the GNU General Public License
324 # along with this program.
325 # If not, see L<http://www.gnu.org/licenses/>.
327 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :