12-preprocess.t 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!perl
  2. #
  3. # Tests for PREPROCESSOR features
  4. # These tests first appeared in version 1.25.
  5. use Text::Template::Preprocess;
  6. die "This is the test program for Text::Template::Preprocess version 1.46.
  7. You are using version $Text::Template::Preprocess::VERSION instead.
  8. That does not make sense.\n
  9. Aborting"
  10. unless $Text::Template::Preprocess::VERSION == 1.46;
  11. $TMPFILE = "tt$$";
  12. print "1..8\n";
  13. my $n = 1;
  14. my $py = sub { tr/x/y/ };
  15. my $pz = sub { tr/x/z/ };
  16. my $t = 'xxx The value of $x is {$x}';
  17. my $outx = 'xxx The value of $x is 119';
  18. my $outy = 'yyy The value of $y is 23';
  19. my $outz = 'zzz The value of $z is 5';
  20. open TF, "> $TMPFILE" or die "Couldn't open test file: $!; aborting";
  21. print TF $t;
  22. close TF;
  23. @result = ($outx, $outy, $outz, $outz);
  24. for my $trial (1, 0) {
  25. for my $test (0 .. 3) {
  26. my $tmpl;
  27. if ($trial == 0) {
  28. $tmpl = new Text::Template::Preprocess
  29. (TYPE => 'STRING', SOURCE => $t) or die;
  30. } else {
  31. open TF, "< $TMPFILE" or die "Couldn't open test file: $!; aborting";
  32. $tmpl = new Text::Template::Preprocess
  33. (TYPE => 'FILEHANDLE', SOURCE => \*TF) or die;
  34. }
  35. $tmpl->preprocessor($py) if ($test & 1) == 1;
  36. my @args = ((($test & 2) == 2) ? (PREPROCESSOR => $pz) : ());
  37. my $o = $tmpl->fill_in(@args,
  38. HASH => {x => 119, 'y' => 23, z => 5});
  39. # print STDERR "$o/$result[$test]\n";
  40. print +(($o eq $result[$test]) ? '' : 'not '), "ok $n\n";
  41. $n++;
  42. }
  43. }
  44. unlink $TMPFILE;