]> git.imager.perl.org - imager-graph.git/blobdiff - t/t10pie.t
fix data, labels, style draw() option handling to not set state in the
[imager-graph.git] / t / t10pie.t
index e8493c9c16d0b61e4a77c596aa837e0f5e7a5457..f57732b9c56c49b98ebccdf78b98adf55dc3851d 100644 (file)
@@ -21,7 +21,7 @@ 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);
 
-plan tests => 16;
+plan tests => 35;
 
 my $pie = Imager::Graph::Pie->new;
 ok($pie, "creating pie chart object");
@@ -115,6 +115,12 @@ cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
   # zero sized segments were drawn to cover the whole circle
   my @data = ( 10, 8, 5, 0.000 );
   my @labels = qw(alpha beta gamma);
+  my @warned;
+  local $SIG{__WARN__} = 
+    sub { 
+      print STDERR $_[0];
+      push @warned, $_[0]
+    };
   
   my $img = $pie->draw
     (
@@ -127,6 +133,99 @@ cmpimg($img6, "testimg/t10_hlegend.png", 550_000);
   ok($img->write(file => 'testout/t10_noother.ppm'),
      "save it");
   cmpimg($img, 'testimg/t10_noother.png', 500_000);
+  unless (is(@warned, 0, "should be no warnings")) {
+    diag($_) for @warned;
+  }
+}
+
+{ # RT #535
+  # no font parameter would crash
+  my $im = $pie->draw
+    (
+     data => \@data,
+     title => 'test',
+    );
+  ok(!$im, "should fail to produce titled graph with no font");
+  like($pie->error, qr/title\.font/, "message should mention which font");
+
+  $im = $pie->draw
+    (
+     labels => \@labels,
+     data => \@data,
+     features => [ 'legend' ],
+    );
+  ok(!$im, "should fail to produce legended graph with no font");
+  like($pie->error, qr/legend\.font/, "message should mention which font");
+
+  $im = $pie->draw
+    ( 
+     data => \@data,
+     labels => \@labels,
+     features => [ 'legend' ],
+     legend => { orientation => "horizontal" },
+    );
+  ok(!$im, "should fail to produce horizontal legended graph with no font");
+  like($pie->error, qr/legend\.font/, "message should mention which font");
+
+  $im = $pie->draw
+    (
+     data => \@data,
+     labels => \@labels,
+    );
+  ok(!$im, "should fail to produce labelled graph with no font");
+  like($pie->error, qr/label\.font/, "message should mention which font");
+
+  $im = $pie->draw
+    (
+     data => \@data,
+     labels => \@labels,
+     features => [ 'allcallouts' ],
+     label => { font => $font },
+    );
+  ok(!$im, "should fail to produce callout labelled graph with no font");
+  like($pie->error, qr/callout\.font/, "message should mention which font");
+
+  # shouldn't need to set label font if doing all callouts
+  $im = $pie->draw
+    (
+     data => \@data,
+     labels => \@labels,
+     features => [ 'allcallouts' ],
+     callout => { font => $font },
+    );
+  ok($im, "should produce callout labelled graph with only callout font")
+    or print "# ", $pie->error, "\n";
+
+  # shouldn't need to set callout font if doing all labels
+  $im = $pie->draw
+    (
+     data => [ 1, 1, 1 ],
+     labels => [ qw/a b c/ ],
+     label => { font => $font }
+    );
+  ok($im, "should produce label only graph with only label font");
+}
+
+{
+  # draw with an empty data array is bad
+  # problem reported and fixed by Patrick Michaud
+  my $im = $pie->draw(data => []);
+  ok(!$im, "fail to draw with empty data");
+  like($pie->error, qr/No values/, "message appropriate");
+}
+
+{ # pie charts can't represent negative values
+  # problem reported and fixed by Patrick Michaud
+  my $im = $pie->draw(data => [ 10, -1, 10 ]);
+  ok(!$im, "fail to draw with negative value");
+  is($pie->error, "Data index 1 is less than zero", "check message");
+}
+
+{ # pie can't represent all zeros
+  # problem reported and fixed by Patrick Michaud
+  my $im = $pie->draw(data => [ 0, 0, 0 ]);
+  ok(!$im, "fail to draw with all zero values");
+  is($pie->error, "Sum of all data values is zero", "check message");
 }
 
 sub cmpimg {