tests/Makefile: Show the output for all tests, don't use Genlog
[gpstools.git] / tests / gpst-file.t
blob82d066eb997e1ab61d3161dfd70144336818a826
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 my $top = 'gpst-file-files';
69 chdir($top) || die("$progname: $top/: Cannot chdir: $!\n");
70 system("tar xzf testfile.tar.gz");
71 chdir("..");
73 if ($Opt{'todo'} && !$Opt{'all'}) {
74 goto todo_section;
77 =pod
79 testcmd("$CMD command", # {{{
80 <<END,
81 [expected stdin]
82 END
83 "",
84 "description",
87 # }}}
89 =cut
91 diag("Testing --author option...");
92 testcmd("$CMD -a sunny $top/testfile/file.txt", # {{{
93 <<END,
94 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\tsunny
95 END
96 "",
97 "--author option works",
100 # }}}
101 testcmd("$CMD -a sunny -o xml $top/testfile/file.txt", # {{{
102 <<END,
103 <?xml version="1.0" encoding="UTF-8"?>
104 <gpstfile>
105 <file>
106 <filename>file.txt</filename>
107 <date>2009-02-23T21:58:01</date>
108 <author>sunny</author>
109 </file>
110 </gpstfile>
113 "Author info is included with -o xml",
116 # }}}
117 # diag("Testing --debug option...");
118 diag("Testing --description option...");
119 testcmd("$CMD -d 'Description stuff \\&<>' $top/testfile/file.txt", # {{{
120 <<END,
121 1\t2009-02-23T21:58:01\t\\N\tDescription stuff \\\\&<>\tfile.txt\t\\N
124 "--description with backslash and stuff",
127 # }}}
128 testcmd("$CMD -o xml -d 'Description stuff \\&<>' $top/testfile/file.txt", # {{{
129 <<END,
130 <?xml version="1.0" encoding="UTF-8"?>
131 <gpstfile>
132 <file>
133 <filename>file.txt</filename>
134 <date>2009-02-23T21:58:01</date>
135 <desc>Description stuff \\&amp;&lt;&gt;</desc>
136 </file>
137 </gpstfile>
140 "--description with backslash and stuff",
143 # }}}
144 diag("Testing -h (--help) option...");
145 likecmd("$CMD -h", # {{{
146 '/ Show this help\./',
147 '/^$/',
148 "Option -h prints help screen",
151 # }}}
152 diag("Testing --output-format option..."); # {{{
153 # pgtab
154 testcmd("$CMD -o pgtab $top/testfile/file.txt", # {{{
155 <<END,
156 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
159 "Output pgtab format from DSC_4426.JPG",
162 # }}}
163 # xml
164 testcmd("$CMD -o xml $top/testfile/file.txt", # {{{
165 <<END,
166 <?xml version="1.0" encoding="UTF-8"?>
167 <gpstfile>
168 <file>
169 <filename>file.txt</filename>
170 <date>2009-02-23T21:58:01</date>
171 </file>
172 </gpstfile>
175 "Output XML information for DSC_4426.JPG",
178 # }}}
179 # Unknown format
180 testcmd("$CMD -o blurfl $top/testfile/file.txt", # {{{
182 "gpst-file: blurfl: Unknown output format\n",
183 "Unknown output format specified",
186 # }}}
187 # }}} --output-format
188 diag("Testing -w (--strip-whitespace) option...");
189 testcmd("$CMD -w -o xml $top/testfile/file.txt", # {{{
190 <<END,
191 <?xml version="1.0" encoding="UTF-8"?>
192 <gpstfile>
193 <file>
194 <filename>file.txt</filename>
195 <date>2009-02-23T21:58:01</date>
196 </file>
197 </gpstfile>
200 "Strip whitespace from XML",
203 # }}}
204 diag("Testing -T (--timezone) option...");
205 testcmd("$CMD --timezone +1234 $top/testfile/file.txt", # {{{
206 <<END,
207 1\t2009-02-23T21:58:01+1234\t\\N\t\\N\tfile.txt\t\\N
210 "--timezone works",
213 # }}}
214 testcmd("$CMD -T +0200 $top/testfile/file.txt", # {{{
215 <<END,
216 1\t2009-02-23T21:58:01+0200\t\\N\t\\N\tfile.txt\t\\N
219 "Positive time zone",
222 # }}}
223 testcmd("$CMD -T-0600 $top/testfile/file.txt", # {{{
224 <<END,
225 1\t2009-02-23T21:58:01-0600\t\\N\t\\N\tfile.txt\t\\N
228 "Negative time zone",
231 # }}}
232 testcmd("$CMD -T CET $top/testfile/file.txt", # {{{
233 <<END,
234 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
237 "Time zone abbreviation",
240 # }}}
241 testcmd("$CMD -T cet $top/testfile/file.txt", # {{{
242 <<END,
243 1\t2009-02-23T21:58:01 CET\t\\N\t\\N\tfile.txt\t\\N
246 "Time zone is converted to upper case",
249 # }}}
250 testcmd("$CMD -T Z $top/testfile/file.txt", # {{{
251 <<END,
252 1\t2009-02-23T21:58:01Z\t\\N\t\\N\tfile.txt\t\\N
255 "Zulu abbreviation",
258 # }}}
259 testcmd("$CMD -T erf324 $top/testfile/file.txt", # {{{
261 "gpst-file: erf324: Invalid time zone\n",
262 "Invalid time zone abbr, contains digits",
265 # }}}
266 testcmd("$CMD -T CET -o xml $top/testfile/file.txt", # {{{
267 <<END,
268 <?xml version="1.0" encoding="UTF-8"?>
269 <gpstfile>
270 <file>
271 <filename>file.txt</filename>
272 <date>2009-02-23T21:58:01 CET</date>
273 </file>
274 </gpstfile>
277 "Time zone abbr. works with -o xml",
280 # }}}
281 testcmd("$CMD -T Z -o xml $top/testfile/file.txt", # {{{
282 <<END,
283 <?xml version="1.0" encoding="UTF-8"?>
284 <gpstfile>
285 <file>
286 <filename>file.txt</filename>
287 <date>2009-02-23T21:58:01Z</date>
288 </file>
289 </gpstfile>
292 "Zulu time zone works with -o xml",
295 # }}}
296 diag("Testing -v (--verbose) option...");
297 likecmd("$CMD -hv", # {{{
298 '/^\n\S+ v\d\.\d\d\n/s',
299 '/^$/',
300 "Option --version with -h returns version number and help screen",
303 # }}}
304 diag("Testing --version option...");
305 likecmd("$CMD --version", # {{{
306 '/^\S+ v\d\.\d\d\n/',
307 '/^$/',
308 "Option --version returns version number",
311 # }}}
312 diag("Various...");
313 testcmd("$CMD $top/testfile/file.txt", # {{{
314 <<END,
315 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
318 "No options",
321 # }}}
322 testcmd("echo $top/testfile/file.txt | $CMD", # {{{
323 <<END,
324 1\t2009-02-23T21:58:01\t\\N\t\\N\tfile.txt\t\\N
327 "Read filename from stdin",
330 # }}}
332 todo_section:
335 if ($Opt{'all'} || $Opt{'todo'}) {
336 diag("Running TODO tests..."); # {{{
338 TODO: {
340 local $TODO = "";
341 # Insert TODO tests here.
344 # TODO tests }}}
347 unlink("$top/testfile/file.txt") || warn("$progname: $top/testfile/file.txt: Cannot remove file: $!\n");
348 rmdir("$top/testfile") || warn("$progname: $top/testfile: Cannot remove directory: $!\n");
350 diag("Testing finished.");
352 sub testcmd {
353 # {{{
354 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
355 my $stderr_cmd = "";
356 my $deb_str = $Opt{'debug'} ? " --debug" : "";
357 my $Txt = join("",
358 "\"$Cmd\"",
359 defined($Desc)
360 ? " - $Desc"
361 : ""
363 my $TMP_STDERR = "gpst-file-stderr.tmp";
365 if (defined($Exp_stderr) && !length($deb_str)) {
366 $stderr_cmd = " 2>$TMP_STDERR";
368 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
369 if (defined($Exp_stderr)) {
370 if (!length($deb_str)) {
371 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
372 unlink($TMP_STDERR);
374 } else {
375 diag("Warning: stderr not defined for '$Txt'");
377 # }}}
378 } # testcmd()
380 sub likecmd {
381 # {{{
382 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
383 my $stderr_cmd = "";
384 my $deb_str = $Opt{'debug'} ? " --debug" : "";
385 my $Txt = join("",
386 "\"$Cmd\"",
387 defined($Desc)
388 ? " - $Desc"
389 : ""
391 my $TMP_STDERR = "gpst-file-stderr.tmp";
393 if (defined($Exp_stderr) && !length($deb_str)) {
394 $stderr_cmd = " 2>$TMP_STDERR";
396 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
397 if (defined($Exp_stderr)) {
398 if (!length($deb_str)) {
399 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
400 unlink($TMP_STDERR);
402 } else {
403 diag("Warning: stderr not defined for '$Txt'");
405 # }}}
406 } # likecmd()
408 sub file_data {
409 # Return file content as a string {{{
410 my $File = shift;
411 my $Txt;
412 if (open(my $fp, "<", $File)) {
413 $Txt = join("", <$fp>);
414 close($fp);
415 return($Txt);
416 } else {
417 return;
419 # }}}
420 } # file_data()
422 sub print_version {
423 # Print program version {{{
424 print("$progname v$VERSION\n");
425 # }}}
426 } # print_version()
428 sub usage {
429 # Send the help message to stdout {{{
430 my $Retval = shift;
432 if ($Opt{'verbose'}) {
433 print("\n");
434 print_version();
436 print(<<END);
438 Usage: $progname [options] [file [files [...]]]
440 Contains tests for the gpst-file(1) program.
442 Options:
444 -a, --all
445 Run all tests, also TODOs.
446 -h, --help
447 Show this help.
448 -t, --todo
449 Run only the TODO tests.
450 -v, --verbose
451 Increase level of verbosity. Can be repeated.
452 --version
453 Print version information.
454 --debug
455 Print debugging messages.
458 exit($Retval);
459 # }}}
460 } # usage()
462 sub msg {
463 # Print a status message to stderr based on verbosity level {{{
464 my ($verbose_level, $Txt) = @_;
466 if ($Opt{'verbose'} >= $verbose_level) {
467 print(STDERR "$progname: $Txt\n");
469 # }}}
470 } # msg()
472 __END__
474 # Plain Old Documentation (POD) {{{
476 =pod
478 =head1 NAME
480 run-tests.pl
482 =head1 SYNOPSIS
484 ./gpst-file.t [options] [file [files [...]]]
486 =head1 DESCRIPTION
488 Contains tests for the gpst-file(1) program.
490 =head1 OPTIONS
492 =over 4
494 =item B<-a>, B<--all>
496 Run all tests, also TODOs.
498 =item B<-h>, B<--help>
500 Print a brief help summary.
502 =item B<-t>, B<--todo>
504 Run only the TODO tests.
506 =item B<-v>, B<--verbose>
508 Increase level of verbosity. Can be repeated.
510 =item B<--version>
512 Print version information.
514 =item B<--debug>
516 Print debugging messages.
518 =back
520 =head1 AUTHOR
522 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
524 =head1 COPYRIGHT
526 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
527 This is free software; see the file F<COPYING> for legalese stuff.
529 =head1 LICENCE
531 This program is free software: you can redistribute it and/or modify it
532 under the terms of the GNU General Public License as published by the
533 Free Software Foundation, either version 3 of the License, or (at your
534 option) any later version.
536 This program is distributed in the hope that it will be useful, but
537 WITHOUT ANY WARRANTY; without even the implied warranty of
538 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
539 See the GNU General Public License for more details.
541 You should have received a copy of the GNU General Public License along
542 with this program.
543 If not, see L<http://www.gnu.org/licenses/>.
545 =head1 SEE ALSO
547 =cut
549 # }}}
551 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :