Test::Inline::Content::Simple - Simple templating Content Handler


Test-Inline documentation Contained in the Test-Inline distribution.

Index


Code Index:

NAME

Top

Test::Inline::Content::Simple - Simple templating Content Handler

SYNOPSIS

Top

  In your inline2test.tpl
  ----------------------
  #!/usr/bin/perl -w

  use strict;
  use Test::More [% plan %];
  $| = 1;

  


  


  [% tests %]

  


  


  1;

DESCRIPTION

Top

It is relatively common to want to customise the contents of the generated test files to set up custom environment things on an all-scripts basis, rather than file by file (using =begin SETUP blocks).

Test::Inline::Content::Simple lets you use a very simple Template Toolkit style template to define this information.

It contains only two tags, plan and tests.

The plan tag will be inserted as either tests = 123> or 'no_plan'.

The tests tag will be replaced by the actual testing code.

METHODS

Top

new $filename

Manually create a new Test::Inline::Content::Simple object. Takes as parameter a single filename which should contain the template code.

Returns a new Test::Inline::Content::Simple object, or undef on error.

template

The template accessor returns the template content for the object

process $Inline, $Script

The process method is unchanged from Test::Inline::Content.

SUPPORT

Top

See the main SUPPORT section.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>, http://ali.as/

COPYRIGHT

Top


Test-Inline documentation Contained in the Test-Inline distribution.
package Test::Inline::Content::Simple;

use strict;
use File::Slurp  ();
use Params::Util qw{_INSTANCE};
use Test::Inline::Content ();

use vars qw{$VERSION @ISA};
BEGIN {
	$VERSION = '2.212';
	@ISA     = 'Test::Inline::Content';
}





#####################################################################
# Constructor and Accessors

sub new {
	my $class = ref $_[0] ? ref shift : shift;
	my $file  = (defined $_[0] and -r $_[0]) ? shift : return undef;
	
	# Create the object
	my $self  = $class->SUPER::new() or return undef;

	# Load, check and add the file
	my $template = File::Slurp::read_file( $file ) or return undef;
	$template =~ /\[%\s+tests\s+\%\]/              or return undef;
	# $template =~ /\[\%\s+plan\s+\%\]/              or return undef;
	$self->{template} = $template;

	$self;
}

sub template { $_[0]->{template} }





#####################################################################
# Test::Inline::Content Methods

sub process {
	my $self   = shift;
	my $Inline = _INSTANCE(shift, 'Test::Inline')         or return undef;
	my $Script = _INSTANCE(shift, 'Test::Inline::Script') or return undef;

	# Get the merged content
	my $content = $Script->merged_content;
	return undef unless defined $content;

	# Determine a plan
	my $tests = $Script->tests;
	my $plan  = defined $tests
		? "tests => $tests"
		: "'no_plan'";

	# Replace the two values
	my $script = $self->{template};
	$script =~ s/\[%\s+tests\s+\%\]/$content/;
	$script =~ s/\[\%\s+plan\s+\%\]/$plan/;

	$script;
}

1;