std.t: create_file(): Use `>` as separate parameter in open()
[sunny256-utils.git] / tests / ustr.t
blob9dfb7e329daf59f37940b2e2c957000de7bb4657
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
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 = "ustr";
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 likecmd("$CMD --help", # {{{
104 '/' .
105 '1\xCC\xB6' .
106 '2\xCC\xB6' .
107 '3\xCC\xB6' .
108 ' \xCC\xB6' .
109 'a\xCC\xB6' .
110 'b\xCC\xB6' .
111 'c\xCC\xB6' .
112 '/',
113 '/^$/',
115 'Strikethrough example in usage screen is displayed correctly',
118 # }}}
119 diag('Testing -v (--verbose) option...');
120 likecmd("$CMD -hv", # {{{
121 '/^\n\S+ \d+\.\d+\.\d+/s',
122 '/^$/',
124 'Option -v with -h returns version number and help screen',
127 # }}}
128 diag('Testing --version option...');
129 likecmd("$CMD --version", # {{{
130 '/^\S+ \d+\.\d+\.\d+/',
131 '/^$/',
133 'Option --version returns version number',
136 # }}}
138 testcmd("echo Dødens pølse ☠ | $CMD", # {{{
139 <<'END',
140 D̲ø̲d̲e̲n̲s̲ ̲p̲ø̲l̲s̲e̲ ̲☠̲
144 'Works with UTF-8',
147 # }}}
148 testcmd("echo Sausage of death ☠ | $CMD -s", # {{{
149 <<'END',
150 S̶a̶u̶s̶a̶g̶e̶ ̶o̶f̶ ̶d̶e̶a̶t̶h̶ ̶☠̶
154 'Use -s (strikethrough)',
157 # }}}
158 testcmd("echo Dødens pølse ☠ | $CMD --strikethrough", # {{{
159 <<'END',
160 D̶ø̶d̶e̶n̶s̶ ̶p̶ø̶l̶s̶e̶ ̶☠̶
164 'Use --strikethrough',
167 # }}}
169 todo_section:
172 if ($Opt{'all'} || $Opt{'todo'}) {
173 diag('Running TODO tests...'); # {{{
175 TODO: {
177 local $TODO = '';
178 # Insert TODO tests here.
181 # TODO tests }}}
184 diag('Testing finished.');
185 return $Retval;
186 # }}}
187 } # main()
189 sub testcmd {
190 # {{{
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;
195 my $stderr_cmd = '';
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";
199 my $retval = 1;
201 if (defined($Exp_stderr)) {
202 $stderr_cmd = " 2>$TMP_STDERR";
204 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
205 my $ret_val = $?;
206 if (defined($Exp_stderr)) {
207 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
208 unlink($TMP_STDERR);
209 } else {
210 diag("Warning: stderr not defined for '$Txt'");
212 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
214 return $retval;
215 # }}}
216 } # testcmd()
218 sub likecmd {
219 # {{{
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;
224 my $stderr_cmd = '';
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";
228 my $retval = 1;
230 if (defined($Exp_stderr)) {
231 $stderr_cmd = " 2>$TMP_STDERR";
233 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
234 my $ret_val = $?;
235 if (defined($Exp_stderr)) {
236 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
237 unlink($TMP_STDERR);
238 } else {
239 diag("Warning: stderr not defined for '$Txt'");
241 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
243 return $retval;
244 # }}}
245 } # likecmd()
247 sub file_data {
248 # Return file content as a string {{{
249 my $File = shift;
250 my $Txt;
252 open(my $fp, '<', $File) or return undef;
253 local $/ = undef;
254 $Txt = <$fp>;
255 close($fp);
256 return $Txt;
257 # }}}
258 } # file_data()
260 sub print_version {
261 # Print program version {{{
262 print("$progname $VERSION\n");
263 return;
264 # }}}
265 } # print_version()
267 sub usage {
268 # Send the help message to stdout {{{
269 my $Retval = shift;
271 if ($Opt{'verbose'}) {
272 print("\n");
273 print_version();
275 print(<<"END");
277 Usage: $progname [options]
279 Contains tests for the $CMD_BASENAME(1) program.
281 Options:
283 -a, --all
284 Run all tests, also TODOs.
285 -h, --help
286 Show this help.
287 -q, --quiet
288 Be more quiet. Can be repeated to increase silence.
289 -t, --todo
290 Run only the TODO tests.
291 -v, --verbose
292 Increase level of verbosity. Can be repeated.
293 --version
294 Print version information.
297 exit($Retval);
298 # }}}
299 } # usage()
301 sub msg {
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");
307 return;
308 # }}}
309 } # msg()
311 __END__
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 :