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 file for
13 #==============================================================================
19 use Test
::More
qw{no_plan
};
28 our $CMD = "../$CMDB";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0'; # Not used here, $CMD decides
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'}) {
66 my $exec_version = `$CMD --version`;
73 diag
('========== BEGIN version info ==========');
75 diag
('=========== END version info ===========');
77 if ($Opt{'todo'} && !$Opt{'all'}) {
81 test_standard_options
();
84 diag
('========== BEGIN version info ==========');
86 diag
('=========== END version info ===========');
91 if ($Opt{'all'} || $Opt{'todo'}) {
92 diag
('Running TODO tests...');
95 # Insert TODO tests here.
99 diag
('Testing finished.');
104 sub test_standard_options
{
105 diag
('Testing -h (--help) option...');
107 '/ Show this help/i',
110 'Option -h prints help screen');
112 likecmd
("$CMD --help",
124 'Strikethrough example in usage screen is displayed correctly');
126 diag
('Testing -v (--verbose) option...');
128 '/^\n\S+ \d+\.\d+\.\d+/s',
131 'Option -v with -h returns version number and help screen');
133 diag
('Testing --version option...');
134 likecmd
("$CMD --version",
135 '/^\S+ \d+\.\d+\.\d+/',
138 'Option --version returns version number');
143 sub test_executable
{
144 testcmd
("echo Dødens pølse ☠ | $CMD",
145 "D̲ø̲d̲e̲n̲s̲ p̲ø̲l̲s̲e̲ ☠̲\n",
149 testcmd
("echo Sausage of death ☠ | $CMD -s",
150 "S̶a̶u̶s̶a̶g̶e̶ o̶f̶ d̶e̶a̶t̶h̶ ☠̶\n",
153 'Use -s (strikethrough)');
154 testcmd
("echo Dødens pølse ☠ | $CMD --strikethrough",
155 "D̶ø̶d̶e̶n̶s̶ p̶ø̶l̶s̶e̶ ☠̶\n",
158 'Use --strikethrough');
159 testcmd
("echo Dødens pølse ☠ | $CMD -S",
160 "D̲ø̲d̲e̲n̲s̲ ̲p̲ø̲l̲s̲e̲ ̲☠̲\n",
163 '-S includes space');
164 testcmd
("echo Sausage of death ☠ | $CMD -sS",
165 "S̶a̶u̶s̶a̶g̶e̶ ̶o̶f̶ ̶d̶e̶a̶t̶h̶ ̶☠̶\n",
168 'Strikethrough, -S includes space');
169 testcmd
("echo S̲t̲a̲n̲d̲a̲r̲d̲ A̲S̲C̲I̲I̲ h̲e̲r̲e̲ | $CMD -d",
170 "Standard ASCII here\n",
173 "-d option deletes underline");
174 testcmd
("echo S̶t̶a̶n̶d̶a̶r̶d̶ A̶S̶C̶I̶I̶ h̶e̶r̶e̶ | $CMD -ds",
175 "Standard ASCII here\n",
178 "-ds deletes strikethrough");
179 testcmd
("echo S̲t̲a̲n̲d̲a̲r̲d̲ A̲S̲C̲I̲I̲ h̲e̲r̲e̲ | $CMD -ds",
180 "S̲t̲a̲n̲d̲a̲r̲d̲ A̲S̲C̲I̲I̲ h̲e̲r̲e̲\n",
183 "-ds doesn't delete underline");
184 testcmd
("echo S̶t̶a̶n̶d̶a̶r̶d̶ A̶S̶C̶I̶I̶ h̶e̶r̶e̶ | $CMD -d",
185 "S̶t̶a̶n̶d̶a̶r̶d̶ A̶S̶C̶I̶I̶ h̶e̶r̶e̶\n",
188 "-d doesn't delete strikethrough");
189 testcmd
("echo B̲æ̲æ̲æ̲d̲i̲ 🐑̲ | $CMD -d",
193 "-d option deletes underline from UTF-8 string");
194 testcmd
("echo B̶æ̶æ̶æ̶d̶i̶ 🐑̶ | $CMD -ds",
198 "-ds option deletes strikethrough from UTF-8 string");
199 testcmd
("printf '\x09With tab' | $CMD",
203 "TABs (U+0009) aren't touched");
204 testcmd
("printf '\x09With tab\x09' | $CMD --space",
205 "\tW̲i̲t̲h̲ ̲t̲a̲b̲\t",
208 "TABs (U+0009) aren't touched, even with --space");
214 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
215 defined($descriptions{$Desc})
216 && BAIL_OUT
("testcmd(): '$Desc' description is used twice");
217 $descriptions{$Desc} = 1;
219 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
220 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
221 my $TMP_STDERR = "$CMDB-stderr.tmp";
224 if (defined($Exp_stderr)) {
225 $stderr_cmd = " 2>$TMP_STDERR";
227 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
229 if (defined($Exp_stderr)) {
230 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
233 diag
("Warning: stderr not defined for '$Txt'");
235 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
241 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
242 defined($descriptions{$Desc})
243 && BAIL_OUT
("likecmd(): '$Desc' description is used twice");
244 $descriptions{$Desc} = 1;
246 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
247 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
248 my $TMP_STDERR = "$CMDB-stderr.tmp";
251 if (defined($Exp_stderr)) {
252 $stderr_cmd = " 2>$TMP_STDERR";
254 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
256 if (defined($Exp_stderr)) {
257 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
260 diag
("Warning: stderr not defined for '$Txt'");
262 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
268 # Return file content as a string
272 open(my $fp, '<', $File) or return undef;
281 # Create new file and fill it with data
282 my ($file, $text) = @_;
285 open(my $fp, ">", $file) or return 0;
288 $retval = is
(file_data
($file), $text,
289 "$file was successfully created");
291 return $retval; # 0 if error, 1 if ok
295 # Print program version
296 print("$progname $VERSION\n");
302 # Send the help message to stdout
305 if ($Opt{'verbose'}) {
311 Usage: $progname [options]
313 Contains tests for the $CMDB(1) program.
318 Run all tests, also TODOs.
322 Be more quiet. Can be repeated to increase silence.
324 Run only the TODO tests.
326 Increase level of verbosity. Can be repeated.
328 Print version information.
335 # Print a status message to stderr based on verbosity level
336 my ($verbose_level, $Txt) = @_;
338 $verbose_level > $Opt{'verbose'} && return;
339 print(STDERR
"$progname: $Txt\n");
346 # This program is free software; you can redistribute it and/or modify it under
347 # the terms of the GNU General Public License as published by the Free Software
348 # Foundation; either version 2 of the License, or (at your option) any later
351 # This program is distributed in the hope that it will be useful, but WITHOUT
352 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
353 # FOR A PARTICULAR PURPOSE.
354 # See the GNU General Public License for more details.
356 # You should have received a copy of the GNU General Public License along with
358 # If not, see L<http://www.gnu.org/licenses/>.
360 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :