Adds two tests for horizontal graphs, and fixes some warnings exposed by them
authorpmichaud <pmichaud@pobox.com>
Thu, 5 Nov 2009 02:02:11 +0000 (02:02 +0000)
committerpmichaud <pmichaud@pobox.com>
Thu, 5 Nov 2009 02:02:11 +0000 (02:02 +0000)
lib/Imager/Graph/Horizontal.pm
t/34horizontal_many_points.t [new file with mode: 0644]
t/t15line_horizontal.t [new file with mode: 0644]
testimg/t34_points.ppm [new file with mode: 0644]

index 2377e06af8f1d2b31912f5d1de4df7b8246a853e..aae51cf384acbc20af1e870d043b266b7f6e721b 100644 (file)
@@ -709,6 +709,14 @@ sub _get_y_tic_width {
   my $self = shift;
 
   my $labels = $self->_get_labels();
+
+  if (!$labels) {
+    return;
+  }
+
+  my %text_info = $self->_text_style('legend')
+    or return;
+
   my $max_width = 0;
   foreach my $label (@$labels) {
     my @box = $self->_text_bbox($label, 'legend');
diff --git a/t/34horizontal_many_points.t b/t/34horizontal_many_points.t
new file mode 100644 (file)
index 0000000..26311e4
--- /dev/null
@@ -0,0 +1,60 @@
+#!perl -w
+use strict;
+use Imager::Graph::Bar;
+use lib 't/lib';
+use Imager::Font::Test;
+use Test::More;
+
+-d 'testout' 
+  or mkdir "testout", 0700 
+  or die "Could not create output directory: $!";
+
+++$|;
+
+my @warned;
+local $SIG{__WARN__} =
+  sub {
+    print STDERR $_[0];
+    push @warned, $_[0]
+  };
+
+
+use Imager qw(:handy);
+
+plan tests => 4;
+
+#my $fontfile = 'ImUgly.ttf';
+#my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
+#  or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
+my $font = Imager::Font::Test->new();
+
+my @data = (1 .. 1000);
+
+my $bar = Imager::Graph::Bar->new();
+ok($bar, "creating bar chart object");
+
+$bar->add_data_series(\@data);
+
+my $img1 = $bar->draw();
+ok($img1, "drawing bar chart");
+
+$img1->write(file=>'testout/t34_points.ppm') or die "Can't save img1: ".$img1->errstr."\n";
+cmpimg($img1, 'testimg/t34_points.ppm', 1);
+
+unless (is(@warned, 0, "should be no warnings")) {
+  diag($_) for @warned;
+}
+
+
+sub cmpimg {
+  my ($img, $file, $limit) = @_;
+
+  $limit ||= 10000;
+
+  my $cmpimg = Imager->new;
+  $cmpimg->read(file=>$file)
+    or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
+  my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
+  cmp_ok($diff, '<', $limit, "Comparison to $file ($diff)");
+}
+
diff --git a/t/t15line_horizontal.t b/t/t15line_horizontal.t
new file mode 100644 (file)
index 0000000..8e0ffe7
--- /dev/null
@@ -0,0 +1,48 @@
+#!perl -w
+use strict;
+use Imager::Graph::Bar;
+use lib 't/lib';
+use Imager::Font::Test;
+use Test::More;
+
+-d 'testout' 
+  or mkdir "testout", 0700 
+  or die "Could not create output directory: $!";
+
+++$|;
+
+my @warned;
+local $SIG{__WARN__} =
+  sub {
+    print STDERR $_[0];
+    push @warned, $_[0]
+  };
+
+
+use Imager qw(:handy);
+
+plan tests => 3;
+
+#my $fontfile = 'ImUgly.ttf';
+#my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
+#  or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
+my $font = Imager::Font::Test->new();
+
+my @data = ( 100, 180, 80, 20, 2, 1, 0.5 );
+my @labels = qw(alpha beta gamma delta epsilon phi gi);
+
+my $line = Imager::Graph::Bar->new();
+ok($line, "creating line chart object");
+
+$line->add_line_data_series(\@data);
+$line->set_labels(\@labels);
+
+my $img1 = $line->draw();
+ok($img1, "drawing line chart");
+
+$img1->write(file=>'testout/t15_basic.ppm') or die "Can't save img1: ".$img1->errstr."\n";
+
+unless (is(@warned, 0, "should be no warnings")) {
+  diag($_) for @warned;
+}
+
diff --git a/testimg/t34_points.ppm b/testimg/t34_points.ppm
new file mode 100644 (file)
index 0000000..8478f2c
Binary files /dev/null and b/testimg/t34_points.ppm differ