]> git.imager.perl.org - imager.git/commitdiff
improve test coverage of Imager::Fountain
authorTony Cook <tony@develop-help.com>
Mon, 10 Jan 2011 10:31:37 +0000 (21:31 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 10 Jan 2011 10:31:37 +0000 (21:31 +1100)
MANIFEST
lib/Imager/Fountain.pm
t/t61filters.t
testimg/gradbad.ggr [new file with mode: 0644]
testimg/gradbad2.ggr [new file with mode: 0644]

index 1471e16a45b970501971b744ac280f828d83f12a..b8cf2d8e8cd9332413984dc6ef964d68876fc770 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -364,6 +364,8 @@ testimg/comp4.bmp           Compressed 4-bit/pixel BMP
 testimg/comp8.bmp              Compressed 8-bit/pixel BMP
 testimg/filltest.ppm           Test for flood fills
 testimg/gimpgrad               A GIMP gradient file
+testimg/gradbad.ggr            A bad GIMP gradient file (bad seg count)
+testimg/gradbad2.ggr           A bad GIMP gradient file (bad segment)
 testimg/imager.pbm             Test bi-level
 testimg/junk.ppm
 testimg/longid.tga             Test TGA with a long id string
index 10fd0d8421eea5695b1483108664ea707f62aee0..dec912fcf005cdb9d71f16e31accdfc81e5c0930 100644 (file)
@@ -62,7 +62,7 @@ sub read {
     return $class->_load_gimp_gradient($fh, $opts{gimp}, $name_ref);
   }
   else {
-    warn "$class::read: Nothing to do!";
+    warn "${class}::read: Nothing to do!";
     return;
   }
 }
index d3e53df6630c20a2eb382b0e93a01ee4602533d4..2c1f16af2f52d0c2bc54054068d461cab3d61873 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use Imager qw(:handy);
-use Test::More tests => 91;
+use Test::More tests => 113;
 Imager::init_log("testout/t61filters.log", 1);
 use Imager::Test qw(is_image_similar test_image is_image is_color4 is_fcolor4);
 # meant for testing the filters themselves
@@ -139,6 +139,48 @@ test($imbase, { type=>'fountain', xa=>75, ya=>75, xb=>90, yb=>15,
                     segments=>$f3, super_sample=>'grid',
                     ftype=>'radial_square', combine=>'color' },
      'testout/t61_fount_gimp.ppm');
+{ # test new fountain with no parameters
+  my $warn = '';
+  local $SIG{__WARN__} = sub { $warn .= "@_" };
+  my $f4 = Imager::Fountain->read();
+  ok(!$f4, "read with no parameters does nothing");
+  like($warn, qr/Nothing to do!/, "check the warning");
+}
+{ # test with missing file
+  my $warn = '';
+  local $SIG{__WARN__} = sub { $warn .= "@_" };
+  my $f = Imager::Fountain->read(gimp => "no-such-file");
+  ok(!$f, "try to read a fountain defintion that doesn't exist");
+  is($warn, "", "should be no warning");
+  like(Imager->errstr, qr/^Cannot open no-such-file: /, "check message");
+}
+SKIP:
+{
+  my $fh = IO::File->new("testimg/gimpgrad", "r");
+  ok($fh, "opened gradient")
+    or skip "Couldn't open gradient: $!", 1;
+  my $f = Imager::Fountain->read(gimp => $fh);
+  ok($f, "read gradient from file handle");
+}
+{
+  # not a gradient
+  my $f = Imager::Fountain->read(gimp => "t/t61filters.t");
+  ok(!$f, "fail to read non-gradient");
+  is(Imager->errstr, "t/t61filters.t is not a GIMP gradient file",
+     "check error message");
+}
+{ # an invalid gradient file
+  my $f = Imager::Fountain->read(gimp => "testimg/gradbad.ggr");
+  ok(!$f, "fail to read bad gradient (bad seg count)");
+  is(Imager->errstr, "testimg/gradbad.ggr is missing the segment count",
+     "check error message");
+}
+{ # an invalid gradient file
+  my $f = Imager::Fountain->read(gimp => "testimg/gradbad2.ggr");
+  ok(!$f, "fail to read bad gradient (bad segment)");
+  is(Imager->errstr, "Bad segment definition",
+     "check error message");
+}
 test($imbase, { type=>'unsharpmask', stddev=>2.0 },
      'testout/t61_unsharp.ppm');
 test($imbase, {type=>'conv', coef=>[ -1, 3, -1, ], },
@@ -231,6 +273,40 @@ is($name, "test gradient", "check the name matches");
   cmp_ok($im->errstr, '=~', 'No color named', "check error message");
 }
 
+{
+  # test simple gradient creation
+  my @colors = map Imager::Color->new($_), qw/white blue red/;
+  my $s = Imager::Fountain->simple(positions => [ 0, 0.3, 1.0 ],
+                                  colors => \@colors);
+  ok($s, "made simple gradient");
+  my $start = $s->[0];
+  is($start->[0], 0, "check start of first correct");
+  is_color4($start->[3], 255, 255, 255, 255, "check color at start");
+}
+{
+  # simple gradient error modes
+  {
+    my $warn = '';
+    local $SIG{__WARN__} = sub { $warn .= "@_" };
+    my $s = Imager::Fountain->simple();
+    ok(!$s, "no parameters to simple()");
+    like($warn, qr/Nothing to do/);
+  }
+  {
+    my $s = Imager::Fountain->simple(positions => [ 0, 1 ],
+                                    colors => [ NC(0, 0, 0) ]);
+    ok(!$s, "mismatch of positions and colors fails");
+    is(Imager->errstr, "positions and colors must be the same size",
+       "check message");
+  }
+  {
+    my $s = Imager::Fountain->simple(positions => [ 0 ],
+                                    colors => [ NC(0, 0, 0) ]);
+    ok(!$s, "not enough positions");
+    is(Imager->errstr, "not enough segments");
+  }
+}
+
 {
   my $im = Imager->new(xsize=>100, ysize=>100);
   # build the gradient the hard way - linear from black to white,
diff --git a/testimg/gradbad.ggr b/testimg/gradbad.ggr
new file mode 100644 (file)
index 0000000..876d608
--- /dev/null
@@ -0,0 +1,3 @@
+GIMP Gradient
+xxx
+We fail to load this as a gradient
diff --git a/testimg/gradbad2.ggr b/testimg/gradbad2.ggr
new file mode 100644 (file)
index 0000000..d3339e8
--- /dev/null
@@ -0,0 +1,4 @@
+GIMP Gradient
+1
+1 2 3 4 5 6 7 8 9 10
+Another invalid file