Move use strict before BEGIN
[gpstools.git] / tests / gpst-file / gpst-file.t
blob3471b89a9a0859b4780233158db677269cb49db7
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 use strict;
16 BEGIN {
17 # push(@INC, "$ENV{'HOME'}/bin/STDlibdirDTS");
18 use Test::More qw{no_plan};
19 # use_ok() goes here
22 use Getopt::Long;
24 $| = 1;
26 our $Debug = 0;
27 our $CMD = "../../gpst-file";
29 our %Opt = (
31 'all' => 0,
32 'debug' => 0,
33 'help' => 0,
34 'todo' => 0,
35 'verbose' => 0,
36 'version' => 0,
40 our $progname = $0;
41 $progname =~ s/^.*\/(.*?)$/$1/;
42 our $VERSION = "0.00";
44 Getopt::Long::Configure("bundling");
45 GetOptions(
47 "all|a" => \$Opt{'all'},
48 "debug" => \$Opt{'debug'},
49 "help|h" => \$Opt{'help'},
50 "todo|t" => \$Opt{'todo'},
51 "verbose|v+" => \$Opt{'verbose'},
52 "version" => \$Opt{'version'},
54 ) || die("$progname: Option error. Use -h for help.\n");
56 $Opt{'debug'} && ($Debug = 1);
57 $Opt{'help'} && usage(0);
58 if ($Opt{'version'}) {
59 print_version();
60 exit(0);
63 diag(sprintf("========== Executing %s v%s ==========",
64 $progname,
65 $VERSION));
67 chdir("files") || die("$progname: files/: Cannot chdir: $!\n");
68 system("tar xzf testfile.tar.gz");
69 chdir("..");
71 if ($Opt{'todo'} && !$Opt{'all'}) {
72 goto todo_section;
75 =pod
77 testcmd("$CMD command", # {{{
78 <<END,
79 [expected stdin]
80 END
81 "",
82 "description",
85 # }}}
87 =cut
89 diag("Testing --author option...");
90 testcmd("$CMD -a sunny files/testfile/file.txt", # {{{
91 <<END,
92 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\tsunny
93 END
94 "",
95 "--author option works",
98 # }}}
99 testcmd("$CMD -a sunny -o xml files/testfile/file.txt", # {{{
100 <<END,
101 <?xml version="1.0" encoding="UTF-8"?>
102 <gpstfile>
103 <file>
104 <filename>file.txt</filename>
105 <date>2009-02-23T21:58:01</date>
106 <author>sunny</author>
107 </file>
108 </gpstfile>
111 "Author info is included with -o xml",
114 # }}}
115 # diag("Testing --debug option...");
116 diag("Testing --description option...");
117 testcmd("$CMD -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
118 <<END,
119 1\t2009-02-23T21:58:01\t\\N\tDescription stuff \\\\&<>\tfile.txt\t\\N
122 "--description with backslash and stuff",
125 # }}}
126 testcmd("$CMD -o xml -d 'Description stuff \\&<>' files/testfile/file.txt", # {{{
127 <<END,
128 <?xml version="1.0" encoding="UTF-8"?>
129 <gpstfile>
130 <file>
131 <filename>file.txt</filename>
132 <date>2009-02-23T21:58:01</date>
133 <desc>Description stuff \\&amp;&lt;&gt;</desc>
134 </file>
135 </gpstfile>
138 "--description with backslash and stuff",
141 # }}}
142 diag("Testing -h (--help) option...");
143 likecmd("$CMD -h", # {{{
144 '/ Show this help\./',
145 '/^$/',
146 "Option -h prints help screen",
149 # }}}
150 diag("Testing --output-format option..."); # {{{
151 # pgtab
152 testcmd("$CMD -o pgtab files/testfile/file.txt", # {{{
153 <<END,
154 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
157 "Output pgtab format from DSC_4426.JPG",
160 # }}}
161 # xml
162 testcmd("$CMD -o xml files/testfile/file.txt", # {{{
163 <<END,
164 <?xml version="1.0" encoding="UTF-8"?>
165 <gpstfile>
166 <file>
167 <filename>file.txt</filename>
168 <date>2009-02-23T21:58:01</date>
169 </file>
170 </gpstfile>
173 "Output XML information for DSC_4426.JPG",
176 # }}}
177 # Unknown format
178 testcmd("$CMD -o blurfl files/testfile/file.txt", # {{{
180 "gpst-file: blurfl: Unknown output format\n",
181 "Unknown output format specified",
184 # }}}
185 # }}} --output-format
186 diag("Testing -w (--strip-whitespace) option...");
187 testcmd("$CMD -w -o xml files/testfile/file.txt", # {{{
188 <<END,
189 <?xml version="1.0" encoding="UTF-8"?>
190 <gpstfile>
191 <file>
192 <filename>file.txt</filename>
193 <date>2009-02-23T21:58:01</date>
194 </file>
195 </gpstfile>
198 "Strip whitespace from XML",
201 # }}}
202 diag("Testing -T (--timezone) option...");
203 testcmd("$CMD --timezone +1234 files/testfile/file.txt", # {{{
204 <<END,
205 1\t2009-02-23T21:58:01+1234\t\\N\t\\N\tfile.txt\t\\N
208 "--timezone works",
211 # }}}
212 testcmd("$CMD -T +0200 files/testfile/file.txt", # {{{
213 <<END,
214 1\t2009-02-23T21:58:01+0200\t\\N\t\\N\tfile.txt\t\\N
217 "Positive time zone",
220 # }}}
221 testcmd("$CMD -T-0600 files/testfile/file.txt", # {{{
222 <<END,
223 1\t2009-02-23T21:58:01-0600\t\\N\t\\N\tfile.txt\t\\N
226 "Negative time zone",
229 # }}}
230 testcmd("$CMD -T CET files/testfile/file.txt", # {{{
231 <<END,
232 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
235 "Time zone abbreviation",
238 # }}}
239 testcmd("$CMD -T cet files/testfile/file.txt", # {{{
240 <<END,
241 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
244 "Time zone is converted to upper case",
247 # }}}
248 testcmd("$CMD -T Z files/testfile/file.txt", # {{{
249 <<END,
250 1\t2009-02-23T21:58:01Z\t\\N\t\\N\tfile.txt\t\\N
253 "Zulu abbreviation",
256 # }}}
257 testcmd("$CMD -T erf324 files/testfile/file.txt", # {{{
259 "gpst-file: erf324: Invalid time zone\n",
260 "Invalid time zone abbr, contains digits",
263 # }}}
264 testcmd("$CMD -T CET -o xml files/testfile/file.txt", # {{{
265 <<END,
266 <?xml version="1.0" encoding="UTF-8"?>
267 <gpstfile>
268 <file>
269 <filename>file.txt</filename>
270 <date>2009-02-23T21:58:01 CET</date>
271 </file>
272 </gpstfile>
275 "Time zone abbr. works with -o xml",
278 # }}}
279 testcmd("$CMD -T Z -o xml files/testfile/file.txt", # {{{
280 <<END,
281 <?xml version="1.0" encoding="UTF-8"?>
282 <gpstfile>
283 <file>
284 <filename>file.txt</filename>
285 <date>2009-02-23T21:58:01Z</date>
286 </file>
287 </gpstfile>
290 "Zulu time zone works with -o xml",
293 # }}}
294 diag("Testing -v (--verbose) option...");
295 likecmd("$CMD -hv", # {{{
296 '/^\n\S+ v\d\.\d\d\n/s',
297 '/^$/',
298 "Option --version with -h returns version number and help screen",
301 # }}}
302 diag("Testing --version option...");
303 likecmd("$CMD --version", # {{{
304 '/^\S+ v\d\.\d\d\n/',
305 '/^$/',
306 "Option --version returns version number",
309 # }}}
310 diag("Various...");
311 testcmd("$CMD files/testfile/file.txt", # {{{
312 <<END,
313 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
316 "No options",
319 # }}}
320 testcmd("echo files/testfile/file.txt | $CMD", # {{{
321 <<END,
322 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
325 "Read filename from stdin",
328 # }}}
330 todo_section:
333 if ($Opt{'all'} || $Opt{'todo'}) {
334 diag("Running TODO tests..."); # {{{
336 TODO: {
338 local $TODO = "";
339 # Insert TODO tests here.
342 # TODO tests }}}
345 unlink("files/testfile/file.txt") || warn("$progname: files/testfile/file.txt: Cannot remove file: $!\n");
346 rmdir("files/testfile") || warn("$progname: files/testfile: Cannot remove directory: $!\n");
348 diag("Testing finished.");
350 sub testcmd {
351 # {{{
352 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
353 my $stderr_cmd = "";
354 my $deb_str = $Opt{'debug'} ? " --debug" : "";
355 my $Txt = join("",
356 "\"$Cmd\"",
357 defined($Desc)
358 ? " - $Desc"
359 : ""
361 my $TMP_STDERR = "gpst-file-stderr.tmp";
363 if (defined($Exp_stderr) && !length($deb_str)) {
364 $stderr_cmd = " 2>$TMP_STDERR";
366 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
367 if (defined($Exp_stderr)) {
368 if (!length($deb_str)) {
369 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
370 unlink($TMP_STDERR);
372 } else {
373 diag("Warning: stderr not defined for '$Txt'");
375 # }}}
376 } # testcmd()
378 sub likecmd {
379 # {{{
380 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
381 my $stderr_cmd = "";
382 my $deb_str = $Opt{'debug'} ? " --debug" : "";
383 my $Txt = join("",
384 "\"$Cmd\"",
385 defined($Desc)
386 ? " - $Desc"
387 : ""
389 my $TMP_STDERR = "gpst-file-stderr.tmp";
391 if (defined($Exp_stderr) && !length($deb_str)) {
392 $stderr_cmd = " 2>$TMP_STDERR";
394 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
395 if (defined($Exp_stderr)) {
396 if (!length($deb_str)) {
397 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
398 unlink($TMP_STDERR);
400 } else {
401 diag("Warning: stderr not defined for '$Txt'");
403 # }}}
404 } # likecmd()
406 sub file_data {
407 # Return file content as a string {{{
408 my $File = shift;
409 my $Txt;
410 if (open(my $fp, "<", $File)) {
411 $Txt = join("", <$fp>);
412 close($fp);
413 return($Txt);
414 } else {
415 return;
417 # }}}
418 } # file_data()
420 sub print_version {
421 # Print program version {{{
422 print("$progname v$VERSION\n");
423 # }}}
424 } # print_version()
426 sub usage {
427 # Send the help message to stdout {{{
428 my $Retval = shift;
430 if ($Opt{'verbose'}) {
431 print("\n");
432 print_version();
434 print(<<END);
436 Usage: $progname [options] [file [files [...]]]
438 Contains tests for the gpst-file(1) program.
440 Options:
442 -a, --all
443 Run all tests, also TODOs.
444 -h, --help
445 Show this help.
446 -t, --todo
447 Run only the TODO tests.
448 -v, --verbose
449 Increase level of verbosity. Can be repeated.
450 --version
451 Print version information.
452 --debug
453 Print debugging messages.
456 exit($Retval);
457 # }}}
458 } # usage()
460 sub msg {
461 # Print a status message to stderr based on verbosity level {{{
462 my ($verbose_level, $Txt) = @_;
464 if ($Opt{'verbose'} >= $verbose_level) {
465 print(STDERR "$progname: $Txt\n");
467 # }}}
468 } # msg()
470 __END__
472 # Plain Old Documentation (POD) {{{
474 =pod
476 =head1 NAME
478 run-tests.pl
480 =head1 SYNOPSIS
482 ./gpst-file.t [options] [file [files [...]]]
484 =head1 DESCRIPTION
486 Contains tests for the gpst-file(1) program.
488 =head1 OPTIONS
490 =over 4
492 =item B<-a>, B<--all>
494 Run all tests, also TODOs.
496 =item B<-h>, B<--help>
498 Print a brief help summary.
500 =item B<-t>, B<--todo>
502 Run only the TODO tests.
504 =item B<-v>, B<--verbose>
506 Increase level of verbosity. Can be repeated.
508 =item B<--version>
510 Print version information.
512 =item B<--debug>
514 Print debugging messages.
516 =back
518 =head1 AUTHOR
520 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
522 =head1 COPYRIGHT
524 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
525 This is free software; see the file F<COPYING> for legalese stuff.
527 =head1 LICENCE
529 This program is free software: you can redistribute it and/or modify it
530 under the terms of the GNU General Public License as published by the
531 Free Software Foundation, either version 3 of the License, or (at your
532 option) any later version.
534 This program is distributed in the hope that it will be useful, but
535 WITHOUT ANY WARRANTY; without even the implied warranty of
536 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
537 See the GNU General Public License for more details.
539 You should have received a copy of the GNU General Public License along
540 with this program.
541 If not, see L<http://www.gnu.org/licenses/>.
543 =head1 SEE ALSO
545 =cut
547 # }}}
549 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :