loosen up comparisons to handle the rounding change in Imager 0.90
[imager-graph.git] / t / x51hstyle.t
CommitLineData
86f73dc2
TC
1#!perl -w
2use strict;
3use Imager::Graph::Bar;
4use lib 't/lib';
5use Imager::Font::Test;
6use Test::More;
7use Imager::Test qw(is_image_similar is_image);
8
9-d 'testout'
10 or mkdir "testout", 0700
11 or die "Could not create output directory: $!";
12
13++$|;
14
15use Imager qw(:handy);
16
17#my $fontfile = 'ImUgly.ttf';
18#my $font = Imager::Font->new(file=>$fontfile, type => 'ft2', aa=>1)
19# or plan skip_all => "Cannot create font object: ",Imager->errstr,"\n";
20my $font = Imager::Font::Test->new();
21
22my @data1 =
23 (
24 100, 180, 80, 20, 2, 1, 0.5 ,
25 );
26my @labels = qw(alpha beta gamma delta epsilon phi gi);
27
28plan tests => 22;
29
30# this may change output quality too
31print "# Imager version: $Imager::VERSION\n";
32print "# Font type: ",ref $font,"\n";
33
34{
35 my $colm = Imager::Graph::Bar->new;
36 ok($colm, "creating chart object");
37 $colm->set_x_tics(10);
38
39 $colm->add_data_series(\@data1, "Test Bar");
40
41 my $img1;
42 { # default outline of chart area
43 $img1 = $colm->draw
44 (
45 labels => \@labels,
46 font => $font,
47 title => "Test",
48 )
49 or print "# ", $colm->error, "\n";
50
51 ok($img1, "made the image");
52
53 ok($img1->write(file => "testout/x51col_def.ppm"),
54 "save to testout");
55
5a50139d 56 cmpimg($img1, "xtestimg/x51col_def.png", 80_000);
86f73dc2
TC
57 }
58
59 { # no outline
60 my $img2 = $colm->draw
61 (
62 labels => \@labels,
63 font => $font,
64 title => "Test",
65 features => [ qw/nograph_outline/ ],
66 )
67 or print "# ", $colm->error, "\n";
68
69 isnt($img1, $img2, "make sure they're different images");
70
71 ok($img2, "made the image");
72
73 ok($img2->write(file => "testout/x51col_noout.ppm"),
74 "save to testout");
75
5a50139d 76 cmpimg($img2, "xtestimg/x51col_noout.png", 80_000);
86f73dc2
TC
77 }
78
79 {
80 # check no state remembered from nograph_outline
81 my $img5 = $colm->draw
82 (
83 labels => \@labels,
84 font => $font,
85 title => "Test",
86 )
87 or print "# ", $colm->error, "\n";
88 ok($img5, "make with border again to check no state held");
89 is_image($img1, $img5, "check no state held");
90 }
91
92 { # styled outline
93 my $img6 = $colm->draw
94 (
95 labels => \@labels,
96 font => $font,
97 title => "Test styled outline",
98 graph =>
99 {
100 outline =>
101 {
102 color => "#fff",
103 style => "dashed",
104 },
105 },
106 );
107 ok($img6, "make chart with dashed outline of graph area");
108 ok($img6->write(file => "testout/x51col_dashout.ppm"),
109 "save it");
5a50139d 110 cmpimg($img6, "xtestimg/x51col_dashout.png", 80_000);
86f73dc2
TC
111 }
112
113 { # no outline, styled fill
114 my $img7 = $colm->draw
115 (
116 labels => \@labels,
117 font => $font,
118 title => "Test styled outline",
119 features => "nograph_outline",
120 graph =>
121 {
122 fill => { solid => "ffffff80" },
123 },
124 )
125 or print "# ", $colm->error, "\n";
126 ok($img7, "made the image");
127 ok($img7->write(file => "testout/x51col_fill.ppm"),
128 "save it");
5a50139d 129 cmpimg($img7, "xtestimg/x51col_fill.png", 120_000);
86f73dc2
TC
130 }
131
132 { # gridlines
133 my $img8 = $colm->draw
134 (
135 labels => \@labels,
136 font => $font,
137 title => "gridlines",
138 features => "vertical_gridlines",
139 vgrid => { style => "dashed", color => "#A0A0A0" },
140 )
141 or print "# ", $colm->error, "\n";
142 ok($img8, "made the gridline image");
143 ok($img8->write(file => "testout/x51col_grid.ppm"),
144 "save it");
5a50139d 145 cmpimg($img8, "xtestimg/x51col_grid.png", 80_000);
86f73dc2
TC
146 }
147
148 { # gridlines (set by method)
149 my $colm2 = Imager::Graph::Bar->new;
150 $colm2->show_vertical_gridlines();
151 $colm2->set_vertical_gridline_style(style => "dashed", color => "#A0A0A0");
152 $colm2->set_labels(\@labels);
153 $colm2->set_title("gridlines");
154 $colm2->add_data_series(\@data1, "Test Bar");
155 $colm2->set_x_tics(10);
156 $colm2->set_font($font);
157
158 my $img9 = $colm2->draw
159 (
160 #labels => \@labels,
161 #font => $font,
162 #title => "gridlines",
163 #features => "vertical_gridlines",
164 #vgrid => { style => "dashed", color => "#A0A0A0" },
165 )
166 or print "# ", $colm2->error, "\n";
167 ok($img9, "made the gridline image (set by methods)");
168 ok($img9->write(file => "testout/x51col_gridm.ppm"),
169 "save it");
5a50139d 170 cmpimg($img9, "xtestimg/x51col_grid.png", 80_000);
86f73dc2
TC
171 }
172}
173
174END {
175 unless ($ENV{IMAGER_GRAPH_KEEP_FILES}) {
176 unlink "testout/x51col_def.ppm";
177 unlink "testout/x51col_noout.ppm";
178 unlink "testout/x51col_dashout.ppm";
179 unlink "testout/x51col_fill.ppm";
180 unlink "testout/x51col_grid.ppm";
181 unlink "testout/x51col_gridm.ppm";
182 }
183}
184
185sub cmpimg {
186 my ($img, $file, $limit) = @_;
187
188 $limit ||= 10000;
189
190 SKIP:
191 {
192 $Imager::formats{png}
193 or skip("No PNG support", 1);
194
195 my $cmpimg = Imager->new;
196 $cmpimg->read(file=>$file)
197 or return ok(0, "Cannot read $file: ".$cmpimg->errstr);
198 my $diff = Imager::i_img_diff($img->{IMG}, $cmpimg->{IMG});
199 is_image_similar($img, $cmpimg, $limit, "Comparison to $file ($diff)");
200 }
201}