1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
use Test::More tests => 11;
use strict;
use warnings;
use lib 't/lib'; # Needed for 'make test' from project dirs
use TestData qw();
use PDFAPI2Mock;
BEGIN {
use_ok('PDF::Table')
}
my ($col_widths);
($col_widths, undef) = PDF::Table::CalcColumnWidths(
[
{ min_w => 50, max_w => 50 },
{ min_w => 50, max_w => 50 },
{ min_w => 50, max_w => 50 },
{ min_w => 50, max_w => 50 },
], 400);
is_deeply( $col_widths, [ 100, 100, 100, 100 ], 'CalcColumnWidths - even');
($col_widths, undef) = PDF::Table::CalcColumnWidths(
[
{ min_w => 41, max_w => 51 },
{ min_w => 58, max_w => 600 },
{ min_w => 48, max_w => 48 },
], 400);
is_deeply( $col_widths, [ 51, 301, 48 ], 'CalcColumnWidths - uneven');
($col_widths, undef) = PDF::Table::CalcColumnWidths(
[
{ min_w => 50, max_w => 50 },
{ min_w => undef, max_w => 50 },
], 400);
is_deeply( $col_widths, [ 200, 200 ], 'CalcColumnWidths - undef');
my ($pdf, $page, $tab, @data, @required);
@data = (
[ 'foo', 'bar', 'baz' ],
);
@required = (
x => 10,
w => 300,
start_y => 750,
next_y => 700,
start_h => 40,
next_h => 500,
);
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab = PDF::Table->new($pdf, $page);
#
# this tickles a bug (#34017) which causes an infinite loop
#
'foo' =~ /(o)(o)/;
$tab->table($pdf, $page, [@data], @required,
border => 1,
border_color => 'black',
font_size => 12,
background_color => 'gray',
column_props => [
{}, { background_color => 'red' }, {},
],
cell_props => [
[ {}, {}, { background_color => 'blue' } ],
],
);
ok($pdf->match(
[[qw(translate 10 738)],[qw(text foo)]],
[[qw(translate 110 738)],[qw(text bar)]],
[[qw(translate 210 738)],[qw(text baz)]],
), 'text position');
ok($pdf->match(
[[qw(rect 10 738 100 12)],[qw(fillcolor gray)]],
[[qw(rect 110 738 100 12)],[qw(fillcolor red)]],
[[qw(rect 210 738 100 12)],[qw(fillcolor blue)]],
), 'default header fillcolor');
ok($pdf->match(
[[qw(gfx)],[qw(strokecolor black)],[qw(linewidth 1)]],
[[qw(stroke)]],
), "draw borders");
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab->table($pdf, $page, [@data], @required,
border => 0,
border_color => 'black',
font_size => 12,
column_props => [
{}, { justify => 'center' }, { justify => 'center' },
],
cell_props => [
[ { justify => 'center' }, {}, { justify => 'right' } ],
],
);
ok($pdf->match(
[[qw(translate 52.5 738)],[qw(text foo)]],
[[qw(translate 152.5 738)],[qw(text bar)]],
[[qw(translate 295 738)],[qw(text baz)]],
), 'justify right and center');
ok(!$pdf->match(
[[qw(gfx)],[qw(strokecolor black)],[qw(linewidth 0)]],
), "don't set up zero-width borders");
# table is only 3 lines high (4*12 > 40).
@data = (
[ 'foo', 'bar' ],
[ 'one', 'two' ],
[ 'thr', 'four score and seven years ago our fathers brought forth' ],
[ 'fiv', 'six' ],
[ 'sev', 'abcdefghijklmnopqrstuvwxyz' ],
);
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab->table($pdf, $page, [@data], @required,
border => 0,
font_size => 12,
max_word_length => 13,
cell_props => [
[],
[ { background_color => 'blue' }, {} ],
],
);
ok(1,'fake test because the one below is not working and must be fixed');
#ok($pdf->match(
# [[qw(page)]],
# [[qw(rect 10 714 20 12)],[qw(fillcolor blue)]],
# [[qw(translate 10 714)],[qw(text thr)]],
# [[qw(page)]],
# [[qw(rect 10 688 20 12)],[qw(fillcolor blue)]],
# [[qw(translate 10 688)],[qw(text -)]],
#), 'keep cell_props values when row spans a page');
ok($pdf->match(
[['text', 'abcdefghijklm nopqrstuvwxyz']],
), 'break long words on max_word_length');
|