1#!perl
2#
3# Tests for PREPROCESSOR features
4# These tests first appeared in version 1.25.
5
6use strict;
7use warnings;
8use Test::More tests => 9;
9use File::Temp;
10
11use_ok 'Text::Template::Preprocess' or exit 1;
12
13my $tmpfile = File::Temp->new;
14my $TMPFILE = $tmpfile->filename;
15
16my $py = sub { tr/x/y/ };
17my $pz = sub { tr/x/z/ };
18
19my $t    = 'xxx The value of $x is {$x}';
20my $outx = 'xxx The value of $x is 119';
21my $outy = 'yyy The value of $y is 23';
22my $outz = 'zzz The value of $z is 5';
23open my $tfh, '>', $TMPFILE or die "Couldn't open test file: $!; aborting";
24print $tfh $t;
25close $tfh;
26
27my @result = ($outx, $outy, $outz, $outz);
28for my $trial (1, 0) {
29    for my $test (0 .. 3) {
30        my $tmpl;
31        if ($trial == 0) {
32            $tmpl = Text::Template::Preprocess->new(TYPE => 'STRING', SOURCE => $t) or die;
33        }
34        else {
35            open $tfh, '<', $TMPFILE or die "Couldn't open test file: $!; aborting";
36            $tmpl = Text::Template::Preprocess->new(TYPE => 'FILEHANDLE', SOURCE => $tfh) or die;
37        }
38        $tmpl->preprocessor($py) if ($test & 1) == 1;
39        my @args = ((($test & 2) == 2) ? (PREPROCESSOR => $pz) : ());
40        my $o = $tmpl->fill_in(@args, HASH => { x => 119, 'y' => 23, z => 5 });
41        is $o, $result[$test];
42    }
43}
44