Template-Alloy-XS
view release on metacpan or search on metacpan
t/00_base.t view on Meta::CPAN
# -*- Mode: Perl; -*-
=head1 NAME
05_tt_base.t - Test the basic language functionality of Template::Alloy - including many edge cases
=cut
use 5.006;
use vars qw($module $is_tt $compile_perl $use_stream $five_six);
BEGIN {
$module = 'Template::Alloy::XS';
if ($ENV{'USE_TT'} || grep {/tt/i} @ARGV) {
$module = 'Template';
}
$is_tt = $module eq 'Template';
$five_six = ($^V < 5.007) ? 1 : 0;
};
use strict;
use Test::More tests => (! $is_tt ? 3074 : 661) - (! $five_six ? 0 : (3 * ($is_tt ? 1 : 2)));
use constant test_taint => 0 && eval { require Taint::Runtime };
use_ok($module);
Taint::Runtime::taint_start() if test_taint;
my $test_dir = $0 .'.test_dir';
END { unlink "$test_dir/stream.out"; rmdir $test_dir }
mkdir $test_dir, 0755;
ok(-d $test_dir, "Got a test dir up and running");
###----------------------------------------------------------------###
sub process_ok { # process the value and say if it was ok
my $str = shift;
my $test = shift;
my $vars = shift || {};
my $conf = local $vars->{'tt_config'} = $vars->{'tt_config'} || [];
push @$conf, (COMPILE_PERL => $compile_perl) if $compile_perl;
push @$conf, (STREAM => 1) if $use_stream;
my $obj = shift || $module->new(@$conf); # new object each time
my $out = '';
my $line = (caller)[2];
delete $vars->{'tt_config'};
Taint::Runtime::taint(\$str) if test_taint;
my $fh;
if ($use_stream) {
open($fh, ">", "$test_dir/stream.out") || return ok(0, "Line $line \"$str\" - Can't open stream.out: $!");
select $fh;
}
$obj->process(\$str, $vars, \$out);
if ($use_stream) {
select STDOUT;
close $fh;
open($fh, "<", "$test_dir/stream.out") || return ok(0, "Line $line \"$str\" - Can't read stream.out: $!");
$out = '';
read($fh, $out, -s "$test_dir/stream.out");
}
my $ok = ref($test) ? $out =~ $test : $out eq $test;
if ($ok) {
ok(1, "Line $line \"$str\" => \"$out\"");
return $obj;
} else {
ok(0, "Line $line \"$str\"");
warn "# Was:\n$out\n# Should've been:\n$test\n";
print $obj->error if $obj->can('error');
print $obj->dump_parse_tree(\$str) if $obj->can('dump_parse_tree');
exit;
}
}
###----------------------------------------------------------------###
### set up some dummy packages for various tests
local $INC{'MyTestPlugin/FooTest.pm'} = $0;
local $INC{'FooTest2.pm'} = $0;
{
package MyTestPlugin::FooTest;
sub load { $_[0] }
sub new {
my $class = shift;
my $context = shift; # note the plugin style object that needs to shift off context
my $args = shift || {};
return bless $args, $class;
}
sub bar { my $self = shift; return join('', map {"$_$self->{$_}"} sort keys %$self) }
sub seven { 7 }
sub many { return 1, 2, 3 }
sub echo { my $self = shift; $_[0] }
}
{
package FooTest2;
use base qw(MyTestPlugin::FooTest);
use vars qw($AUTOLOAD);
sub new {
my $class = shift;
my $args = shift || {}; # note - no plugin context
return bless $args, $class;
( run in 2.684 seconds using v1.01-cache-2.11-cpan-5e09290becf )