1*e0c4386eSCy Schubert#!perl 2*e0c4386eSCy Schubert# 3*e0c4386eSCy Schubert# Tests of basic, essential functionality 4*e0c4386eSCy Schubert# 5*e0c4386eSCy Schubert 6*e0c4386eSCy Schubertuse strict; 7*e0c4386eSCy Schubertuse warnings; 8*e0c4386eSCy Schubertuse Test::More tests => 34; 9*e0c4386eSCy Schubertuse File::Temp; 10*e0c4386eSCy Schubert 11*e0c4386eSCy Schubertmy $tmpfile = File::Temp->new; 12*e0c4386eSCy Schubert 13*e0c4386eSCy Schubertuse_ok 'Text::Template' or exit 1; 14*e0c4386eSCy Schubert 15*e0c4386eSCy Schubert$X::v = $Y::v = 0; # Suppress `var used only once' 16*e0c4386eSCy Schubert 17*e0c4386eSCy Schubertmy $template_1 = <<EOM; 18*e0c4386eSCy SchubertWe will put value of \$v (which is "abc") here -> {\$v} 19*e0c4386eSCy SchubertWe will evaluate 1+1 here -> {1 + 1} 20*e0c4386eSCy SchubertEOM 21*e0c4386eSCy Schubert 22*e0c4386eSCy Schubert# (1) Construct temporary template file for testing 23*e0c4386eSCy Schubert# file operations 24*e0c4386eSCy Schubertmy $TEMPFILE = $tmpfile->filename; 25*e0c4386eSCy Schubert 26*e0c4386eSCy Schuberteval { 27*e0c4386eSCy Schubert open my $tmp, '>', $TEMPFILE 28*e0c4386eSCy Schubert or die "Couldn't write tempfile $TEMPFILE: $!"; 29*e0c4386eSCy Schubert 30*e0c4386eSCy Schubert print $tmp $template_1; 31*e0c4386eSCy Schubert close $tmp; 32*e0c4386eSCy Schubert 33*e0c4386eSCy Schubert pass; 34*e0c4386eSCy Schubert}; 35*e0c4386eSCy Schubertif ($@) { 36*e0c4386eSCy Schubert fail $@; 37*e0c4386eSCy Schubert} 38*e0c4386eSCy Schubert 39*e0c4386eSCy Schubert# (2) Build template from file 40*e0c4386eSCy Schubertmy $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE); 41*e0c4386eSCy Schubertok(defined $template) or diag $Text::Template::ERROR; 42*e0c4386eSCy Schubert 43*e0c4386eSCy Schubert# (3) Fill in template from file 44*e0c4386eSCy Schubert$X::v = "abc"; 45*e0c4386eSCy Schubertmy $resultX = <<EOM; 46*e0c4386eSCy SchubertWe will put value of \$v (which is "abc") here -> abc 47*e0c4386eSCy SchubertWe will evaluate 1+1 here -> 2 48*e0c4386eSCy SchubertEOM 49*e0c4386eSCy Schubert$Y::v = "ABC"; 50*e0c4386eSCy Schubertmy $resultY = <<EOM; 51*e0c4386eSCy SchubertWe will put value of \$v (which is "abc") here -> ABC 52*e0c4386eSCy SchubertWe will evaluate 1+1 here -> 2 53*e0c4386eSCy SchubertEOM 54*e0c4386eSCy Schubert 55*e0c4386eSCy Schubertmy $text = $template->fill_in('package' => 'X'); 56*e0c4386eSCy Schubertis $text, $resultX; 57*e0c4386eSCy Schubert 58*e0c4386eSCy Schubert# (4) Fill in same template again 59*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'Y'); 60*e0c4386eSCy Schubertis $text, $resultY; 61*e0c4386eSCy Schubert 62*e0c4386eSCy Schubert# (5) Simple test of `fill_this_in' 63*e0c4386eSCy Schubert$text = Text::Template->fill_this_in($template_1, 'package' => 'X'); 64*e0c4386eSCy Schubertis $text, $resultX; 65*e0c4386eSCy Schubert 66*e0c4386eSCy Schubert# (6) test creation of template from filehandle 67*e0c4386eSCy Schubertopen my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!"; 68*e0c4386eSCy Schubert 69*e0c4386eSCy Schubert$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl); 70*e0c4386eSCy Schubertok defined $template or diag $Text::Template::ERROR; 71*e0c4386eSCy Schubert 72*e0c4386eSCy Schubert# (7) test filling in of template from filehandle 73*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'X'); 74*e0c4386eSCy Schubertis $text, $resultX; 75*e0c4386eSCy Schubert 76*e0c4386eSCy Schubert# (8) test second fill_in on same template object 77*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'Y'); 78*e0c4386eSCy Schubertis $text, $resultY; 79*e0c4386eSCy Schubert 80*e0c4386eSCy Schubertclose $tmpl; 81*e0c4386eSCy Schubert 82*e0c4386eSCy Schubert# (9) test creation of template from array 83*e0c4386eSCy Schubert$template = Text::Template->new( 84*e0c4386eSCy Schubert type => 'ARRAY', 85*e0c4386eSCy Schubert source => [ 86*e0c4386eSCy Schubert 'We will put value of $v (which is "abc") here -> {$v}', "\n", 87*e0c4386eSCy Schubert 'We will evaluate 1+1 here -> {1+1}', "\n" 88*e0c4386eSCy Schubert ] 89*e0c4386eSCy Schubert); 90*e0c4386eSCy Schubert 91*e0c4386eSCy Schubertok defined $template; # or diag $Text::Template::ERROR; 92*e0c4386eSCy Schubert 93*e0c4386eSCy Schubert# (10) test filling in of template from array 94*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'X'); 95*e0c4386eSCy Schubertis $text, $resultX; 96*e0c4386eSCy Schubert 97*e0c4386eSCy Schubert# (11) test second fill_in on same array template object 98*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'Y'); 99*e0c4386eSCy Schubertis $text, $resultY; 100*e0c4386eSCy Schubert 101*e0c4386eSCy Schubert# (12) Make sure \ is working properly 102*e0c4386eSCy Schubert# Test added for version 1.11 103*e0c4386eSCy Schubert$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D'); 104*e0c4386eSCy Schubert 105*e0c4386eSCy Schubert# This should fail if the \ are not interpreted properly. 106*e0c4386eSCy Schubert$text = $tmpl->fill_in(); 107*e0c4386eSCy Schubertis $text, 'B}C{D'; 108*e0c4386eSCy Schubert 109*e0c4386eSCy Schubert# (13) Make sure \ is working properly 110*e0c4386eSCy Schubert# Test added for version 1.11 111*e0c4386eSCy Schubert$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B}); 112*e0c4386eSCy Schubert 113*e0c4386eSCy Schubert# Symptom of old problem: ALL \ were special in templates, so 114*e0c4386eSCy Schubert# The lexer would return (A, PROGTEXT("t"), B), and the 115*e0c4386eSCy Schubert# result text would be AtB instead of A(tab)B. 116*e0c4386eSCy Schubert$text = $tmpl->fill_in(); 117*e0c4386eSCy Schubert 118*e0c4386eSCy Schubertis $text, "A\tB"; 119*e0c4386eSCy Schubert 120*e0c4386eSCy Schubert# (14-27) Make sure \ is working properly 121*e0c4386eSCy Schubert# Test added for version 1.11 122*e0c4386eSCy Schubert# This is a sort of general test. 123*e0c4386eSCy Schubertmy @tests = ( 124*e0c4386eSCy Schubert '{""}' => '', # (14) 125*e0c4386eSCy Schubert '{"}"}' => undef, # (15) 126*e0c4386eSCy Schubert '{"\\}"}' => '}', # One backslash 127*e0c4386eSCy Schubert '{"\\\\}"}' => undef, # Two backslashes 128*e0c4386eSCy Schubert '{"\\\\\\}"}' => '}', # Three backslashes 129*e0c4386eSCy Schubert '{"\\\\\\\\}"}' => undef, # Four backslashes 130*e0c4386eSCy Schubert '{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20) 131*e0c4386eSCy Schubert '{"x20"}' => 'x20', 132*e0c4386eSCy Schubert '{"\\x20"}' => ' ', # One backslash 133*e0c4386eSCy Schubert '{"\\\\x20"}' => '\\x20', # Two backslashes 134*e0c4386eSCy Schubert '{"\\\\\\x20"}' => '\\ ', # Three backslashes 135*e0c4386eSCy Schubert '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25) 136*e0c4386eSCy Schubert '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes 137*e0c4386eSCy Schubert '{"\\x20\\}"}' => ' }', # (27) 138*e0c4386eSCy Schubert); 139*e0c4386eSCy Schubert 140*e0c4386eSCy Schubertwhile (my ($test, $result) = splice @tests, 0, 2) { 141*e0c4386eSCy Schubert my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test); 142*e0c4386eSCy Schubert my $text = $tmpl->fill_in; 143*e0c4386eSCy Schubert 144*e0c4386eSCy Schubert ok(!defined $text && !defined $result || $text eq $result) 145*e0c4386eSCy Schubert or diag "expected .$result. got .$text."; 146*e0c4386eSCy Schubert} 147*e0c4386eSCy Schubert 148*e0c4386eSCy Schubert# (28-30) I discovered that you can't pass a glob ref as your filehandle. 149*e0c4386eSCy Schubert# MJD 20010827 150*e0c4386eSCy Schubert# (28) test creation of template from filehandle 151*e0c4386eSCy Schubert$tmpl = undef; 152*e0c4386eSCy Schubertok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!"; 153*e0c4386eSCy Schubert$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl); 154*e0c4386eSCy Schubertok(defined $template) or diag $Text::Template::ERROR; 155*e0c4386eSCy Schubert 156*e0c4386eSCy Schubert# (29) test filling in of template from filehandle 157*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'X'); 158*e0c4386eSCy Schubertis $text, $resultX; 159*e0c4386eSCy Schubert 160*e0c4386eSCy Schubert# (30) test second fill_in on same template object 161*e0c4386eSCy Schubert$text = $template->fill_in('package' => 'Y'); 162*e0c4386eSCy Schubertis $text, $resultY; 163*e0c4386eSCy Schubert 164*e0c4386eSCy Schubertclose $tmpl; 165*e0c4386eSCy Schubert 166*e0c4386eSCy Schubert# (31) Test _scrubpkg for leakiness 167*e0c4386eSCy Schubert$Text::Template::GEN0::test = 1; 168*e0c4386eSCy SchubertText::Template::_scrubpkg('Text::Template::GEN0'); 169*e0c4386eSCy Schubertok !($Text::Template::GEN0::test 170*e0c4386eSCy Schubert || exists $Text::Template::GEN0::{test} 171*e0c4386eSCy Schubert || exists $Text::Template::{'GEN0::'}); 172*e0c4386eSCy Schubert 173*e0c4386eSCy Schubert# that filename parameter works. we use BROKEN to verify this 174*e0c4386eSCy Schubert$text = Text::Template->new( 175*e0c4386eSCy Schubert TYPE => 'string', 176*e0c4386eSCy Schubert SOURCE => 'Hello {1/0}' 177*e0c4386eSCy Schubert)->fill_in(FILENAME => 'foo.txt'); 178*e0c4386eSCy Schubert 179*e0c4386eSCy Schubertlike $text, qr/division by zero at foo\.txt line 1/; 180