tests: Don’t use bareword file handles in file_data()
[gpstools.git] / tests / addpoints / addpoints.t
blobd8ff6e64ffbfcbfad6c3e0ca97c7383c903688e6
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # tests/addpoints/addpoints.t
5 # File ID: ccab1138-f924-11dd-9694-0001805bf4b1
6 # Test suite for addpoints(1).
8 # Character set: UTF-8
9 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 3 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 BEGIN {
15 # push(@INC, "$ENV{'HOME'}/bin/STDlibdirDTS");
16 use Test::More qw{no_plan};
17 # use_ok() goes here
20 use strict;
21 use Getopt::Long;
23 $| = 1;
25 our $Debug = 0;
26 our $CMD = "../../addpoints";
28 our %Opt = (
30 'all' => 0,
31 'debug' => 0,
32 'help' => 0,
33 'todo' => 0,
34 'verbose' => 0,
35 'version' => 0,
39 our $progname = $0;
40 $progname =~ s/^.*\/(.*?)$/$1/;
41 our $VERSION = "0.00";
43 Getopt::Long::Configure("bundling");
44 GetOptions(
46 "all|a" => \$Opt{'all'},
47 "debug" => \$Opt{'debug'},
48 "help|h" => \$Opt{'help'},
49 "todo|t" => \$Opt{'todo'},
50 "verbose|v+" => \$Opt{'verbose'},
51 "version" => \$Opt{'version'},
53 ) || die("$progname: Option error. Use -h for help.\n");
55 $Opt{'debug'} && ($Debug = 1);
56 $Opt{'help'} && usage(0);
57 if ($Opt{'version'}) {
58 print_version();
59 exit(0);
62 diag(sprintf("========== Executing %s v%s ==========",
63 $progname,
64 $VERSION));
66 if ($Opt{'todo'} && !$Opt{'all'}) {
67 goto todo_section;
70 =pod
72 testcmd("$CMD command", # {{{
73 <<END,
74 [expected stdin]
75 END
76 "",
77 "description",
80 # }}}
82 =cut
84 diag("Testing -h (--help) option...");
85 likecmd("$CMD -h", # {{{
86 '/ Show this help\./',
87 '/^$/',
88 "Option -h prints help screen",
91 # }}}
92 diag("Testing -v (--verbose) option...");
93 likecmd("$CMD -hv", # {{{
94 '/^\n\S+ v\d\.\d\d\n/s',
95 '/^$/',
96 "Option --version with -h returns version number and help screen",
99 # }}}
100 diag("Testing --version option...");
101 likecmd("$CMD --version", # {{{
102 '/^\S+ v\d\.\d\d\n/',
103 '/^$/',
104 "Option --version returns version number",
107 # }}}
109 todo_section:
112 if ($Opt{'all'} || $Opt{'todo'}) {
113 diag("Running TODO tests..."); # {{{
115 TODO: {
117 local $TODO = "";
118 # Insert TODO tests here.
121 # TODO tests }}}
124 diag("Testing finished.");
126 sub testcmd {
127 # {{{
128 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
129 my $stderr_cmd = "";
130 my $deb_str = $Opt{'debug'} ? " --debug" : "";
131 my $Txt = join("",
132 "\"$Cmd\"",
133 defined($Desc)
134 ? " - $Desc"
135 : ""
137 my $TMP_STDERR = "addpoints-stderr.tmp";
139 if (defined($Exp_stderr) && !length($deb_str)) {
140 $stderr_cmd = " 2>$TMP_STDERR";
142 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
143 if (defined($Exp_stderr)) {
144 if (!length($deb_str)) {
145 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
146 unlink($TMP_STDERR);
148 } else {
149 diag("Warning: stderr not defined for '$Txt'");
151 # }}}
152 } # testcmd()
154 sub likecmd {
155 # {{{
156 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
157 my $stderr_cmd = "";
158 my $deb_str = $Opt{'debug'} ? " --debug" : "";
159 my $Txt = join("",
160 "\"$Cmd\"",
161 defined($Desc)
162 ? " - $Desc"
163 : ""
165 my $TMP_STDERR = "addpoints-stderr.tmp";
167 if (defined($Exp_stderr) && !length($deb_str)) {
168 $stderr_cmd = " 2>$TMP_STDERR";
170 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
171 if (defined($Exp_stderr)) {
172 if (!length($deb_str)) {
173 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
174 unlink($TMP_STDERR);
176 } else {
177 diag("Warning: stderr not defined for '$Txt'");
179 # }}}
180 } # likecmd()
182 sub file_data {
183 # Return file content as a string {{{
184 my $File = shift;
185 my $Txt;
186 if (open(my $fp, "<", $File)) {
187 $Txt = join("", <$fp>);
188 close($fp);
189 return($Txt);
190 } else {
191 return undef;
193 # }}}
194 } # file_data()
196 sub print_version {
197 # Print program version {{{
198 print("$progname v$VERSION\n");
199 # }}}
200 } # print_version()
202 sub usage {
203 # Send the help message to stdout {{{
204 my $Retval = shift;
206 if ($Opt{'verbose'}) {
207 print("\n");
208 print_version();
210 print(<<END);
212 Usage: $progname [options] [file [files [...]]]
214 Contains tests for the addpoints(1) program.
216 Options:
218 -a, --all
219 Run all tests, also TODOs.
220 -h, --help
221 Show this help.
222 -t, --todo
223 Run only the TODO tests.
224 -v, --verbose
225 Increase level of verbosity. Can be repeated.
226 --version
227 Print version information.
228 --debug
229 Print debugging messages.
232 exit($Retval);
233 # }}}
234 } # usage()
236 sub msg {
237 # Print a status message to stderr based on verbosity level {{{
238 my ($verbose_level, $Txt) = @_;
240 if ($Opt{'verbose'} >= $verbose_level) {
241 print(STDERR "$progname: $Txt\n");
243 # }}}
244 } # msg()
246 __END__
248 # Plain Old Documentation (POD) {{{
250 =pod
252 =head1 NAME
254 run-tests.pl
256 =head1 SYNOPSIS
258 addpoints.t [options] [file [files [...]]]
260 =head1 DESCRIPTION
262 Contains tests for the addpoints(1) program.
264 =head1 OPTIONS
266 =over 4
268 =item B<-a>, B<--all>
270 Run all tests, also TODOs.
272 =item B<-h>, B<--help>
274 Print a brief help summary.
276 =item B<-t>, B<--todo>
278 Run only the TODO tests.
280 =item B<-v>, B<--verbose>
282 Increase level of verbosity. Can be repeated.
284 =item B<--version>
286 Print version information.
288 =item B<--debug>
290 Print debugging messages.
292 =back
294 =head1 AUTHOR
296 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
298 =head1 COPYRIGHT
300 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
301 This is free software; see the file F<COPYING> for legalese stuff.
303 =head1 LICENCE
305 This program is free software: you can redistribute it and/or modify it
306 under the terms of the GNU General Public License as published by the
307 Free Software Foundation, either version 3 of the License, or (at your
308 option) any later version.
310 This program is distributed in the hope that it will be useful, but
311 WITHOUT ANY WARRANTY; without even the implied warranty of
312 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
313 See the GNU General Public License for more details.
315 You should have received a copy of the GNU General Public License along
316 with this program.
317 If not, see L<http://www.gnu.org/licenses/>.
319 =head1 SEE ALSO
321 =cut
323 # }}}
325 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :