create_funcs.sql: Remove output noise from loop_wayp_new()
[gpstools.git] / tests / gpst-file / gpst-file.t
blob4304b721adfb2d278fe1f04d6b9cc7e8e224d946
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # tests/gpst-file/gpst-file.t
5 # File ID: bbcbed10-009f-11de-824e-000475e441b9
6 # Test suite for gpst-file(1).
8 # Character set: UTF-8
9 # ©opyleft 2009– Ø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 = "../../gpst-file";
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 chdir("files") || die("$progname: files/: Cannot chdir: $!\n");
67 system("tar xzf testfile.tar.gz");
68 chdir("..");
70 if ($Opt{'todo'} && !$Opt{'all'}) {
71 goto todo_section;
74 =pod
76 testcmd("$CMD command", # {{{
77 <<END,
78 [expected stdin]
79 END
80 "",
81 "description",
84 # }}}
86 =cut
88 diag("Testing --author option...");
89 testcmd("$CMD -a sunny files/testfile/file.txt", # {{{
90 <<END,
91 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\tsunny
92 END
93 "",
94 "--author option works",
97 # }}}
98 testcmd("$CMD -a sunny -o xml files/testfile/file.txt", # {{{
99 <<END,
100 <?xml version="1.0" encoding="UTF-8"?>
101 <gpstfile>
102 <file>
103 <filename>file.txt</filename>
104 <date>2009-02-23T21:58:01</date>
105 <author>sunny</author>
106 </file>
107 </gpstfile>
110 "Author info is included with -o xml",
113 # }}}
114 # diag("Testing --debug option...");
115 diag("Testing --description option...");
116 testcmd("$CMD -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
117 <<END,
118 1\t2009-02-23T21:58:01\t\\N\tDescription stuff \\\\&<>\tfile.txt\t\\N
121 "--description with backslash and stuff",
124 # }}}
125 testcmd("$CMD -o xml -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
126 <<END,
127 <?xml version="1.0" encoding="UTF-8"?>
128 <gpstfile>
129 <file>
130 <filename>file.txt</filename>
131 <date>2009-02-23T21:58:01</date>
132 <desc>Description stuff \\&amp;&lt;&gt;</desc>
133 </file>
134 </gpstfile>
137 "--description with backslash and stuff",
140 # }}}
141 diag("Testing -h (--help) option...");
142 likecmd("$CMD -h", # {{{
143 '/ Show this help\./',
144 '/^$/',
145 "Option -h prints help screen",
148 # }}}
149 diag("Testing --output-format option..."); # {{{
150 # pgtab
151 testcmd("$CMD -o pgtab files/testfile/file.txt", # {{{
152 <<END,
153 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
156 "Output pgtab format from DSC_4426.JPG",
159 # }}}
160 # xml
161 testcmd("$CMD -o xml files/testfile/file.txt", # {{{
162 <<END,
163 <?xml version="1.0" encoding="UTF-8"?>
164 <gpstfile>
165 <file>
166 <filename>file.txt</filename>
167 <date>2009-02-23T21:58:01</date>
168 </file>
169 </gpstfile>
172 "Output XML information for DSC_4426.JPG",
175 # }}}
176 # Unknown format
177 testcmd("$CMD -o blurfl files/testfile/file.txt", # {{{
179 "gpst-file: blurfl: Unknown output format\n",
180 "Unknown output format specified",
183 # }}}
184 # }}} --output-format
185 diag("Testing -w (--strip-whitespace) option...");
186 testcmd("$CMD -w -o xml files/testfile/file.txt", # {{{
187 <<END,
188 <?xml version="1.0" encoding="UTF-8"?>
189 <gpstfile>
190 <file>
191 <filename>file.txt</filename>
192 <date>2009-02-23T21:58:01</date>
193 </file>
194 </gpstfile>
197 "Strip whitespace from XML",
200 # }}}
201 diag("Testing -T (--timezone) option...");
202 testcmd("$CMD --timezone +1234 files/testfile/file.txt", # {{{
203 <<END,
204 1\t2009-02-23T21:58:01+1234\t\\N\t\\N\tfile.txt\t\\N
207 "--timezone works",
210 # }}}
211 testcmd("$CMD -T +0200 files/testfile/file.txt", # {{{
212 <<END,
213 1\t2009-02-23T21:58:01+0200\t\\N\t\\N\tfile.txt\t\\N
216 "Positive time zone",
219 # }}}
220 testcmd("$CMD -T-0600 files/testfile/file.txt", # {{{
221 <<END,
222 1\t2009-02-23T21:58:01-0600\t\\N\t\\N\tfile.txt\t\\N
225 "Negative time zone",
228 # }}}
229 testcmd("$CMD -T CET files/testfile/file.txt", # {{{
230 <<END,
231 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
234 "Time zone abbreviation",
237 # }}}
238 testcmd("$CMD -T cet files/testfile/file.txt", # {{{
239 <<END,
240 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
243 "Time zone is converted to upper case",
246 # }}}
247 testcmd("$CMD -T Z files/testfile/file.txt", # {{{
248 <<END,
249 1\t2009-02-23T21:58:01Z\t\\N\t\\N\tfile.txt\t\\N
252 "Zulu abbreviation",
255 # }}}
256 testcmd("$CMD -T erf324 files/testfile/file.txt", # {{{
258 "gpst-file: erf324: Invalid time zone\n",
259 "Invalid time zone abbr, contains digits",
262 # }}}
263 testcmd("$CMD -T CET -o xml files/testfile/file.txt", # {{{
264 <<END,
265 <?xml version="1.0" encoding="UTF-8"?>
266 <gpstfile>
267 <file>
268 <filename>file.txt</filename>
269 <date>2009-02-23T21:58:01 CET</date>
270 </file>
271 </gpstfile>
274 "Time zone abbr. works with -o xml",
277 # }}}
278 testcmd("$CMD -T Z -o xml files/testfile/file.txt", # {{{
279 <<END,
280 <?xml version="1.0" encoding="UTF-8"?>
281 <gpstfile>
282 <file>
283 <filename>file.txt</filename>
284 <date>2009-02-23T21:58:01Z</date>
285 </file>
286 </gpstfile>
289 "Zulu time zone works with -o xml",
292 # }}}
293 diag("Testing -v (--verbose) option...");
294 likecmd("$CMD -hv", # {{{
295 '/^\n\S+ v\d\.\d\d\n/s',
296 '/^$/',
297 "Option --version with -h returns version number and help screen",
300 # }}}
301 diag("Testing --version option...");
302 likecmd("$CMD --version", # {{{
303 '/^\S+ v\d\.\d\d\n/',
304 '/^$/',
305 "Option --version returns version number",
308 # }}}
309 diag("Various...");
310 testcmd("$CMD files/testfile/file.txt", # {{{
311 <<END,
312 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
315 "No options",
318 # }}}
319 testcmd("echo files/testfile/file.txt | $CMD", # {{{
320 <<END,
321 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
324 "Read filename from stdin",
327 # }}}
329 todo_section:
332 if ($Opt{'all'} || $Opt{'todo'}) {
333 diag("Running TODO tests..."); # {{{
335 TODO: {
337 local $TODO = "";
338 # Insert TODO tests here.
341 # TODO tests }}}
344 unlink("files/testfile/file.txt") || warn("$progname: files/testfile/file.txt: Cannot remove file: $!\n");
345 rmdir("files/testfile") || warn("$progname: files/testfile: Cannot remove directory: $!\n");
347 diag("Testing finished.");
349 sub testcmd {
350 # {{{
351 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
352 my $stderr_cmd = "";
353 my $deb_str = $Opt{'debug'} ? " --debug" : "";
354 my $Txt = join("",
355 "\"$Cmd\"",
356 defined($Desc)
357 ? " - $Desc"
358 : ""
360 my $TMP_STDERR = "gpst-file-stderr.tmp";
362 if (defined($Exp_stderr) && !length($deb_str)) {
363 $stderr_cmd = " 2>$TMP_STDERR";
365 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
366 if (defined($Exp_stderr)) {
367 if (!length($deb_str)) {
368 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
369 unlink($TMP_STDERR);
371 } else {
372 diag("Warning: stderr not defined for '$Txt'");
374 # }}}
375 } # testcmd()
377 sub likecmd {
378 # {{{
379 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
380 my $stderr_cmd = "";
381 my $deb_str = $Opt{'debug'} ? " --debug" : "";
382 my $Txt = join("",
383 "\"$Cmd\"",
384 defined($Desc)
385 ? " - $Desc"
386 : ""
388 my $TMP_STDERR = "gpst-file-stderr.tmp";
390 if (defined($Exp_stderr) && !length($deb_str)) {
391 $stderr_cmd = " 2>$TMP_STDERR";
393 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
394 if (defined($Exp_stderr)) {
395 if (!length($deb_str)) {
396 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
397 unlink($TMP_STDERR);
399 } else {
400 diag("Warning: stderr not defined for '$Txt'");
402 # }}}
403 } # likecmd()
405 sub file_data {
406 # Return file content as a string {{{
407 my $File = shift;
408 my $Txt;
409 if (open(FP, "<", $File)) {
410 $Txt = join("", <FP>);
411 close(FP);
412 return($Txt);
413 } else {
414 return undef;
416 # }}}
417 } # file_data()
419 sub print_version {
420 # Print program version {{{
421 print("$progname v$VERSION\n");
422 # }}}
423 } # print_version()
425 sub usage {
426 # Send the help message to stdout {{{
427 my $Retval = shift;
429 if ($Opt{'verbose'}) {
430 print("\n");
431 print_version();
433 print(<<END);
435 Usage: $progname [options] [file [files [...]]]
437 Contains tests for the gpst-file(1) program.
439 Options:
441 -a, --all
442 Run all tests, also TODOs.
443 -h, --help
444 Show this help.
445 -t, --todo
446 Run only the TODO tests.
447 -v, --verbose
448 Increase level of verbosity. Can be repeated.
449 --version
450 Print version information.
451 --debug
452 Print debugging messages.
455 exit($Retval);
456 # }}}
457 } # usage()
459 sub msg {
460 # Print a status message to stderr based on verbosity level {{{
461 my ($verbose_level, $Txt) = @_;
463 if ($Opt{'verbose'} >= $verbose_level) {
464 print(STDERR "$progname: $Txt\n");
466 # }}}
467 } # msg()
469 __END__
471 # Plain Old Documentation (POD) {{{
473 =pod
475 =head1 NAME
477 run-tests.pl
479 =head1 SYNOPSIS
481 ./gpst-file.t [options] [file [files [...]]]
483 =head1 DESCRIPTION
485 Contains tests for the gpst-file(1) program.
487 =head1 OPTIONS
489 =over 4
491 =item B<-a>, B<--all>
493 Run all tests, also TODOs.
495 =item B<-h>, B<--help>
497 Print a brief help summary.
499 =item B<-t>, B<--todo>
501 Run only the TODO tests.
503 =item B<-v>, B<--verbose>
505 Increase level of verbosity. Can be repeated.
507 =item B<--version>
509 Print version information.
511 =item B<--debug>
513 Print debugging messages.
515 =back
517 =head1 AUTHOR
519 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
521 =head1 COPYRIGHT
523 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
524 This is free software; see the file F<COPYING> for legalese stuff.
526 =head1 LICENCE
528 This program is free software: you can redistribute it and/or modify it
529 under the terms of the GNU General Public License as published by the
530 Free Software Foundation, either version 3 of the License, or (at your
531 option) any later version.
533 This program is distributed in the hope that it will be useful, but
534 WITHOUT ANY WARRANTY; without even the implied warranty of
535 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
536 See the GNU General Public License for more details.
538 You should have received a copy of the GNU General Public License along
539 with this program.
540 If not, see L<http://www.gnu.org/licenses/>.
542 =head1 SEE ALSO
544 =cut
546 # }}}
548 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :