blib/lib/Test/Image/GD.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1  
2 package Test::Image::GD;
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Test::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use GD ':cmp';
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 require Exporter;
12  
13 our $VERSION = '0.03';
14 our @ISA = ('Exporter');
15 our @EXPORT = qw(
16 cmp_image
17 size_ok
18 height_ok
19 width_ok
20 );
21  
22 my $Test = Test::Builder->new;
23  
24 sub cmp_image ($$;$) {
25 my ($got, $expected, $message) = @_;
26 _coerce_image($got);
27 _coerce_image($expected);
28
29 if ($got->compare($expected) & GD_CMP_IMAGE) {
30 $Test->ok(0, $message);
31 }
32 else {
33 $Test->ok(1, $message);
34 }
35 }
36  
37 sub size_ok ($$;$) {
38 my ($got, $expected, $message) = @_;
39 _coerce_image($got);
40 (ref($expected) && ref($expected) eq 'ARRAY')
41 || die "expected must be an ARRAY ref";
42  
43 if ($got->width == $expected->[0] &&
44 $got->height == $expected->[1] ){
45 $Test->ok(1, $message);
46 }
47 else {
48 $Test->diag("... (image => (width, height))\n" .
49 " w: (" . $got->width . " => " . $expected->[0] . ")\n" .
50 " h: (" . $got->height . " => " . $expected->[1] . ")");
51 $Test->ok(0, $message);
52 }
53 }
54  
55 sub height_ok ($$;$) {
56 my ($got, $expected, $message) = @_;
57 _coerce_image($got);
58  
59 if ($got->height == $expected){
60 $Test->ok(1, $message);
61 }
62 else {
63 $Test->diag("... (image => (height))\n" .
64 " h: (" . $got->height . " => " . $expected . ")");
65 $Test->ok(0, $message);
66 }
67 }
68  
69 sub width_ok ($$;$) {
70 my ($got, $expected, $message) = @_;
71 _coerce_image($got);
72  
73 if ($got->width == $expected){
74 $Test->ok(1, $message);
75 }
76 else {
77 $Test->diag("... (image => (width))\n" .
78 " w: (" . $got->width . " => " . $expected . ")");
79 $Test->ok(0, $message);
80 }
81 }
82  
83 ## Utility Methods
84  
85 sub _coerce_image {
86 unless (blessed($_[0]) && $_[0]->isa('GD::Image')) {
87 $_[0] = GD::Image->new($_[0])
88 || die "Could not create GD::Image instance with : " . $_[0];
89 }
90 }
91  
92 1;
93  
94 __END__
↑ Summary