Use "#!/usr/bin/env perl" in all Perl scripts
[gpstools.git] / tests / gpst-file / gpst-file.t
blob104d35082200fcea46568d9013cf028a4fb99af4
1 #!/usr/bin/env perl
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 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 = "../../gpst-file";
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 chdir("files") || die("$progname: files/: Cannot chdir: $!\n");
69 system("tar xzf testfile.tar.gz");
70 chdir("..");
72 if ($Opt{'todo'} && !$Opt{'all'}) {
73 goto todo_section;
76 =pod
78 testcmd("$CMD command", # {{{
79 <<END,
80 [expected stdin]
81 END
82 "",
83 "description",
86 # }}}
88 =cut
90 diag("Testing --author option...");
91 testcmd("$CMD -a sunny files/testfile/file.txt", # {{{
92 <<END,
93 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\tsunny
94 END
95 "",
96 "--author option works",
99 # }}}
100 testcmd("$CMD -a sunny -o xml files/testfile/file.txt", # {{{
101 <<END,
102 <?xml version="1.0" encoding="UTF-8"?>
103 <gpstfile>
104 <file>
105 <filename>file.txt</filename>
106 <date>2009-02-23T21:58:01</date>
107 <author>sunny</author>
108 </file>
109 </gpstfile>
112 "Author info is included with -o xml",
115 # }}}
116 # diag("Testing --debug option...");
117 diag("Testing --description option...");
118 testcmd("$CMD -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
119 <<END,
120 1\t2009-02-23T21:58:01\t\\N\tDescription stuff \\\\&<>\tfile.txt\t\\N
123 "--description with backslash and stuff",
126 # }}}
127 testcmd("$CMD -o xml -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
128 <<END,
129 <?xml version="1.0" encoding="UTF-8"?>
130 <gpstfile>
131 <file>
132 <filename>file.txt</filename>
133 <date>2009-02-23T21:58:01</date>
134 <desc>Description stuff \\&amp;&lt;&gt;</desc>
135 </file>
136 </gpstfile>
139 "--description with backslash and stuff",
142 # }}}
143 diag("Testing -h (--help) option...");
144 likecmd("$CMD -h", # {{{
145 '/ Show this help\./',
146 '/^$/',
147 "Option -h prints help screen",
150 # }}}
151 diag("Testing --output-format option..."); # {{{
152 # pgtab
153 testcmd("$CMD -o pgtab files/testfile/file.txt", # {{{
154 <<END,
155 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
158 "Output pgtab format from DSC_4426.JPG",
161 # }}}
162 # xml
163 testcmd("$CMD -o xml files/testfile/file.txt", # {{{
164 <<END,
165 <?xml version="1.0" encoding="UTF-8"?>
166 <gpstfile>
167 <file>
168 <filename>file.txt</filename>
169 <date>2009-02-23T21:58:01</date>
170 </file>
171 </gpstfile>
174 "Output XML information for DSC_4426.JPG",
177 # }}}
178 # Unknown format
179 testcmd("$CMD -o blurfl files/testfile/file.txt", # {{{
181 "gpst-file: blurfl: Unknown output format\n",
182 "Unknown output format specified",
185 # }}}
186 # }}} --output-format
187 diag("Testing -w (--strip-whitespace) option...");
188 testcmd("$CMD -w -o xml files/testfile/file.txt", # {{{
189 <<END,
190 <?xml version="1.0" encoding="UTF-8"?>
191 <gpstfile>
192 <file>
193 <filename>file.txt</filename>
194 <date>2009-02-23T21:58:01</date>
195 </file>
196 </gpstfile>
199 "Strip whitespace from XML",
202 # }}}
203 diag("Testing -T (--timezone) option...");
204 testcmd("$CMD --timezone +1234 files/testfile/file.txt", # {{{
205 <<END,
206 1\t2009-02-23T21:58:01+1234\t\\N\t\\N\tfile.txt\t\\N
209 "--timezone works",
212 # }}}
213 testcmd("$CMD -T +0200 files/testfile/file.txt", # {{{
214 <<END,
215 1\t2009-02-23T21:58:01+0200\t\\N\t\\N\tfile.txt\t\\N
218 "Positive time zone",
221 # }}}
222 testcmd("$CMD -T-0600 files/testfile/file.txt", # {{{
223 <<END,
224 1\t2009-02-23T21:58:01-0600\t\\N\t\\N\tfile.txt\t\\N
227 "Negative time zone",
230 # }}}
231 testcmd("$CMD -T CET files/testfile/file.txt", # {{{
232 <<END,
233 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
236 "Time zone abbreviation",
239 # }}}
240 testcmd("$CMD -T cet files/testfile/file.txt", # {{{
241 <<END,
242 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
245 "Time zone is converted to upper case",
248 # }}}
249 testcmd("$CMD -T Z files/testfile/file.txt", # {{{
250 <<END,
251 1\t2009-02-23T21:58:01Z\t\\N\t\\N\tfile.txt\t\\N
254 "Zulu abbreviation",
257 # }}}
258 testcmd("$CMD -T erf324 files/testfile/file.txt", # {{{
260 "gpst-file: erf324: Invalid time zone\n",
261 "Invalid time zone abbr, contains digits",
264 # }}}
265 testcmd("$CMD -T CET -o xml files/testfile/file.txt", # {{{
266 <<END,
267 <?xml version="1.0" encoding="UTF-8"?>
268 <gpstfile>
269 <file>
270 <filename>file.txt</filename>
271 <date>2009-02-23T21:58:01 CET</date>
272 </file>
273 </gpstfile>
276 "Time zone abbr. works with -o xml",
279 # }}}
280 testcmd("$CMD -T Z -o xml files/testfile/file.txt", # {{{
281 <<END,
282 <?xml version="1.0" encoding="UTF-8"?>
283 <gpstfile>
284 <file>
285 <filename>file.txt</filename>
286 <date>2009-02-23T21:58:01Z</date>
287 </file>
288 </gpstfile>
291 "Zulu time zone works with -o xml",
294 # }}}
295 diag("Testing -v (--verbose) option...");
296 likecmd("$CMD -hv", # {{{
297 '/^\n\S+ v\d\.\d\d\n/s',
298 '/^$/',
299 "Option --version with -h returns version number and help screen",
302 # }}}
303 diag("Testing --version option...");
304 likecmd("$CMD --version", # {{{
305 '/^\S+ v\d\.\d\d\n/',
306 '/^$/',
307 "Option --version returns version number",
310 # }}}
311 diag("Various...");
312 testcmd("$CMD files/testfile/file.txt", # {{{
313 <<END,
314 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
317 "No options",
320 # }}}
321 testcmd("echo files/testfile/file.txt | $CMD", # {{{
322 <<END,
323 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
326 "Read filename from stdin",
329 # }}}
331 todo_section:
334 if ($Opt{'all'} || $Opt{'todo'}) {
335 diag("Running TODO tests..."); # {{{
337 TODO: {
339 local $TODO = "";
340 # Insert TODO tests here.
343 # TODO tests }}}
346 unlink("files/testfile/file.txt") || warn("$progname: files/testfile/file.txt: Cannot remove file: $!\n");
347 rmdir("files/testfile") || warn("$progname: files/testfile: Cannot remove directory: $!\n");
349 diag("Testing finished.");
351 sub testcmd {
352 # {{{
353 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
354 my $stderr_cmd = "";
355 my $deb_str = $Opt{'debug'} ? " --debug" : "";
356 my $Txt = join("",
357 "\"$Cmd\"",
358 defined($Desc)
359 ? " - $Desc"
360 : ""
362 my $TMP_STDERR = "gpst-file-stderr.tmp";
364 if (defined($Exp_stderr) && !length($deb_str)) {
365 $stderr_cmd = " 2>$TMP_STDERR";
367 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
368 if (defined($Exp_stderr)) {
369 if (!length($deb_str)) {
370 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
371 unlink($TMP_STDERR);
373 } else {
374 diag("Warning: stderr not defined for '$Txt'");
376 # }}}
377 } # testcmd()
379 sub likecmd {
380 # {{{
381 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
382 my $stderr_cmd = "";
383 my $deb_str = $Opt{'debug'} ? " --debug" : "";
384 my $Txt = join("",
385 "\"$Cmd\"",
386 defined($Desc)
387 ? " - $Desc"
388 : ""
390 my $TMP_STDERR = "gpst-file-stderr.tmp";
392 if (defined($Exp_stderr) && !length($deb_str)) {
393 $stderr_cmd = " 2>$TMP_STDERR";
395 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
396 if (defined($Exp_stderr)) {
397 if (!length($deb_str)) {
398 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
399 unlink($TMP_STDERR);
401 } else {
402 diag("Warning: stderr not defined for '$Txt'");
404 # }}}
405 } # likecmd()
407 sub file_data {
408 # Return file content as a string {{{
409 my $File = shift;
410 my $Txt;
411 if (open(my $fp, "<", $File)) {
412 $Txt = join("", <$fp>);
413 close($fp);
414 return($Txt);
415 } else {
416 return;
418 # }}}
419 } # file_data()
421 sub print_version {
422 # Print program version {{{
423 print("$progname v$VERSION\n");
424 # }}}
425 } # print_version()
427 sub usage {
428 # Send the help message to stdout {{{
429 my $Retval = shift;
431 if ($Opt{'verbose'}) {
432 print("\n");
433 print_version();
435 print(<<END);
437 Usage: $progname [options] [file [files [...]]]
439 Contains tests for the gpst-file(1) program.
441 Options:
443 -a, --all
444 Run all tests, also TODOs.
445 -h, --help
446 Show this help.
447 -t, --todo
448 Run only the TODO tests.
449 -v, --verbose
450 Increase level of verbosity. Can be repeated.
451 --version
452 Print version information.
453 --debug
454 Print debugging messages.
457 exit($Retval);
458 # }}}
459 } # usage()
461 sub msg {
462 # Print a status message to stderr based on verbosity level {{{
463 my ($verbose_level, $Txt) = @_;
465 if ($Opt{'verbose'} >= $verbose_level) {
466 print(STDERR "$progname: $Txt\n");
468 # }}}
469 } # msg()
471 __END__
473 # Plain Old Documentation (POD) {{{
475 =pod
477 =head1 NAME
479 run-tests.pl
481 =head1 SYNOPSIS
483 ./gpst-file.t [options] [file [files [...]]]
485 =head1 DESCRIPTION
487 Contains tests for the gpst-file(1) program.
489 =head1 OPTIONS
491 =over 4
493 =item B<-a>, B<--all>
495 Run all tests, also TODOs.
497 =item B<-h>, B<--help>
499 Print a brief help summary.
501 =item B<-t>, B<--todo>
503 Run only the TODO tests.
505 =item B<-v>, B<--verbose>
507 Increase level of verbosity. Can be repeated.
509 =item B<--version>
511 Print version information.
513 =item B<--debug>
515 Print debugging messages.
517 =back
519 =head1 AUTHOR
521 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
523 =head1 COPYRIGHT
525 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
526 This is free software; see the file F<COPYING> for legalese stuff.
528 =head1 LICENCE
530 This program is free software: you can redistribute it and/or modify it
531 under the terms of the GNU General Public License as published by the
532 Free Software Foundation, either version 3 of the License, or (at your
533 option) any later version.
535 This program is distributed in the hope that it will be useful, but
536 WITHOUT ANY WARRANTY; without even the implied warranty of
537 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
538 See the GNU General Public License for more details.
540 You should have received a copy of the GNU General Public License along
541 with this program.
542 If not, see L<http://www.gnu.org/licenses/>.
544 =head1 SEE ALSO
546 =cut
548 # }}}
550 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :