File: Basics.t

package info (click to toggle)
libpdf-table-perl 1%3A1.002-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 448 kB
  • sloc: perl: 3,006; makefile: 11
file content (118 lines) | stat: -rw-r--r-- 3,347 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 7;

use lib 't/lib';    # Needed for 'make test' from project dir
use TestData;
use PDFAPI2Mock;    # provide dummy PDF::API2. obviously a real PDF::API2 or
                    # PDF::Builder installation will be needed in order to run

BEGIN {
    use_ok('PDF::Table');
}
require_ok('PDF::Table');

my ( $pdf, $page, $tab, @data, %opts );

$pdf  = PDF::API2->new();
$page = $pdf->page();
$tab  = PDF::Table->new($pdf,$page);

@data = ( [ 'foo', 'bar', 'baz' ], );
$tab->table( $pdf, $page, \@data, %TestData::required, );

#Check default font size
ok( $pdf->match( [ [qw(font 1 12)], [qw(font 1 12)], [qw(font 1 12)] ] ),
    'default font_size' )
  || note explain $pdf;

#Check default text placement
ok(
    $pdf->match(
        [ [qw(translate 12 686)],  [qw(text foo)] ],
        [ [qw(translate 112 686)], [qw(text bar)] ],
        [ [qw(translate 212 686)], [qw(text baz)] ],
    ),
    'default text placement in one row'
) or note explain $pdf;

#Check default splitting of long words
@data = ( ['123456789012345678901234567890123456789012345678901234567890'], );
%opts = (
    %TestData::required,
    w => 400,    #override w so table() will not use text_block()
);
$tab->table( $pdf, $page, \@data, %opts );
ok(
    $pdf->match(
        [
            [
                'text',
                '12345678901234567890 12345678901234567890 12345678901234567890'
            ]
        ],
    ),
    'default break long words on every 20th character'
) or note explain $pdf;

#
# Test header alignment if unspecified (should default to column alignment
# if unspecified)
# 
$pdf  = PDF::API2->new();
$page = $pdf->page();
$tab  = PDF::Table->new($pdf,$page);

@data = ( [ 'head1', 'head2', 'head3'], [ 'foo', 'bar', 'baz' ], );

# Match column properties to default header properties
my $col_props = [
    { font_color => '#000066', font_size => 14, background_color => '#FFFFAA', justify => 'left' },
    { font_color => '#000066', font_size => 14, background_color => '#FFFFAA', justify => 'center' },
    { font_color => '#000066', font_size => 14, background_color => '#FFFFAA', justify => 'right' },
];
%opts = (
    %TestData::required,
    column_props => $col_props,
);
$tab->table( $pdf, $page, \@data, %opts );
my @pdf_no_header_props = $pdf->getall;

my $pdf2  = PDF::API2->new();
my $page2 = $pdf2->page();
my $tab2  = PDF::Table->new($pdf2,$page2);

@data = ( [ 'head1', 'head2', 'head3'], [ 'foo', 'bar', 'baz' ], );
%opts = (
    %TestData::required,
    header_props => {
       repeat => 1,
    },
    column_props => $col_props,
);
$tab2->table( $pdf2, $page2, \@data, %opts );
ok(
    $pdf2->match( \@pdf_no_header_props ),
    'Header alignment matches column alignment if unspecified'
) or note explain $pdf2;

$pdf  = PDF::API2->new();
$page = $pdf->page();
@data = ( [0..2], [3..5], [6..8] );
my $cell_data;
%opts = (
    %TestData::required,
    cell_render_hook => sub {
      my ($page, $first_row, $row, $col, $x, $y, $w, $h) = @_;
      $cell_data .= "($row, $col), "; 
    }
);
$tab = PDF::Table->new( $pdf, $page );
$tab->table( $pdf2, $page2, \@data, %opts );
ok(
    $cell_data eq '(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), ',
    'The cell_render_hook() subroutine output is valid'
) or diag explain \$pdf;

1;