Devel::Cover for Template-Toolkit templates


Concept module Template::Coverable so that Devel::Cover coverage stats can be collected against Template-Toolkit templates. Coupled with a wrapper to unit test of templates, this can be a powerful testing tool for those TT apps that don't have 100.0% separatation of logic & content.
dwestbrook@gmail.com (cpan/perlmonks: davidrw)

Presentation

This Devel::Cover::TT method is a drop-in replacement for Devel::Cover, and does the necessary adjustments (for Devel::Cover and Template::Coverable) under the hood.
  use Devel::Cover::TT qw{ -silent 1 -ignore . };
Template source:
[% title %][% ' - ' _ subtitle IF subtitle %]
~~~~
[% PROCESS show_rows IF verbose && rows.size > 0 %]
~~~~

[% BLOCK show_rows %]
  [% FOREACH row = rows -%]
    [% row.x %] / [% row.y %]  =  [% row.y == 0 ? 'N/A' : ( row.x / row.y ) %]
  [% END -%]
[% END %]

Input for first test:
{
  title => 'Test1',
  verbose => 0,
  rows => [
    { x => 1, y => 2, },
    { x => 2, y => 4, },
  ],
};
Output from first test
Coverage wjust first test
Input for second test:
{
  title => 'Test2',
  subtitle => 'second test',
  verbose => 1,
  rows => [
    { x => 1, y => 2, },
    { x => 3, y => 0, },
    { x => 2, y => 4, },
  ],
};
Output from second test
Coverage w/both tests


Details: it's coverage of the TT-compiled perl code based on the actual template .. so not exact, but definitely still useful

i changed the TT caching scheme from:
  save to cache -- file has contents:
      Template::Document->new( .... );
  read from cache:
     $foo = require  $cachefile
to this:
  save to cache -- file has contents:
      package blah::blah::blah::template_name_tmpl.pm;
      sub getDocument {
      Template::Document->new( .... );
      }
      1;
  read from cache:
    eval {
      use blah::blah::blah::template_name_tmpl;
      $foo = blah::blah::blah::template_name_tmpl->getDocument();
    }
since TT apparently doesn't cover stuff in a file executed by require ... so forced it to look like it belonged to a module.