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