std.c: Don't use `EXIT_SUCCESS`/`EXIT_FAILURE` as function return values
[sunny256-utils.git] / tests / ustr.t
blob5380dbb154ab29f163c418df236ca353aee1b94d
1 #!/usr/bin/env perl
3 #==============================================================================
4 # ustr.t
5 # File ID: 01540956-06f4-11e5-bc56-000df06acc56
7 # Test suite for ustr(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 file for
12 # 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 = "ustr";
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'; # Not used here, $CMD decides
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 my $exec_version = `$CMD --version`;
68 exit(main());
70 sub main {
71 my $Retval = 0;
73 diag('========== BEGIN version info ==========');
74 diag($exec_version);
75 diag('=========== END version info ===========');
77 if ($Opt{'todo'} && !$Opt{'all'}) {
78 goto todo_section;
81 test_standard_options();
82 test_executable();
84 diag('========== BEGIN version info ==========');
85 diag($exec_version);
86 diag('=========== END version info ===========');
88 todo_section:
91 if ($Opt{'all'} || $Opt{'todo'}) {
92 diag('Running TODO tests...');
93 TODO: {
94 local $TODO = '';
95 # Insert TODO tests here.
99 diag('Testing finished.');
101 return $Retval;
104 sub test_standard_options {
105 diag('Testing -h (--help) option...');
106 likecmd("$CMD -h",
107 '/ Show this help/i',
108 '/^$/',
110 'Option -h prints help screen');
112 likecmd("$CMD --help",
114 . '1\xCC\xB6'
115 . '2\xCC\xB6'
116 . '3\xCC\xB6'
117 . ' '
118 . 'a\xCC\xB6'
119 . 'b\xCC\xB6'
120 . 'c\xCC\xB6'
121 . '/',
122 '/^$/',
124 'Strikethrough example in usage screen is displayed correctly');
126 diag('Testing -v (--verbose) option...');
127 likecmd("$CMD -hv",
128 '/^\n\S+ \d+\.\d+\.\d+/s',
129 '/^$/',
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+/',
136 '/^$/',
138 'Option --version returns version number');
140 return;
143 sub test_executable {
144 testcmd("echo Dødens pølse ☠ | $CMD",
145 "D̲ø̲d̲e̲n̲s̲ p̲ø̲l̲s̲e̲ ☠̲\n",
148 'Works with UTF-8');
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",
190 "Bææædi 🐑\n",
193 "-d option deletes underline from UTF-8 string");
194 testcmd("echo B̶æ̶æ̶æ̶d̶i̶ 🐑̶ | $CMD -ds",
195 "Bææædi 🐑\n",
198 "-ds option deletes strikethrough from UTF-8 string");
199 testcmd("printf '\x09With tab' | $CMD",
200 "\tW̲i̲t̲h̲ t̲a̲b̲",
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");
210 return;
213 sub testcmd {
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;
218 my $stderr_cmd = '';
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";
222 my $retval = 1;
224 if (defined($Exp_stderr)) {
225 $stderr_cmd = " 2>$TMP_STDERR";
227 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
228 my $ret_val = $?;
229 if (defined($Exp_stderr)) {
230 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
231 unlink($TMP_STDERR);
232 } else {
233 diag("Warning: stderr not defined for '$Txt'");
235 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
237 return $retval;
240 sub likecmd {
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;
245 my $stderr_cmd = '';
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";
249 my $retval = 1;
251 if (defined($Exp_stderr)) {
252 $stderr_cmd = " 2>$TMP_STDERR";
254 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
255 my $ret_val = $?;
256 if (defined($Exp_stderr)) {
257 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
258 unlink($TMP_STDERR);
259 } else {
260 diag("Warning: stderr not defined for '$Txt'");
262 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
264 return $retval;
267 sub file_data {
268 # Return file content as a string
269 my $File = shift;
270 my $Txt;
272 open(my $fp, '<', $File) or return undef;
273 local $/ = undef;
274 $Txt = <$fp>;
275 close($fp);
277 return $Txt;
280 sub create_file {
281 # Create new file and fill it with data
282 my ($file, $text) = @_;
283 my $retval = 0;
285 open(my $fp, ">", $file) or return 0;
286 print($fp $text);
287 close($fp);
288 $retval = is(file_data($file), $text,
289 "$file was successfully created");
291 return $retval; # 0 if error, 1 if ok
294 sub print_version {
295 # Print program version
296 print("$progname $VERSION\n");
298 return;
301 sub usage {
302 # Send the help message to stdout
303 my $Retval = shift;
305 if ($Opt{'verbose'}) {
306 print("\n");
307 print_version();
309 print(<<"END");
311 Usage: $progname [options]
313 Contains tests for the $CMDB(1) program.
315 Options:
317 -a, --all
318 Run all tests, also TODOs.
319 -h, --help
320 Show this help.
321 -q, --quiet
322 Be more quiet. Can be repeated to increase silence.
323 -t, --todo
324 Run only the TODO tests.
325 -v, --verbose
326 Increase level of verbosity. Can be repeated.
327 --version
328 Print version information.
331 exit($Retval);
334 sub msg {
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");
341 return;
344 __END__
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
349 # version.
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
357 # this program.
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 :