1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
use Test::More tests => 62;
BEGIN { require 't/test_setup.pl'; }
my $tphoto = 't/test_photo.jpg';
my $tdata = 't/test_photo.desc';
my $cphoto = 't/test_photo_copy.jpg';
my $ref = '\[REFERENCE\].*-->.*$';
my $trim = sub { join '\n', map { s/^.*\"(.*)\".*$/$1/; $_ }
grep { /0:/ } split '\n', $_[0] };
my $fake = sub { my $s = newsegment('COM',\ "Fake $_[0] segment");
$s->{name} = $_[0]; return $s; };
my ($lines, $image, $image_2, $error, $handle, $buffer, $seg,
$status, @desc, @desc_2, $h1, $h2, $num, $num2, @segs1, @segs2);
#=======================================
diag "Testing [Image::MetaData::JPEG]";
#=======================================
BEGIN { use_ok ($::pkgname) or exit; }
BEGIN { use_ok ($::segname) or exit; }
#########################
ok( -s $tphoto, "Test photo exists" );
#########################
$image = newimage("'Invalid'");
ok( ! $image, 'Fail OK: ' . &$trim($::pkgname->Error()) );
#########################
$image = newimage(undef);
ok( ! $image, 'Fail OK: ' . &$trim($::pkgname->Error()) );
#########################
$image = newimage(\ '');
ok( ! $image, 'Fail OK: ' . &$trim($::pkgname->Error()) );
#########################
$image = newimage($tphoto);
ok( $image, "Plain constructor" );
#########################
isa_ok( $image, $::pkgname );
#########################
open($handle, "<", $tphoto); binmode($handle); # for Windows
read($handle, $buffer, -s $tphoto); close($handle);
$image_2 = newimage(\ $buffer);
ok( $image_2, "Constructor with reference" );
#########################
is_deeply( $image->{segments}, $image_2->{segments}, "Objects coincide" );
#########################
$error = $::pkgname->Error();
is( $error, undef, "Ctor error unset (default)" );
#########################
$image = newimage($tphoto, "COM|SOF");
ok( $image, "Restricted constructor" );
#########################
$image = newimage($tphoto, "COM|SOF", "FASTREADONLY");
ok( $image, "Fast constructor" );
#########################
ok( -e $tdata, "Metadata file exists" );
open(ZZ, $tdata); $lines = my @a = <ZZ>; close(ZZ);
#########################
$image = newimage($tphoto);
@desc = map { s/$ref//; $_ } split /\n/, $image->get_description();
is( @desc, $lines, "Description from file" );
#########################
open(ZZ, $tdata); @desc_2 = map { chomp; s/$ref//; $_ } <ZZ>; close(ZZ);
is_deeply( \@desc, \@desc_2, "Detailed description check");
#########################
open($handle, "<", $tphoto); binmode($handle); # for Windows
read($handle, $buffer, -s $tphoto); close($handle);
$image_2 = newimage(\ $buffer);
@desc_2 = map { s/$ref//; $_ } split /\n/, $image_2->get_description();
is( @desc_2, $lines, "Description from reference" );
#########################
$h1 = shift @desc; $h2 = shift @desc_2;
isnt( $h1, $h2, "Descriptions differing (header)" );
#########################
is_deeply( \@desc, \@desc_2, "The two descriptions are the same" );
#########################
$num = scalar grep { /^\s*\d+B <.*>\s*$/ } @desc;
"dddxx" =~ /dddxx/; # test stupid Perl behaviour with m//
is( scalar $image->get_segments(), $num, "Get all segments (undef string)" );
#########################
"dddxx" =~ /dddxx/; # test stupid Perl behaviour with m//
is( scalar $image->get_segments(""), $num, "Get all segments (empty string)" );
#########################
is( $image->get_segments("^S"), 3, "Segments beginning with S" );
#########################
is_deeply( [$image->get_segments("^S", "INDEXES")], [0, 7, 10],
"Segments through their indexes" );
#########################
is_deeply( [$image->get_dimensions()], [432, 288], "Image dimensions" );
#########################
is( $image->find_new_app_segment_position('APP1'), 4, "New APPx position" );
#########################
ok( $image->save($cphoto), "Exit status of save()" );
unlink $cphoto;
#########################
ok( eval { $image->save(\ ($buffer = "")); }, "Image saved to memory" );
#########################
$image_2 = newimage(\ $buffer);
isa_ok( $image_2, $::pkgname );
#########################
is_deeply( $image->{segments}, $image_2->{segments},
"From-disk and in-memory compare equal" );
#########################
$image = newimage($tphoto, 'COM');
ok( $image->save(\ ($buffer = "")), "Exit status of save() (2)" );
#########################
is_deeply( [$image->get_dimensions()], [0, 0],
"No dimensions without SOF segment" );
#########################
$image = newimage($tphoto, 'APP1$', "FASTREADONLY");
ok( ! $image->save(\ ($buffer = "")), "Do not save incomplete files" );
#########################
is( $image->get_segments(), 1, "Number of APP1 segments");
#########################
is( $image->find_new_app_segment_position('APP1'), 0,
"find_new_app_segment_position not fooled by only 1 segment" );
#########################
$image = newimage($tphoto);
$num = scalar $image->get_segments();
$num2 = scalar $image->get_segments('^(APP\d{1,2}|COM)$');
$image->drop_segments('METADATA');
is( scalar $image->get_segments(), $num - $num2, "All metadata erased" );
#########################
is( scalar $image->get_segments('^(APP\d{1,2}|COM)$'), 0,
"... infact, they are no more there" );
#########################
eval { $image->drop_segments() };
isnt( $@, '', "drop_segments' regex cannot be undefined" );
#########################
eval { $image->drop_segments('') };
isnt( $@, '', "drop_segments' regex cannot be an empty string" );
#########################
$image = newimage($tphoto);
$num = scalar $image->get_segments();
$num2 = scalar $image->get_segments('^COM$');
$image->drop_segments('COM');
is( scalar $image->get_segments(), $num - $num2, "All comments erased" );
#########################
$image = newimage($tphoto);
$num = scalar $image->get_segments();
$num2 = scalar $image->get_segments('^APP\d{1,2}$');
$image->drop_segments('APP\d{1,2}');
is( scalar $image->get_segments(), $num - $num2, "All APP segments erased" );
#########################
@segs1 = $image->get_segments();
eval { $image->insert_segments() };
is( $@, '', "insert_segments without a segment does not fail" );
#########################
@segs2 = $image->get_segments();
is_deeply( \ @segs1, \ @segs2, "... but segments are not changed" );
#########################
$seg = newsegment('COM', \ 'dummy');
eval { $image->insert_segments($seg, 0) };
isnt( $@, '', "... pos=0 fails miserably" );
#########################
eval { $image->insert_segments($seg, scalar $image->get_segments()) };
isnt( $@, '', "... pos=last also" );
#########################
@segs2 = $image->get_segments();
is_deeply( \ @segs1, \ @segs2, "... segments still unchanged" );
#########################
$image->insert_segments($seg, 3);
@segs1 = $image->get_segments();
splice @segs2, 3, 0, $seg;
is_deeply( \ @segs1, \ @segs2, "inserting a segment with pos=3" );
#########################
splice @segs2, $image->find_new_app_segment_position('COM'), 0, $seg;
$image->insert_segments($seg);
@segs1 = $image->get_segments();
is_deeply( \ @segs1, \ @segs2, "... now with automatic positioning" );
#########################
$image->insert_segments([$seg, $seg], 9);
@segs1 = $image->get_segments();
splice @segs2, 9, 0, $seg, $seg;
is_deeply( \ @segs1, \ @segs2, "inserting more than one segment" );
#########################
$image->insert_segments([$seg, $seg], 1, 3);
@segs1 = $image->get_segments();
splice @segs2, 1, 3, $seg, $seg;
is_deeply( \ @segs1, \ @segs2, "overwriting instead of inserting" );
#########################
@{$image->{segments}} = $image->get_segments('^(SOI|EOI)$');
is_deeply( [map { $_->{name} } $image->get_segments()],
['SOI', 'EOI'], "only SOI and EOI left" );
#########################
$image->insert_segments(&$fake('COM')); # SOI, COM, EOI
is_deeply( $image->{segments}[1], &$fake('COM'), "insert with only SOI/EOI" );
#########################
$image->{segments}[1] = &$fake('SOF_0'); # SOI, SOF_0, EOI
is_deeply( $image->{segments}[1], &$fake('SOF_0'), "insertion of a fake SOF" );
#########################
$image->insert_segments(&$fake('COM')); # SOI, COM, SOF_0, EOI
is_deeply( $image->{segments}[1], &$fake('COM'), "insert in [SOI, SOF]" );
#########################
$image->{segments}[1] = &$fake('APP2'); # SOI, APP2, SOF_0, EOI
$image->insert_segments(&$fake('COM')); # SOI, APP2, COM, SOF_0, EOI
is_deeply( $image->{segments}[2], &$fake('COM'), "insert COM after APPx" );
#########################
$image->insert_segments(&$fake('APP0')); # SOI, APP0, APP2, COM, SOF_0, EOI
is_deeply( $image->{segments}[1], &$fake('APP0'), "insert APP0 before APP2" );
#########################
$image->insert_segments(&$fake('APP9')); # SOI,APP0,APP2,APP9,COM,SOF_0,EOI
is_deeply( $image->{segments}[3], &$fake('APP9'), "insert APP9 after APP2" );
#########################
$image->insert_segments(&$fake('APP0')); # SOI,APP0,APP0,APP2,APP9,COM,SOF0,EOI
is_deeply( $image->{segments}[2], &$fake('APP0'), "insert APP0 after APP0" );
#########################
$image->{segments}[1] = &$fake('APP1'); # SOI,APP1,APP0,APP2,APP9,COM,SOF0,EOI
$image->insert_segments(&$fake('APP1')); #SOI,2APP1,APP0,APP2,APP9,COM,SOF0,EOI
is_deeply( $image->{segments}[2], &$fake('APP1'),
"insert APP1 after APP1 with before APP0" );
#########################
$image->drop_segments('COM');
$image->{segments}[2] = &$fake('COM'); # SOI,APP1,COM,APP0,APP2,APP9...
$image->insert_segments(&$fake('COM')); # SOI,APP1,COM,COM,APP0,APP2,APP9...
is_deeply( $image->{segments}[3], &$fake('COM'),
"insert COM after COM among APPx" );
#########################
$image->drop_segments('COM');
$image->insert_segments(&$fake('COM')); # SOI,APP1,APP0,APP2,APP9,COM...
is_deeply( $image->{segments}[5], &$fake('COM'),
"insert COM after all APPx" );
#########################
$image->insert_segments(&$fake('APP7')); # SOI,APP1,APP0,APP2,APP7,APP9,COM...
is_deeply( $image->{segments}[4], &$fake('APP7'), "insert APP7 among APPx" );
#########################
$image->{segments}[1] = &$fake('COM'); # SOI,COM,APP0,APP2,APP7,APP9,COM...
$image->provide_app1_Exif_segment();
$num = ($image->get_segments('^APP1$', 'INDEXES'))[0];
is( $num, 3, "provide_app1_Exif_segment finds its way ..." );
### Local Variables: ***
### mode:perl ***
### End: ***
|