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
|
use Test::More tests => 25;
BEGIN { require 't/test_setup.pl'; }
my $tphoto = 't/test_photo.jpg';
my $tdata = 't/test_photo.desc';
my $limit = 2**16 - 3;
my $com2 = 'x' x $limit;
my $com3 = 'x' x ($limit + 1);
my $com4 = "Regular comment";
my ($image, $newim, @list, $num, @savelist, @sl, $buffer);
my $reduce = sub {
@{$_[0]} = map {(length $_ > $limit) ? (substr($_,0,$limit),
substr($_,$limit)) :$_} @{$_[0]};};
#===============================
diag "Testing comment routines";
#===============================
BEGIN { use_ok ($::pkgname) or exit; }
#########################
$image = newimage($tphoto);
is( $image->get_number_of_comments(), $num=1, "Get number of comments" );
#########################
$image->add_comment($com2);
is( $image->get_number_of_comments(), ++$num, "Two comments now" );
#########################
@list = $image->get_comments();
is( $list[1], $com2, "Rereading second comment" );
#########################
$image->add_comment($com3);
is( $image->get_number_of_comments(), ($num+=2), "Comment too long, broken" );
#########################
$image->set_comment(2, $com4);
@list = $image->get_comments();
is( $list[2], $com4, "Setting an existing comment" );
#########################
isnt( $list[2], $com3, "Just to see we really read" );
#########################
$image->set_comment(0, $com3);
is( $image->get_number_of_comments(), ++$num, "Set with long comment" );
#########################
@savelist = $image->get_comments();
is( $savelist[2], $com2, "Second comment now third" );
#########################
$image->set_comment(2, undef);
is( $image->get_number_of_comments(), --$num, "Erase comment with undef set" );
#########################
$image->remove_comment(1);
is( $image->get_number_of_comments(), --$num, "Remove one comment" );
#########################
open DUP_ERR, ">&STDERR"; close STDERR;
$image->remove_comment(-1);
open STDERR, ">&DUP_ERR"; close DUP_ERR;
is( $image->get_number_of_comments(), $num, "Remove out-of-bound" );
#########################
open DUP_ERR, ">&STDERR"; close STDERR;
$image->remove_comment($num);
open STDERR, ">&DUP_ERR"; close DUP_ERR;
is( $image->get_number_of_comments(), $num, "Remove out-of-bound (2)" );
#########################
$image->remove_all_comments();
is( $image->get_number_of_comments(), 0, "Erase all comments" );
#########################
@list = $image->get_comments();
is_deeply( \ @list, [], "No comments as a list" );
#########################
$image->add_comment($_) for @savelist; $num = @savelist;
is( $image->get_number_of_comments(), $num, "Restoring comments" );
#########################
@sl = @savelist;
@sl = ( $sl[0]."-".$sl[2]."-".$sl[4], $sl[1], $sl[3]);
&$reduce(\@sl) for (1..((length $sl[0]) / $limit));
$image->join_comments("-", 0, 2, 4);
@list = $image->get_comments();
is_deeply( \@list, \@sl, "Complex joining" );
#########################
@sl = (join "***", @sl);
&$reduce(\@sl) for (1..((length $sl[0]) / $limit));
$image->join_comments("***");
@list = $image->get_comments();
is_deeply( \@list, \@sl, "Total joining" );
#########################
eval { $image->join_comments("-", 0, 2, -4) };
isnt( $@, '', "Negative index in join_comments catched" );
#########################
eval { $image->join_comments("-", 0, 114, 2) };
isnt( $@, '', "Out-of-bound index in join_comments catched" );
#########################
eval { $image->join_comments("-", undef, 0) };
isnt( $@, '', "Undefined index in join_comments catched" );
#########################
eval { $image->join_comments("-", 'invalid', 2, 4) };
isnt( $@, '', "Invalid index in join_comments catched" );
#########################
$image->remove_all_comments();
$image->add_comment($_) for @savelist; $num = @savelist;
$image->save(\ ($buffer = ""));
$newim = newimage(\ $buffer);
@list = $newim->get_comments();
is_deeply( \@list, \@savelist, "Save and re-read" );
#########################
$image->remove_all_comments();
$image->add_comment("Dummy");
$image->set_comment(0, "");
$image->save(\ ($buffer = ""));
$newim = newimage(\ $buffer);
ok( $newim, "Saving a picture with a null comment" );
#########################
@list = $newim->get_comments();
is_deeply( \@list, [ "" ], "The comment is really null" );
### Local Variables: ***
### mode:perl ***
### End: ***
|