Move gpst-pic.t to parent directory
[gpstools.git] / tests / gpst-pic.t
blob7f55fcdc0c34e4880bde55531152aa68a172e474
1 #!/usr/bin/env perl
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 use strict;
15 use warnings;
17 BEGIN {
18 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
19 use Test::More qw{no_plan};
20 use_ok('GPST');
21 use_ok('GPSTxml');
24 use Getopt::Long;
26 $| = 1;
28 our $Debug = 0;
29 our $CMD = "../gpst-pic";
31 our %Opt = (
33 'all' => 0,
34 'debug' => 0,
35 'help' => 0,
36 'todo' => 0,
37 'verbose' => 0,
38 'version' => 0,
42 our $progname = $0;
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = "0.00";
46 Getopt::Long::Configure("bundling");
47 GetOptions(
49 "all|a" => \$Opt{'all'},
50 "debug" => \$Opt{'debug'},
51 "help|h" => \$Opt{'help'},
52 "todo|t" => \$Opt{'todo'},
53 "verbose|v+" => \$Opt{'verbose'},
54 "version" => \$Opt{'version'},
56 ) || die("$progname: Option error. Use -h for help.\n");
58 $Opt{'debug'} && ($Debug = 1);
59 $Opt{'help'} && usage(0);
60 if ($Opt{'version'}) {
61 print_version();
62 exit(0);
65 diag(sprintf("========== Executing %s v%s ==========",
66 $progname,
67 $VERSION));
69 if ($Opt{'todo'} && !$Opt{'all'}) {
70 goto todo_section;
73 =pod
75 testcmd("$CMD command", # {{{
76 <<END,
77 [expected stdin]
78 END
79 "",
80 "description",
83 # }}}
85 =cut
87 my $top = 'gpst-pic-files';
88 diag("Checking dependencies...");
89 likecmd("exifprobe -V", # {{{
90 "/Program: 'exifprobe' version [234]/",
91 '/^$/',
92 "Check that exifprobe(1) is installed",
95 # }}}
96 diag("Testing --author option...");
97 testcmd("$CMD -a sunny $top/DSC_4426.JPG", # {{{
98 <<END,
99 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\tsunny
102 "Read date from DSC_4426.JPG and set --author",
105 # }}}
106 testcmd("$CMD -a sunny -o xml $top/DSC_4426.JPG", # {{{
107 <<END,
108 <?xml version="1.0" encoding="UTF-8"?>
109 <gpstpic>
110 <img>
111 <filename>DSC_4426.JPG</filename>
112 <date>2008-09-18T17:02:27</date>
113 <author>sunny</author>
114 </img>
115 </gpstpic>
118 "Author info is included with -o xml",
121 # }}}
122 # diag("Testing --debug option...");
123 diag("Testing --description option...");
124 testcmd("$CMD -d 'Skumle til\\stander i Bergen.' $top/DSC_4426.JPG", # {{{
125 <<END,
126 1\t2008-09-18T17:02:27\t\\N\tSkumle til\\\\stander i Bergen.\tDSC_4426.JPG\t\\N
129 "Read date from DSC_4426.JPG and set --description with backslash",
132 # }}}
133 testcmd("$CMD -d 'Skumle til\\stander &<> i Bergen.' -o xml $top/DSC_4426.JPG", # {{{
134 <<END,
135 <?xml version="1.0" encoding="UTF-8"?>
136 <gpstpic>
137 <img>
138 <filename>DSC_4426.JPG</filename>
139 <date>2008-09-18T17:02:27</date>
140 <desc>Skumle til\\stander &amp;&lt;&gt; i Bergen.</desc>
141 </img>
142 </gpstpic>
145 "--description works witk XML output",
148 # }}}
149 diag("Testing -h (--help) option...");
150 likecmd("$CMD -h", # {{{
151 '/ Show this help\./',
152 '/^$/',
153 "Option -h prints help screen",
156 # }}}
157 diag("Testing --output-format option..."); # {{{
158 # pgtab
159 testcmd("$CMD -o pgtab $top/DSC_4426.JPG", # {{{
160 <<END,
161 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\t\\N
164 "Output pgtab format from DSC_4426.JPG",
167 # }}}
168 # xml
169 testcmd("$CMD -o xml $top/DSC_4426.JPG", # {{{
170 <<END,
171 <?xml version="1.0" encoding="UTF-8"?>
172 <gpstpic>
173 <img>
174 <filename>DSC_4426.JPG</filename>
175 <date>2008-09-18T17:02:27</date>
176 </img>
177 </gpstpic>
180 "Output XML information for DSC_4426.JPG",
183 # }}}
184 # Unknown format
185 testcmd("$CMD -o blurfl $top/DSC_4426.JPG", # {{{
187 "gpst-pic: blurfl: Unknown output format\n",
188 "Unknown output format specified",
191 # }}}
192 # }}} --output-format
193 diag("Testing -w (--strip-whitespace) option...");
194 testcmd("$CMD -w -o xml $top/DSC_4426.JPG", # {{{
195 <<END,
196 <?xml version="1.0" encoding="UTF-8"?>
197 <gpstpic>
198 <img>
199 <filename>DSC_4426.JPG</filename>
200 <date>2008-09-18T17:02:27</date>
201 </img>
202 </gpstpic>
205 "Strip whitespace from XML",
208 # }}}
209 diag("Testing -T (--timezone) option...");
210 testcmd("$CMD --timezone +1234 $top/DSC_4426.JPG", # {{{
211 <<END,
212 1\t2008-09-18T17:02:27+1234\t\\N\t\\N\tDSC_4426.JPG\t\\N
215 "--timezone works",
218 # }}}
219 testcmd("$CMD -T +0200 $top/DSC_4426.JPG", # {{{
220 <<END,
221 1\t2008-09-18T17:02:27+0200\t\\N\t\\N\tDSC_4426.JPG\t\\N
224 "Positive time zone",
227 # }}}
228 testcmd("$CMD -T-0600 $top/DSC_4426.JPG", # {{{
229 <<END,
230 1\t2008-09-18T17:02:27-0600\t\\N\t\\N\tDSC_4426.JPG\t\\N
233 "Negative time zone",
236 # }}}
237 testcmd("$CMD -T CET $top/DSC_4426.JPG", # {{{
238 <<END,
239 1\t2008-09-18T17:02:27 CET\t\\N\t\\N\tDSC_4426.JPG\t\\N
242 "Time zone abbreviation",
245 # }}}
246 testcmd("$CMD -T cet $top/DSC_4426.JPG", # {{{
247 <<END,
248 1\t2008-09-18T17:02:27 CET\t\\N\t\\N\tDSC_4426.JPG\t\\N
251 "Time zone is converted to upper case",
254 # }}}
255 testcmd("$CMD -T Z $top/DSC_4426.JPG", # {{{
256 <<END,
257 1\t2008-09-18T17:02:27Z\t\\N\t\\N\tDSC_4426.JPG\t\\N
260 "Zulu abbreviation",
263 # }}}
264 testcmd("$CMD -T erf324 $top/DSC_4426.JPG", # {{{
266 "gpst-pic: erf324: Invalid time zone\n",
267 "Invalid time zone abbr, contains digits",
270 # }}}
271 testcmd("$CMD -T CET -o xml $top/DSC_4426.JPG", # {{{
272 <<END,
273 <?xml version="1.0" encoding="UTF-8"?>
274 <gpstpic>
275 <img>
276 <filename>DSC_4426.JPG</filename>
277 <date>2008-09-18T17:02:27 CET</date>
278 </img>
279 </gpstpic>
282 "Time zone abbr. works with -o xml",
285 # }}}
286 testcmd("$CMD -T Z -o xml $top/DSC_4426.JPG", # {{{
287 <<END,
288 <?xml version="1.0" encoding="UTF-8"?>
289 <gpstpic>
290 <img>
291 <filename>DSC_4426.JPG</filename>
292 <date>2008-09-18T17:02:27Z</date>
293 </img>
294 </gpstpic>
297 "Zulu time zone works with -o xml",
300 # }}}
301 diag("Testing -v (--verbose) option...");
302 likecmd("$CMD -hv", # {{{
303 '/^\n\S+ v\d\.\d\d\n/s',
304 '/^$/',
305 "Option --version with -h returns version number and help screen",
308 # }}}
309 diag("Testing --version option...");
310 likecmd("$CMD --version", # {{{
311 '/^\S+ v\d\.\d\d\n/',
312 '/^$/',
313 "Option --version returns version number",
316 # }}}
317 diag("Various...");
318 testcmd("$CMD $top/DSC_4426.JPG", # {{{
319 <<END,
320 1\t2008-09-18T17:02:27\t\\N\t\\N\tDSC_4426.JPG\t\\N
323 "Read date from DSC_4426.JPG, no options",
326 # }}}
328 todo_section:
331 if ($Opt{'all'} || $Opt{'todo'}) {
332 diag("Running TODO tests..."); # {{{
334 TODO: {
336 local $TODO = "";
337 testcmd("$CMD -o extxml files/DSC_4426.JPG", # {{{
338 <<END,
339 <?xml version="1.0" encoding="UTF-8"?>
340 <gpstpic>
341 <img>
342 <filename>DSC_4426.JPG</filename>
343 <crc type="sha1">267e841cb6788c795541e36aea70e2a55d8ec3bb</crc>
344 <crc type="md5">19eb5c86f6b3662b57bc94c3ea428372</crc>
345 <date type="DateTimeOriginal">2008-03-02T17:51:39Z</date>
346 <date type="DateTimeDigitized">2008-03-02T17:51:39Z</date>
347 <date type="DateTime">2008-09-18T15:02:27Z</date>
348 <exposure>
349 <iso>200</iso>
350 <speed>0.333333</speed>
351 <fnumber>3.5 APEX</fnumber>
352 <flash>0</flash>
353 </exposure>
354 <camera>
355 <make>NIKON CORPRORATION</make>
356 <model>NIKON D300</model>
357 <shuttercount>4610</shuttercount>
358 </camera>
359 </img>
360 </gpstpic>
363 "Show extended XML information for DSC_4426.JPG",
366 # }}}
369 # TODO tests }}}
372 diag("Testing finished.");
374 sub testcmd {
375 # {{{
376 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
377 my $stderr_cmd = "";
378 my $deb_str = $Opt{'debug'} ? " --debug" : "";
379 my $Txt = join("",
380 "\"$Cmd\"",
381 defined($Desc)
382 ? " - $Desc"
383 : ""
385 my $TMP_STDERR = "gpst-pic-stderr.tmp";
387 if (defined($Exp_stderr) && !length($deb_str)) {
388 $stderr_cmd = " 2>$TMP_STDERR";
390 is(`$Cmd$deb_str$stderr_cmd`, $Exp_stdout, $Txt);
391 if (defined($Exp_stderr)) {
392 if (!length($deb_str)) {
393 is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
394 unlink($TMP_STDERR);
396 } else {
397 diag("Warning: stderr not defined for '$Txt'");
399 # }}}
400 } # testcmd()
402 sub likecmd {
403 # {{{
404 my ($Cmd, $Exp_stdout, $Exp_stderr, $Desc) = @_;
405 my $stderr_cmd = "";
406 my $deb_str = $Opt{'debug'} ? " --debug" : "";
407 my $Txt = join("",
408 "\"$Cmd\"",
409 defined($Desc)
410 ? " - $Desc"
411 : ""
413 my $TMP_STDERR = "gpst-pic-stderr.tmp";
415 if (defined($Exp_stderr) && !length($deb_str)) {
416 $stderr_cmd = " 2>$TMP_STDERR";
418 like(`$Cmd$deb_str$stderr_cmd`, "$Exp_stdout", $Txt);
419 if (defined($Exp_stderr)) {
420 if (!length($deb_str)) {
421 like(file_data($TMP_STDERR), "$Exp_stderr", "$Txt (stderr)");
422 unlink($TMP_STDERR);
424 } else {
425 diag("Warning: stderr not defined for '$Txt'");
427 # }}}
428 } # likecmd()
430 sub file_data {
431 # Return file content as a string {{{
432 my $File = shift;
433 my $Txt;
434 if (open(my $fp, "<", $File)) {
435 $Txt = join("", <$fp>);
436 close($fp);
437 return($Txt);
438 } else {
439 return;
441 # }}}
442 } # file_data()
444 sub print_version {
445 # Print program version {{{
446 print("$progname v$VERSION\n");
447 # }}}
448 } # print_version()
450 sub usage {
451 # Send the help message to stdout {{{
452 my $Retval = shift;
454 if ($Opt{'verbose'}) {
455 print("\n");
456 print_version();
458 print(<<END);
460 Usage: $progname [options] [file [files [...]]]
462 Contains tests for the gpst-pic(1) program.
464 Options:
466 -a, --all
467 Run all tests, also TODOs.
468 -h, --help
469 Show this help.
470 -t, --todo
471 Run only the TODO tests.
472 -v, --verbose
473 Increase level of verbosity. Can be repeated.
474 --version
475 Print version information.
476 --debug
477 Print debugging messages.
480 exit($Retval);
481 # }}}
482 } # usage()
484 sub msg {
485 # Print a status message to stderr based on verbosity level {{{
486 my ($verbose_level, $Txt) = @_;
488 if ($Opt{'verbose'} >= $verbose_level) {
489 print(STDERR "$progname: $Txt\n");
491 # }}}
492 } # msg()
494 __END__
496 # Plain Old Documentation (POD) {{{
498 =pod
500 =head1 NAME
502 run-tests.pl
504 =head1 SYNOPSIS
506 gpst-pic.t [options] [file [files [...]]]
508 =head1 DESCRIPTION
510 Contains tests for the gpst-pic(1) program.
512 =head1 OPTIONS
514 =over 4
516 =item B<-a>, B<--all>
518 Run all tests, also TODOs.
520 =item B<-h>, B<--help>
522 Print a brief help summary.
524 =item B<-t>, B<--todo>
526 Run only the TODO tests.
528 =item B<-v>, B<--verbose>
530 Increase level of verbosity. Can be repeated.
532 =item B<--version>
534 Print version information.
536 =item B<--debug>
538 Print debugging messages.
540 =back
542 =head1 AUTHOR
544 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
546 =head1 COPYRIGHT
548 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
549 This is free software; see the file F<COPYING> for legalese stuff.
551 =head1 LICENCE
553 This program is free software: you can redistribute it and/or modify it
554 under the terms of the GNU General Public License as published by the
555 Free Software Foundation, either version 3 of the License, or (at your
556 option) any later version.
558 This program is distributed in the hope that it will be useful, but
559 WITHOUT ANY WARRANTY; without even the implied warranty of
560 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
561 See the GNU General Public License for more details.
563 You should have received a copy of the GNU General Public License along
564 with this program.
565 If not, see L<http://www.gnu.org/licenses/>.
567 =head1 SEE ALSO
569 gpst(1)
571 =cut
573 # }}}
575 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :