Merge branch 'use-trunc-rounding' into rounding-fixes
[gpstools.git] / tests / csv2gpx.t
blobf2ef74ca35babfbc84cce38f5c7cff2222fe91e3
1 #!/usr/bin/perl
3 #=======================================================================
4 # csv2gpx.t
5 # File ID: d4031198-f987-11dd-9f3b-000475e441b9
6 # Test suite for csv2gpx(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 use strict;
15 use warnings;
17 BEGIN {
18 # push(@INC, "$ENV{'HOME'}/bin/STDlibdirDTS");
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 $| = 1;
27 our $Debug = 0;
28 our $CMD = "../csv2gpx";
30 our %Opt = (
32 'all' => 0,
33 'debug' => 0,
34 'help' => 0,
35 'todo' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = "0.00";
45 Getopt::Long::Configure("bundling");
46 GetOptions(
48 "all|a" => \$Opt{'all'},
49 "debug" => \$Opt{'debug'},
50 "help|h" => \$Opt{'help'},
51 "todo|t" => \$Opt{'todo'},
52 "verbose|v+" => \$Opt{'verbose'},
53 "version" => \$Opt{'version'},
55 ) || die("$progname: Option error. Use -h for help.\n");
57 $Opt{'debug'} && ($Debug = 1);
58 $Opt{'help'} && usage(0);
59 if ($Opt{'version'}) {
60 print_version();
61 exit(0);
64 diag(sprintf("========== Executing %s v%s ==========",
65 $progname,
66 $VERSION));
68 if ($Opt{'todo'} && !$Opt{'all'}) {
69 goto todo_section;
72 =pod
74 testcmd("$CMD command", # {{{
75 <<END,
76 [expected stdin]
77 END
78 "",
79 "description",
82 # }}}
84 =cut
86 diag("Testing -h (--help) option...");
87 likecmd("$CMD -h", # {{{
88 '/ Show this help\./',
89 '/^$/',
90 "Option -h prints help screen",
93 # }}}
94 diag("Testing -v (--verbose) option...");
95 likecmd("$CMD -hv", # {{{
96 '/^\n\S+ v\d\.\d\d\n/s',
97 '/^$/',
98 "Option --version with -h returns version number and help screen",
101 # }}}
102 diag("Testing --version option...");
103 likecmd("$CMD --version", # {{{
104 '/^\S+ v\d\.\d\d\n/',
105 '/^$/',
106 "Option --version returns version number",
109 # }}}
111 todo_section:
114 if ($Opt{'all'} || $Opt{'todo'}) {
115 diag("Running TODO tests..."); # {{{
117 TODO: {
119 local $TODO = "";
120 # Insert TODO tests here.
123 # TODO tests }}}
126 diag("Testing finished.");
128 sub testcmd {
129 # {{{
130 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
131 my $stderr_cmd = "";
132 my $deb_str = $Opt{'debug'} ? " --debug" : "";
133 my $Txt = join("",
134 "\"$Cmd\"",
135 defined($Desc)
136 ? " - $Desc"
137 : ""
139 my $TMP_STDERR = "csv2gpx-stderr.tmp";
141 if (defined($Exp_stderr) && !length($deb_str)) {
142 $stderr_cmd = " 2>$TMP_STDERR";
144 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
145 if (defined($Exp_stderr)) {
146 if (!length($deb_str)) {
147 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
148 unlink($TMP_STDERR);
150 } else {
151 diag("Warning: stderr not defined for '$Txt'");
153 # }}}
154 } # testcmd()
156 sub likecmd {
157 # {{{
158 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
159 my $stderr_cmd = "";
160 my $deb_str = $Opt{'debug'} ? " --debug" : "";
161 my $Txt = join("",
162 "\"$Cmd\"",
163 defined($Desc)
164 ? " - $Desc"
165 : ""
167 my $TMP_STDERR = "csv2gpx-stderr.tmp";
169 if (defined($Exp_stderr) && !length($deb_str)) {
170 $stderr_cmd = " 2>$TMP_STDERR";
172 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
173 if (defined($Exp_stderr)) {
174 if (!length($deb_str)) {
175 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
176 unlink($TMP_STDERR);
178 } else {
179 diag("Warning: stderr not defined for '$Txt'");
181 # }}}
182 } # likecmd()
184 sub file_data {
185 # Return file content as a string {{{
186 my $File = shift;
187 my $Txt;
188 if (open(my $fp, "<", $File)) {
189 $Txt = join("", <$fp>);
190 close($fp);
191 return($Txt);
192 } else {
193 return;
195 # }}}
196 } # file_data()
198 sub print_version {
199 # Print program version {{{
200 print("$progname v$VERSION\n");
201 # }}}
202 } # print_version()
204 sub usage {
205 # Send the help message to stdout {{{
206 my $Retval = shift;
208 if ($Opt{'verbose'}) {
209 print("\n");
210 print_version();
212 print(<<END);
214 Usage: $progname [options] [file [files [...]]]
216 Contains tests for the csv2gpx(1) program.
218 Options:
220 -a, --all
221 Run all tests, also TODOs.
222 -h, --help
223 Show this help.
224 -t, --todo
225 Run only the TODO tests.
226 -v, --verbose
227 Increase level of verbosity. Can be repeated.
228 --version
229 Print version information.
230 --debug
231 Print debugging messages.
234 exit($Retval);
235 # }}}
236 } # usage()
238 sub msg {
239 # Print a status message to stderr based on verbosity level {{{
240 my ($verbose_level, $Txt) = @_;
242 if ($Opt{'verbose'} >= $verbose_level) {
243 print(STDERR "$progname: $Txt\n");
245 # }}}
246 } # msg()
248 __END__
250 # Plain Old Documentation (POD) {{{
252 =pod
254 =head1 NAME
256 run-tests.pl
258 =head1 SYNOPSIS
260 csv2gpx.t [options] [file [files [...]]]
262 =head1 DESCRIPTION
264 Contains tests for the csv2gpx(1) program.
266 =head1 OPTIONS
268 =over 4
270 =item B<-a>, B<--all>
272 Run all tests, also TODOs.
274 =item B<-h>, B<--help>
276 Print a brief help summary.
278 =item B<-t>, B<--todo>
280 Run only the TODO tests.
282 =item B<-v>, B<--verbose>
284 Increase level of verbosity. Can be repeated.
286 =item B<--version>
288 Print version information.
290 =item B<--debug>
292 Print debugging messages.
294 =back
296 =head1 AUTHOR
298 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
300 =head1 COPYRIGHT
302 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
303 This is free software; see the file F<COPYING> for legalese stuff.
305 =head1 LICENCE
307 This program is free software: you can redistribute it and/or modify it
308 under the terms of the GNU General Public License as published by the
309 Free Software Foundation, either version 3 of the License, or (at your
310 option) any later version.
312 This program is distributed in the hope that it will be useful, but
313 WITHOUT ANY WARRANTY; without even the implied warranty of
314 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
315 See the GNU General Public License for more details.
317 You should have received a copy of the GNU General Public License along
318 with this program.
319 If not, see L<http://www.gnu.org/licenses/>.
321 =head1 SEE ALSO
323 =cut
325 # }}}
327 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :