1###############################################################################
2#
3# A test for Spreadsheet::WriteExcel::Chart.
4#
5# Tests for the Excel Chart.pm format conversion methods.
6#
7# reverse('�'), January 2010, John McNamara, jmcnamara@cpan.org
8#
9
10# prove -I../lib --nocolor -v 62_chart_formats.t
11
12use strict;
13
14use Spreadsheet::WriteExcel;
15
16use Test::More tests => 20;
17#use Test::More 'no_plan';
18
19
20###############################################################################
21#
22# Tests setup
23#
24my $test_file = 'temp_test_file.xls';
25my $workbook  = Spreadsheet::WriteExcel->new( $test_file );
26my $chart     = $workbook->add_chart( type => 'column' );
27my %values;
28my $color;
29my $got;
30my $got_index;
31my $got_rgb;
32my $expected;
33my $expected_index;
34my $expected_rgb;
35my $caption;
36my $caption1;
37my $caption2;
38
39
40###############################################################################
41#
42# Test. User defined colour as string.
43#
44$color    = 'red';
45$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
46$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
47
48$expected_index = 0x0A;
49$expected_rgb   = 0x000000FF;
50
51
52( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
53
54is( $got_index, $expected_index, $caption1 );
55is( $got_rgb,   $expected_rgb,   $caption2 );
56
57
58###############################################################################
59#
60# Test. User defined colour as string.
61#
62$color    = 'black';
63$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
64$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
65
66$expected_index = 0x08;
67$expected_rgb   = 0x00000000;
68
69
70( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
71
72is( $got_index, $expected_index, $caption1 );
73is( $got_rgb,   $expected_rgb,   $caption2 );
74
75
76###############################################################################
77#
78# Test. User defined colour as string.
79#
80$color    = 'white';
81$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
82$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
83
84$expected_index = 0x09;
85$expected_rgb   = 0x00FFFFFF;
86
87
88( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
89
90is( $got_index, $expected_index, $caption1 );
91is( $got_rgb,   $expected_rgb,   $caption2 );
92
93
94###############################################################################
95#
96# Test. User defined colour as an index.
97#
98$color    = 0x0A;
99$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
100$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
101
102$expected_index = 0x0A;
103$expected_rgb   = 0x000000FF;
104
105
106( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
107
108is( $got_index, $expected_index, $caption1 );
109is( $got_rgb,   $expected_rgb,   $caption2 );
110
111
112###############################################################################
113#
114# Test. User defined colour as an out of range index.
115#
116$color    = 7;
117$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
118$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
119
120$expected_index = undef;
121$expected_rgb   = undef;
122
123
124( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
125
126is( $got_index, $expected_index, $caption1 );
127is( $got_rgb,   $expected_rgb,   $caption2 );
128
129
130###############################################################################
131#
132# Test. User defined colour as an out of range index.
133#
134$color    = 64;
135$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
136$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
137
138$expected_index = undef;
139$expected_rgb   = undef;
140
141
142( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
143
144is( $got_index, $expected_index, $caption1 );
145is( $got_rgb,   $expected_rgb,   $caption2 );
146
147
148###############################################################################
149#
150# Test. User defined colour as an invalid string.
151#
152$color    = 'plaid';
153$caption1 = " \tChart: \$index   = _get_color_indices( $color )";
154$caption2 = " \tChart: \$rgb     = _get_color_indices( $color )";
155
156$expected_index = undef;
157$expected_rgb   = undef;
158
159
160( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
161
162is( $got_index, $expected_index, $caption1 );
163is( $got_rgb,   $expected_rgb,   $caption2 );
164
165
166###############################################################################
167#
168# Test. User defined colour as an undef property.
169#
170$color    = undef;
171$caption1 = " \tChart: \$index   = _get_color_indices( undef )";
172$caption2 = " \tChart: \$rgb     = _get_color_indices( undef )";
173
174$expected_index = undef;
175$expected_rgb   = undef;
176
177
178( $got_index, $got_rgb ) = $chart->_get_color_indices( $color );
179
180is( $got_index, $expected_index, $caption1 );
181is( $got_rgb,   $expected_rgb,   $caption2 );
182
183
184###############################################################################
185#
186# Test. Line patterns with indices.
187#
188$caption = " \tChart: \$pattern = _get_line_pattern()";
189
190%values = (
191    0     => 5,
192    1     => 0,
193    2     => 1,
194    3     => 2,
195    4     => 3,
196    5     => 4,
197    6     => 7,
198    7     => 6,
199    8     => 8,
200    9     => 0,
201    undef => 0
202);
203
204$expected = [];
205$got      = [];
206
207while ( my ( $user, $excel ) = each %values ) {
208    push @$got,      $chart->_get_line_pattern( $user );
209    push @$expected, $excel;
210}
211
212is_deeply( $got, $expected, $caption );
213
214
215###############################################################################
216#
217# Test. Line patterns with names.
218#
219$caption = " \tChart: \$pattern = _get_line_pattern()";
220
221%values = (
222    'solid'        => 0,
223    'dash'         => 1,
224    'dot'          => 2,
225    'dash-dot'     => 3,
226    'dash-dot-dot' => 4,
227    'none'         => 5,
228    'dark-gray'    => 6,
229    'medium-gray'  => 7,
230    'light-gray'   => 8,
231    'DASH'         => 1,
232    'fictional'    => 0
233);
234
235$expected = [];
236$got      = [];
237
238while ( my ( $user, $excel ) = each %values ) {
239    push @$got,      $chart->_get_line_pattern( $user );
240    push @$expected, $excel;
241}
242
243is_deeply( $got, $expected, $caption );
244
245
246###############################################################################
247#
248# Test. Line weights with indices.
249#
250$caption = " \tChart: \$weight  = _get_line_weight()";
251
252%values = (
253    1     => -1,
254    2     => 0,
255    3     => 1,
256    4     => 2,
257    5     => 0,
258    0     => 0,
259    undef => 0
260);
261
262$expected = [];
263$got      = [];
264
265while ( my ( $user, $excel ) = each %values ) {
266    push @$got,      $chart->_get_line_weight( $user );
267    push @$expected, $excel;
268}
269
270is_deeply( $got, $expected, $caption );
271
272
273###############################################################################
274#
275# Test. Line weights with names.
276#
277$caption = " \tChart: \$weight  = _get_line_weight()";
278
279%values = (
280    'hairline'  => -1,
281    'narrow'    => 0,
282    'medium'    => 1,
283    'wide'      => 2,
284    'WIDE'      => 2,
285    'Fictional' => 0,
286);
287
288$expected = [];
289$got      = [];
290
291while ( my ( $user, $excel ) = each %values ) {
292    push @$got,      $chart->_get_line_weight( $user );
293    push @$expected, $excel;
294}
295
296is_deeply( $got, $expected, $caption );
297
298
299###############################################################################
300#
301# Clean up.
302#
303$workbook->close();
304unlink $test_file;
305
306
307__END__
308