1 | #!perl
|
---|
2 | #
|
---|
3 | # Tests for PREPROCESSOR features
|
---|
4 | # These tests first appeared in version 1.25.
|
---|
5 |
|
---|
6 | use strict;
|
---|
7 | use warnings;
|
---|
8 | use Test::More tests => 9;
|
---|
9 | use File::Temp;
|
---|
10 |
|
---|
11 | use_ok 'Text::Template::Preprocess' or exit 1;
|
---|
12 |
|
---|
13 | my $tmpfile = File::Temp->new;
|
---|
14 | my $TMPFILE = $tmpfile->filename;
|
---|
15 |
|
---|
16 | my $py = sub { tr/x/y/ };
|
---|
17 | my $pz = sub { tr/x/z/ };
|
---|
18 |
|
---|
19 | my $t = 'xxx The value of $x is {$x}';
|
---|
20 | my $outx = 'xxx The value of $x is 119';
|
---|
21 | my $outy = 'yyy The value of $y is 23';
|
---|
22 | my $outz = 'zzz The value of $z is 5';
|
---|
23 | open my $tfh, '>', $TMPFILE or die "Couldn't open test file: $!; aborting";
|
---|
24 | print $tfh $t;
|
---|
25 | close $tfh;
|
---|
26 |
|
---|
27 | my @result = ($outx, $outy, $outz, $outz);
|
---|
28 | for 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 | }
|
---|