mergesvn at everything under src/gpstools/.
[gpstools.git] / tests / gpst-pic / gpst-pic.t
blob51a40644dd0226bb1049827ec3c5289fb5ea6edc
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # tests/gpst-pic/gpst-pic.t
5 # File ID: f210dafc-f924-11dd-9315-0001805bf4b1
6 # Test suite for gpst-pic(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 BEGIN {
15 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
16 use Test::More qw{no_plan};
17 use_ok(GPST);
18 use_ok(GPSTxml);
21 use strict;
22 use Getopt::Long;
24 $| = 1;
26 our $Debug = 0;
27 our $CMD = "../../gpst-pic";
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 if ($Opt{'todo'} && !$Opt{'all'}) {
68 goto todo_section;
71 =pod
73 testcmd("$CMD command", # {{{
74 <<END,
75 [expected stdin]
76 END
77 "",
78 "description",
81 # }}}
83 =cut
85 diag("Checking dependencies...");
86 likecmd("exifprobe -V", # {{{
87 "/Program: 'exifprobe' version [234]/",
88 '/^$/',
89 "Check that exifprobe(1) is installed",
92 # }}}
93 diag("Testing --author option...");
94 testcmd("$CMD -a sunny files/DSC_4426.JPG", # {{{
95 <<END,
96 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\tsunny
97 END
98 "",
99 "Read date from DSC_4426.JPG and set --author",
102 # }}}
103 testcmd("$CMD -a sunny -o xml files/DSC_4426.JPG", # {{{
104 <<END,
105 <?xml version="1.0" encoding="UTF-8"?>
106 <gpstpic>
107 <img>
108 <filename>DSC_4426.JPG</filename>
109 <date>2008-09-18T17:02:27</date>
110 <author>sunny</author>
111 </img>
112 </gpstpic>
115 "Author info is included with -o xml",
118 # }}}
119 # diag("Testing --debug option...");
120 diag("Testing --description option...");
121 testcmd("$CMD -d 'Skumle til\\stander i Bergen.' files/DSC_4426.JPG", # {{{
122 <<END,
123 1\t2008-09-18T17:02:27\t\\N\tSkumle til\\\\stander i Bergen.\tDSC_4426.JPG\t\\N
126 "Read date from DSC_4426.JPG and set --description with backslash",
129 # }}}
130 testcmd("$CMD -d 'Skumle til\\stander &<> i Bergen.' -o xml files/DSC_4426.JPG", # {{{
131 <<END,
132 <?xml version="1.0" encoding="UTF-8"?>
133 <gpstpic>
134 <img>
135 <filename>DSC_4426.JPG</filename>
136 <date>2008-09-18T17:02:27</date>
137 <desc>Skumle til\\stander &amp;&lt;&gt; i Bergen.</desc>
138 </img>
139 </gpstpic>
142 "--description works witk XML output",
145 # }}}
146 diag("Testing -h (--help) option...");
147 likecmd("$CMD -h", # {{{
148 '/ Show this help\./',
149 '/^$/',
150 "Option -h prints help screen",
153 # }}}
154 ok(`$CMD -h` !~ /^\n\S+ v\d\.\d\d\n/s, "\"$CMD -h\" - No Id with only -h");
155 diag("Testing --output-format option..."); # {{{
156 # pgtab
157 testcmd("$CMD -o pgtab files/DSC_4426.JPG", # {{{
158 <<END,
159 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\t\\N
162 "Output pgtab format from DSC_4426.JPG",
165 # }}}
166 # xml
167 testcmd("$CMD -o xml files/DSC_4426.JPG", # {{{
168 <<END,
169 <?xml version="1.0" encoding="UTF-8"?>
170 <gpstpic>
171 <img>
172 <filename>DSC_4426.JPG</filename>
173 <date>2008-09-18T17:02:27</date>
174 </img>
175 </gpstpic>
178 "Output XML information for DSC_4426.JPG",
181 # }}}
182 # Unknown format
183 testcmd("$CMD -o blurfl files/DSC_4426.JPG", # {{{
185 "gpst-pic: blurfl: Unknown output format\n",
186 "Unknown output format specified",
189 # }}}
190 # }}} --output-format
191 diag("Testing -w (--strip-whitespace) option...");
192 testcmd("$CMD -w -o xml files/DSC_4426.JPG", # {{{
193 <<END,
194 <?xml version="1.0" encoding="UTF-8"?>
195 <gpstpic>
196 <img>
197 <filename>DSC_4426.JPG</filename>
198 <date>2008-09-18T17:02:27</date>
199 </img>
200 </gpstpic>
203 "Strip whitespace from XML",
206 # }}}
207 diag("Testing -T (--timezone) option...");
208 testcmd("$CMD --timezone +1234 files/DSC_4426.JPG", # {{{
209 <<END,
210 1\t2008-09-18T17:02:27+1234\t\\N\t\\N\tDSC_4426.JPG\t\\N
213 "--timezone works",
216 # }}}
217 testcmd("$CMD -T +0200 files/DSC_4426.JPG", # {{{
218 <<END,
219 1\t2008-09-18T17:02:27+0200\t\\N\t\\N\tDSC_4426.JPG\t\\N
222 "Positive time zone",
225 # }}}
226 testcmd("$CMD -T-0600 files/DSC_4426.JPG", # {{{
227 <<END,
228 1\t2008-09-18T17:02:27-0600\t\\N\t\\N\tDSC_4426.JPG\t\\N
231 "Negative time zone",
234 # }}}
235 testcmd("$CMD -T CET files/DSC_4426.JPG", # {{{
236 <<END,
237 1\t2008-09-18T17:02:27 CET\t\\N\t\\N\tDSC_4426.JPG\t\\N
240 "Time zone abbreviation",
243 # }}}
244 testcmd("$CMD -T cet files/DSC_4426.JPG", # {{{
245 <<END,
246 1\t2008-09-18T17:02:27 CET\t\\N\t\\N\tDSC_4426.JPG\t\\N
249 "Time zone is converted to upper case",
252 # }}}
253 testcmd("$CMD -T Z files/DSC_4426.JPG", # {{{
254 <<END,
255 1\t2008-09-18T17:02:27Z\t\\N\t\\N\tDSC_4426.JPG\t\\N
258 "Zulu abbreviation",
261 # }}}
262 testcmd("$CMD -T erf324 files/DSC_4426.JPG", # {{{
264 "gpst-pic: erf324: Invalid time zone\n",
265 "Invalid time zone abbr, contains digits",
268 # }}}
269 testcmd("$CMD -T CET -o xml files/DSC_4426.JPG", # {{{
270 <<END,
271 <?xml version="1.0" encoding="UTF-8"?>
272 <gpstpic>
273 <img>
274 <filename>DSC_4426.JPG</filename>
275 <date>2008-09-18T17:02:27 CET</date>
276 </img>
277 </gpstpic>
280 "Time zone abbr. works with -o xml",
283 # }}}
284 testcmd("$CMD -T Z -o xml files/DSC_4426.JPG", # {{{
285 <<END,
286 <?xml version="1.0" encoding="UTF-8"?>
287 <gpstpic>
288 <img>
289 <filename>DSC_4426.JPG</filename>
290 <date>2008-09-18T17:02:27Z</date>
291 </img>
292 </gpstpic>
295 "Zulu time zone works with -o xml",
298 # }}}
299 diag("Testing -v (--verbose) option...");
300 likecmd("$CMD -hv", # {{{
301 '/^\n\S+ v\d\.\d\d\n/s',
302 '/^$/',
303 "Option --version with -h returns version number and help screen",
306 # }}}
307 diag("Testing --version option...");
308 likecmd("$CMD --version", # {{{
309 '/^\S+ v\d\.\d\d\n/',
310 '/^$/',
311 "Option --version returns version number",
314 # }}}
315 diag("Various...");
316 testcmd("$CMD files/DSC_4426.JPG", # {{{
317 <<END,
318 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\t\\N
321 "Read date from DSC_4426.JPG, no options",
324 # }}}
326 todo_section:
329 if ($Opt{'all'} || $Opt{'todo'}) {
330 diag("Running TODO tests..."); # {{{
332 TODO: {
334 local $TODO = "";
335 testcmd("$CMD -o extxml files/DSC_4426.JPG", # {{{
336 <<END,
337 <?xml version="1.0" encoding="UTF-8"?>
338 <gpstpic>
339 <img>
340 <filename>DSC_4426.JPG</filename>
341 <crc type="sha1">267e841cb6788c795541e36aea70e2a55d8ec3bb</crc>
342 <crc type="md5">19eb5c86f6b3662b57bc94c3ea428372</crc>
343 <date type="DateTimeOriginal">2008-03-02T17:51:39Z</date>
344 <date type="DateTimeDigitized">2008-03-02T17:51:39Z</date>
345 <date type="DateTime">2008-09-18T15:02:27Z</date>
346 <exposure>
347 <iso>200</iso>
348 <speed>0.333333</speed>
349 <fnumber>3.5 APEX</fnumber>
350 <flash>0</flash>
351 </exposure>
352 <camera>
353 <make>NIKON CORPRORATION</make>
354 <model>NIKON D300</model>
355 <shuttercount>4610</shuttercount>
356 </camera>
357 </img>
358 </gpstpic>
361 "Show extended XML information for DSC_4426.JPG",
364 # }}}
367 # TODO tests }}}
370 diag("Testing finished.");
372 sub testcmd {
373 # {{{
374 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
375 my $stderr_cmd = "";
376 my $deb_str = $Opt{'debug'} ? " --debug" : "";
377 my $Txt = join("",
378 "\"$Cmd\"",
379 defined($Desc)
380 ? " - $Desc"
381 : ""
383 my $TMP_STDERR = "gpst-pic-stderr.tmp";
385 if (defined($Exp_stderr) && !length($deb_str)) {
386 $stderr_cmd = " 2>$TMP_STDERR";
388 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
389 if (defined($Exp_stderr)) {
390 if (!length($deb_str)) {
391 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
392 unlink($TMP_STDERR);
394 } else {
395 diag("Warning: stderr not defined for '$Txt'");
397 # }}}
398 } # testcmd()
400 sub likecmd {
401 # {{{
402 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
403 my $stderr_cmd = "";
404 my $deb_str = $Opt{'debug'} ? " --debug" : "";
405 my $Txt = join("",
406 "\"$Cmd\"",
407 defined($Desc)
408 ? " - $Desc"
409 : ""
411 my $TMP_STDERR = "gpst-pic-stderr.tmp";
413 if (defined($Exp_stderr) && !length($deb_str)) {
414 $stderr_cmd = " 2>$TMP_STDERR";
416 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
417 if (defined($Exp_stderr)) {
418 if (!length($deb_str)) {
419 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
420 unlink($TMP_STDERR);
422 } else {
423 diag("Warning: stderr not defined for '$Txt'");
425 # }}}
426 } # likecmd()
428 sub file_data {
429 # Return file content as a string {{{
430 my $File = shift;
431 my $Txt;
432 if (open(FP, "<", $File)) {
433 $Txt = join("", <FP>);
434 close(FP);
435 return($Txt);
436 } else {
437 return undef;
439 # }}}
440 } # file_data()
442 sub print_version {
443 # Print program version {{{
444 print("$progname v$VERSION\n");
445 # }}}
446 } # print_version()
448 sub usage {
449 # Send the help message to stdout {{{
450 my $Retval = shift;
452 if ($Opt{'verbose'}) {
453 print("\n");
454 print_version();
456 print(<<END);
458 Usage: $progname [options] [file [files [...]]]
460 Contains tests for the gpst-pic(1) program.
462 Options:
464 -a, --all
465 Run all tests, also TODOs.
466 -h, --help
467 Show this help.
468 -t, --todo
469 Run only the TODO tests.
470 -v, --verbose
471 Increase level of verbosity. Can be repeated.
472 --version
473 Print version information.
474 --debug
475 Print debugging messages.
478 exit($Retval);
479 # }}}
480 } # usage()
482 sub msg {
483 # Print a status message to stderr based on verbosity level {{{
484 my ($verbose_level, $Txt) = @_;
486 if ($Opt{'verbose'} >= $verbose_level) {
487 print(STDERR "$progname: $Txt\n");
489 # }}}
490 } # msg()
492 __END__
494 # Plain Old Documentation (POD) {{{
496 =pod
498 =head1 NAME
500 run-tests.pl
502 =head1 SYNOPSIS
504 gpst-pic.t [options] [file [files [...]]]
506 =head1 DESCRIPTION
508 Contains tests for the gpst-pic(1) program.
510 =head1 OPTIONS
512 =over 4
514 =item B<-a>, B<--all>
516 Run all tests, also TODOs.
518 =item B<-h>, B<--help>
520 Print a brief help summary.
522 =item B<-t>, B<--todo>
524 Run only the TODO tests.
526 =item B<-v>, B<--verbose>
528 Increase level of verbosity. Can be repeated.
530 =item B<--version>
532 Print version information.
534 =item B<--debug>
536 Print debugging messages.
538 =back
540 =head1 AUTHOR
542 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
544 =head1 COPYRIGHT
546 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
547 This is free software; see the file F<COPYING> for legalese stuff.
549 =head1 LICENCE
551 This program is free software: you can redistribute it and/or modify it
552 under the terms of the GNU General Public License as published by the
553 Free Software Foundation, either version 3 of the License, or (at your
554 option) any later version.
556 This program is distributed in the hope that it will be useful, but
557 WITHOUT ANY WARRANTY; without even the implied warranty of
558 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
559 See the GNU General Public License for more details.
561 You should have received a copy of the GNU General Public License along
562 with this program.
563 If not, see L<http://www.gnu.org/licenses/>.
565 =head1 SEE ALSO
567 gpst(1)
569 =cut
571 # }}}
573 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :