Template-Alloy

 view release on metacpan or  search on metacpan

t/05_tt_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;
our ($module, $is_tt, $compile_perl, $use_stream, $five_six, $five_eight, $has_tt_filter);
BEGIN {
    $module = 'Template::Alloy';
    if ($ENV{'USE_TT'} || grep {/tt/i} @ARGV) {
        $module = 'Template';
    }
    $is_tt = $module eq 'Template';
    $five_six   = ($^V < 5.007) ? 1 : 0;
    $five_eight = ($^V < 5.009) ? 1 : 0;
    $has_tt_filter = !eval { require Template::Filters } ? 0 : $is_tt ? 1 : 3;
};

use strict;
use Test::More tests => (! $is_tt ? 3260 : 674) - (!$five_six ? 0 : 3*($is_tt ? 1 : 3)) + $has_tt_filter;
use constant test_taint => 0 && eval { require Taint::Runtime };
use Data::Dumper;

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 map {"$_\n"} grep { defined } $obj->error if $obj->can('error');
        print $obj->dump_parse_tree(\$str) if $obj->can('dump_parse_tree');
        my ($k,$v) = each %{ $obj->{'_documents'} };
        #local $Data::Dumper::Terse = 1;
        #local $Data::Dumper::Indent = 0;
        #print "    ".Data::Dumper::Dumper($v->{'_tree'}),"\n";
        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);
    our $AUTOLOAD;



( run in 1.234 second using v1.01-cache-2.11-cpan-800906f7e73 )