#!/usr/bin/perl -w
###############################################################################
#
# A test for Spreadsheet::WriteExcelXML.
#
# Tests cell formatting in the Spreadsheet::WriteExcelXML::Format module.
#
# reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
#
use strict;
use Spreadsheet::WriteExcelXML;
use Test::More tests => 193;
my @tests1 = ( # Cell alignment properties
# Horizontal properties
[[],
'' ],
[['align', 'top'],
'' ],
[['align', 'vcenter'],
'' ],
[['align', 'bottom'],
'' ],
[['align', 'vjustify'],
'' ],
[['align', 'vdistributed'],
'' ],
# Horizontal properties
[['align', 'left'],
'' ],
[['align', 'left', 'indent', 1],
'' ],
[['align', 'center'],
''],
[['align', 'right'],
'' ],
[['align', 'right', 'indent', 1],
'' ],
[['align', 'fill'],
'' ],
[['align', 'justify'],
'' ],
[['align', 'center_across'],
'' ],
[['align', 'distributed', 'indent', 0],
'' ],
[['align', 'distributed', 'indent', 1],
'' ],
# TODO
# ss:Horizontal="JustifyDistributed" ss:Vertical="Bottom"
# Other options
[['text_wrap', 1],
'' ],
[['shrink', 1],
'' ],
[['reading_order', 1],
'' ],
[['reading_order', 2],
'' ],
[['text_vertical', 1],
'' ],
[['rotation', 45],
'' ],
[['rotation', -45],
'' ],
[['rotation', 90],
'' ],
[['rotation', -90],
'' ],
[['rotation', 270],
'' ],
# Tests for properties are mutually exclusive
[['text_vertical', 1, 'rotation', 45],
'' ],
[['text_wrap', 1, 'shrink', 1],
'' ],
[['align', 'fill', 'shrink', 1],
'' ],
[['align', 'justify', 'shrink', 1],
'' ],
[['align', 'distributed', 'shrink', 1],
'' ],
);
my @tests2 = ( # Border properties
[['top' => 1],
'' ],
[['top' => 2],
'' ],
[['top' => 3],
'' ],
[['top' => 4],
'' ],
[['top' => 5],
'' ],
[['top' => 6],
'' ],
[['top' => 7],
'' ],
[['top' => 8],
'' ],
[['top' => 9],
'' ],
[['top' => 10],
'' ],
[['top' => 11],
'' ],
[['top' => 12],
'' ],
[['top' => 13],
'' ],
# Other sides
[['bottom' => 1],
'' ],
[['left' => 1],
'' ],
[['right' => 1],
'' ],
# With Color
[['top' => 1, 'border_color' => 'red'],
'' ],
# Diagonal borders
[['diag_type' => 1, 'diag_border' => 1],
'' ],
[['diag_type' => 2, 'diag_border' => 1],
'' ],
[['diag_type' => 1, 'diag_border' => 1, 'diag_color' =>'red'],
'' ],
[['diag_type' => 2, 'diag_border' => 1, 'diag_color' =>'red'],
'' ],
# Diagonal borders
[['diag_type' => 1],
'' ],
);
my @tests2b = ( # Multiple border properties
[['border' => 1],
'' .
'' .
'' .
'' ],
[['border' => 1, 'bottom_color' => 'red',
'left_color' => 'blue',
'right_color' => 'yellow',
'top_color' => 'green',],
'' .
'' .
'' .
'' ],
# Diagonal borders
[['diag_type' => 3, 'diag_border' => 1],
'' .
'' ],
[['diag_type' => 3, 'diag_border' => 1, 'diag_color' =>'red'],
'' .
'' ],
);
my @tests3 = ( # Font properties
[[],
'' ],
[['color' => 'red'],
'' ],
[['bold' => 1 ],
'' ],
[['bold' => 100],
'' ],
[['italic' => 1],
'' ],
[['underline' => 1],
'' ],
[['underline' => 2],
'' ],
[['underline' => 33],
'' ],
[['font_strikeout' => 1],
'' ],
[['font_script' => 1],
'' ],
[['font_script' => 2],
'' ],
[['font_outline' => 1],
'' ],
[['font_shadow' => 1],
'' ],
[['font_family' => 'Swiss'],
'' ],
[['font_charset' => 'Test'],
'' ],
[['font' => 'Arial Black'],
'' ],
[['size' => 12],
'' ],
[['font' => 'Arial Black', 'size' => 12],
'' ],
[['font' => 'Arial Black', 'size' => 12],
'' ],
);
my @tests4 = ( # Interiors properties
[[],
'' ],
# No pattern. Foreground color only
[['fg_color' => 'red'],
'' ],
# No pattern. Background color only
[['bg_color' => 'red'],
'' ],
# No pattern. bg color takes precedence over fg color
[['bg_color' => 'red', 'fg_color' => 'green',],
'' ],
# Solid pattern only
[['pattern' => 1],
'' ],
# Non-solid pattern only
[['pattern' => 2],
'' ],
# Solid pattern. Background color only
[['bg_color' => 'red', 'pattern' => 1],
'' ],
# Solid pattern. Foreground color only
[['fg_color' => 'red', 'pattern' => 1],
'' ],
# Explicit example
[['bg_color' => 'yellow',
'fg_color' => 'red',
'pattern' => 15],
'' ],
# All non-solid patterns
[['pattern' => 2],
'' ],
[['pattern' => 3],
'' ],
[['pattern' => 4],
'' ],
[['pattern' => 5],
'' ],
[['pattern' => 6],
'' ],
[['pattern' => 7],
'' ],
[['pattern' => 8],
'' ],
[['pattern' => 9],
'' ],
[['pattern' => 10],
'' ],
[['pattern' => 11],
'' ],
[['pattern' => 12],
'' ],
[['pattern' => 13],
'' ],
[['pattern' => 14],
'' ],
[['pattern' => 15],
'' ],
[['pattern' => 16],
'' ],
[['pattern' => 17],
'' ],
[['pattern' => 18],
'' ],
# Out of range
[['pattern' => 19],
'' ],
);
my @tests5 = ( # Number formats
[[],
'' ],
[['num_format' => 1],
'' ],
[['num_format' => 2],
'' ],
[['num_format' => 3],
'' ],
[['num_format' => 4],
'' ],
[['num_format' => 5],
'' ],
[['num_format' => 6],
'' ],
[['num_format' => 7],
'' ],
[['num_format' => 8],
'' ],
[['num_format' => 9],
'' ],
[['num_format' => 10],
'' ],
[['num_format' => 11],
'' ],
[['num_format' => 12],
'' ],
[['num_format' => 13],
'' ],
[['num_format' => 14],
'' ],
[['num_format' => 15],
'' ],
[['num_format' => 16],
'' ],
[['num_format' => 17],
'' ],
[['num_format' => 18],
'' ],
[['num_format' => 19],
'' ],
[['num_format' => 20],
'' ],
[['num_format' => 21],
'' ],
[['num_format' => 22],
'' ],
# Omitted internal international formats 23 .. 36
[['num_format' => 37],
'' ],
[['num_format' => 38],
'' ],
[['num_format' => 39],
'' ],
[['num_format' => 40],
'' ],
[['num_format' => 41],
'' ],
[['num_format' => 42],
'' ],
[['num_format' => 43],
'' ],
[['num_format' => 44],
''],
[['num_format' => 45],
'' ],
[['num_format' => 46],
'' ],
[['num_format' => 47],
'' ],
[['num_format' => 48],
'' ],
[['num_format' => 49],
'' ],
# Use named explicit formats
[['num_format' => 'General'],
'' ],
[['num_format' => 'General Number'],
'' ],
[['num_format' => 'General Date'],
'' ],
[['num_format' => 'Long Date'],
'' ],
[['num_format' => 'Medium Date'],
'' ],
[['num_format' => 'Short Date'],
'' ],
[['num_format' => 'Long Time'],
'' ],
[['num_format' => 'Medium Time'],
'' ],
[['num_format' => 'Short Time'],
'' ],
[['num_format' => 'Currency'],
'' ],
[['num_format' => 'Euro Currency'],
'' ],
[['num_format' => 'Fixed'],
'' ],
[['num_format' => 'Standard'],
'' ],
[['num_format' => 'Percent'],
'' ],
[['num_format' => 'Scientific'],
'' ],
[['num_format' => 'Yes/No'],
'' ],
[['num_format' => 'True/False'],
'' ],
[['num_format' => 'On/Off'],
'' ],
# Use other explicit formats
[['num_format' => 'mm:ss'],
'' ],
[['num_format' => '@'],
'' ],
);
my @tests6 = ( # Protection formats
[[],
'' ],
[['hidden' => 1],
'' ],
[['locked' => 0],
'' ],
[['hidden' => 1, 'locked' => 0],
'' ],
);
my @tests7 = ( # Font color conversion test
[ ["color" => "red" ], "ss:Color #FF0000" ],
[ ["color" => "purple" ], "ss:Color #800080" ],
[ ["color" => "lime" ], "ss:Color #00FF00" ],
[ ["color" => "blue" ], "ss:Color #0000FF" ],
[ ["color" => "yellow" ], "ss:Color #FFFF00" ],
[ ["color" => "silver" ], "ss:Color #C0C0C0" ],
[ ["color" => "magenta" ], "ss:Color #FF00FF" ],
[ ["color" => "gray" ], "ss:Color #808080" ],
[ ["color" => "cyan" ], "ss:Color #00FFFF" ],
[ ["color" => "brown" ], "ss:Color #800000" ],
[ ["color" => "orange" ], "ss:Color #FF6600" ],
[ ["color" => "black" ], "ss:Color #000000" ],
[ ["color" => "green" ], "ss:Color #008000" ],
[ ["color" => "white" ], "ss:Color #FFFFFF" ],
[ ["color" => "navy" ], "ss:Color #000080" ],
[ ["color" => 10 ], "ss:Color #FF0000" ],
[ ["color" => 20 ], "ss:Color #800080" ],
[ ["color" => 11 ], "ss:Color #00FF00" ],
[ ["color" => 12 ], "ss:Color #0000FF" ],
[ ["color" => 13 ], "ss:Color #FFFF00" ],
[ ["color" => 22 ], "ss:Color #C0C0C0" ],
[ ["color" => 14 ], "ss:Color #FF00FF" ],
[ ["color" => 23 ], "ss:Color #808080" ],
[ ["color" => 15 ], "ss:Color #00FFFF" ],
[ ["color" => 16 ], "ss:Color #800000" ],
[ ["color" => 53 ], "ss:Color #FF6600" ],
[ ["color" => 8 ], "ss:Color #000000" ],
[ ["color" => 17 ], "ss:Color #008000" ],
[ ["color" => 9 ], "ss:Color #FFFFFF" ],
[ ["color" => 18 ], "ss:Color #000080" ],
);
my $test_file = "temp_test_file.xml";
my $workbook = Spreadsheet::WriteExcelXML->new($test_file);
$workbook->{_filehandle} = undef; # Turn default print off during testing.
my $worksheet = $workbook->add_worksheet();
$workbook->close();
unlink $test_file;
###############################################################################
#
# 1. Run the tests for cell font properties.
#
for my $test_ref (@tests1) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my @attribs = $format->get_align_properties();
is($workbook->_write_xml_element(0, 0, 0, 'Alignment', @attribs),
$result, "Testing alignment:\t" . join " ", @test_data );
}
###############################################################################
#
# 2. Run the tests for cell border properties.
#
for my $test_ref (@tests2) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my ($aref) = $format->get_border_properties();
my @attribs = @$aref;
is($workbook->_write_xml_element(0, 0, 0, 'Border', @attribs),
$result, "Testing borders:\t" . join " ", @test_data );
}
###############################################################################
#
# 2b. Run the tests for multiple cell border properties.
#
for my $test_ref (@tests2b) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my $str = '';
$str .= $workbook->_write_xml_element(0, 0, 0, 'Border', @$_)
for $format->get_border_properties();
is($str, $result, "Testing borders:\t" . join " ", @test_data );
}
###############################################################################
#
# 3. Run the tests for cell font properties.
#
for my $test_ref (@tests3) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my @attribs = $format->get_font_properties();
is($workbook->_write_xml_element(0, 0, 0, 'Font', @attribs),
$result, "Testing fonts:\t" . join " ", @test_data );
}
###############################################################################
#
# 4. Run the tests for cell interior/pattern properties.
#
for my $test_ref (@tests4) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my @attribs = $format->get_interior_properties();
is($workbook->_write_xml_element(0, 0, 0, 'Interior', @attribs),
$result, "Testing interior:\t" . join " ", @test_data );
}
###############################################################################
#
# 5. Run the tests for cell font properties.
#
for my $test_ref (@tests5) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my @attribs = $format->get_num_format_properties();
is($workbook->_write_xml_element(0, 0, 0, 'NumberFormat', @attribs),
$result, "Testing interior:\t" . join " ", @test_data );
}
###############################################################################
#
# 6. Run the tests for cell protection properties.
#
for my $test_ref (@tests6) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
my @attribs = $format->get_protection_properties();
is($workbook->_write_xml_element(0, 0, 0, 'Protection', @attribs),
$result, "Testing interior:\t" . join " ", @test_data );
}
###############################################################################
#
# 7. Run the tests for colour conversions.
#
for my $test_ref (@tests7) {
my $format = $workbook->add_format();
my @test_data = @{$test_ref->[0]};
my $result = $test_ref->[1];
$format->set_properties(@test_data);
is(join(" ", $format->get_font_properties()), $result,
"Testing colors:\t" . join " ", @test_data );
}
__END__