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