preprocess.t 1.3 KB

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