CAM-PDF

 view release on metacpan or  search on metacpan

t/pdf.t  view on Meta::CPAN

#!/usr/bin/perl -w

use warnings;
use strict;
use Carp;

$SIG{__WARN__} = $SIG{__DIE__} = \&Carp::confess;

use Test::More;

# If any test PDF has more than this many pages, trim them
my $max_pages = 30;
my $begin_pages = 9;
my $end_pages = 9;

# This is a little complicated because there are some tests that are
# intended to run only on the developer's computer (namely, against
# PDF docs that are too big or cannot be distributed).

# We build up a list of PDFs to test, then winnow out the ones we
# can't test.

my @testdocs = filter_testdocs(
   {
      filename => 't/inlineimage.pdf',
      linear => 0,
      pages => [ { extern_images => 1, inline_images => 1 } ],
   },
   {
      filename => 't/sample1.pdf',
      linear => 0,
      pages => [ { extern_images => 7, inline_images => 0 },
                 { extern_images => 0, inline_images => 0 } ],
   },
   {
      filename => 't/resume.pdf',
      linear => 0,
      pages => [ { extern_images => 0, inline_images => 0 },
                 { extern_images => 0, inline_images => 0 } ],
   },
   {
      filename => 't/PDFReference15_v5.pdf',
      linear => 1,
      pages => [ map { +{ } } 1..1172 ],  # I don't know how many images per page
   },
   {
      # This one has Type 2 encryption, but I don't own it so I can't include it in the CPAN upload
      filename => 't/Sample5500.pdf',
      linear => 0,
      pages => [ map { +{ } } 1..3 ],  # no images
      permissions => [1,0,0,1], # no modify, no copy
   },
   {
      # This is a crazy one that uses PDF v1.5 object streams and cross-reference streams and annotations
      filename => 't/pdf15.pdf',
      linear => 1,
      pages => [ +{ } ],
   },
);

{
   # Set up the test plan dynamically using the @testdocs data
   my @testpages = grep {!$_->{skip}} map {@{$_->{pages}}} @testdocs;
   my @impages = grep {exists $_->{extern_images}} @testpages;
   if (@testpages > 30) # that is, on my dev machine
   {
      diag 'docs: '.@testdocs.', pages: '.@testpages.', impages: '.@impages;
   }

   my $tests = 2 + @testdocs * 33 + @testpages * 4 + @impages * 4;
   plan tests => $tests;
}

# Begin testing

use_ok('CAM::PDF');

# Utility routines for diagnosing test failures
sub clearerr { $CAM::PDF::errstr = ''; }
sub checkerr { if ($CAM::PDF::errstr) { diag($CAM::PDF::errstr); clearerr(); } }

# Flag.  We only do this test one time total, since it is slow
my $did_deep_compare = 0;

my $can_test_leaks = eval { require Test::Memory::Cycle; 1; };

foreach my $testdoc (@testdocs)
{
   clearerr();

   my $file = $testdoc->{filename};

   my $doc = CAM::PDF->new($file);
   ok($doc, 'open pdf '.$file);
   checkerr();

   is($doc->numPages(), scalar @{$testdoc->{pages}}, 'test predicted right number of pages');

   {
      open my $fh, '<', $file or die;
      binmode $fh;
      my $content = do{local $/=undef;<$fh>};
      close $fh;
      ok($doc->toPDF() eq $content, 'toPDF');
   }
   
   is($doc->isLinearized() ? 1 : 0, $testdoc->{linear}, 'isLinearized');



( run in 1.039 second using v1.01-cache-2.11-cpan-b16cb0d3907 )