1*e0c4386eSCy Schubert#!perl 2*e0c4386eSCy Schubert# 3*e0c4386eSCy Schubert# test apparatus for Text::Template module 4*e0c4386eSCy Schubert# still incomplete. 5*e0c4386eSCy Schubert 6*e0c4386eSCy Schubertuse strict; 7*e0c4386eSCy Schubertuse warnings; 8*e0c4386eSCy Schubertuse Test::More; 9*e0c4386eSCy Schubert 10*e0c4386eSCy Schubertunless (eval { require Safe; 1 }) { 11*e0c4386eSCy Schubert plan skip_all => 'Safe.pm is required for this test'; 12*e0c4386eSCy Schubert} 13*e0c4386eSCy Schubertelse { 14*e0c4386eSCy Schubert plan tests => 12; 15*e0c4386eSCy Schubert} 16*e0c4386eSCy Schubert 17*e0c4386eSCy Schubertuse_ok 'Text::Template' or exit 1; 18*e0c4386eSCy Schubert 19*e0c4386eSCy Schubertmy $c = Safe->new or die; 20*e0c4386eSCy Schubert 21*e0c4386eSCy Schubert# Test handling of packages and importing. 22*e0c4386eSCy Schubert$c->reval('$P = "safe root"'); 23*e0c4386eSCy Schubertour $P = 'main'; 24*e0c4386eSCy Schubert$Q::P = $Q::P = 'Q'; 25*e0c4386eSCy Schubert 26*e0c4386eSCy Schubert# How to effectively test the gensymming? 27*e0c4386eSCy Schubert 28*e0c4386eSCy Schubertmy $t = Text::Template->new( 29*e0c4386eSCy Schubert TYPE => 'STRING', 30*e0c4386eSCy Schubert SOURCE => 'package is {$P}') or die; 31*e0c4386eSCy Schubert 32*e0c4386eSCy Schubert# (1) Default behavior: Inherit from calling package, `main' in this case. 33*e0c4386eSCy Schubertmy $text = $t->fill_in(); 34*e0c4386eSCy Schubertis $text, 'package is main'; 35*e0c4386eSCy Schubert 36*e0c4386eSCy Schubert# (2) When a package is specified, we should use that package instead. 37*e0c4386eSCy Schubert$text = $t->fill_in(PACKAGE => 'Q'); 38*e0c4386eSCy Schubertis $text, 'package is Q'; 39*e0c4386eSCy Schubert 40*e0c4386eSCy Schubert# (3) When no package is specified in safe mode, we should use the 41*e0c4386eSCy Schubert# default safe root. 42*e0c4386eSCy Schubert$text = $t->fill_in(SAFE => $c); 43*e0c4386eSCy Schubertis $text, 'package is safe root'; 44*e0c4386eSCy Schubert 45*e0c4386eSCy Schubert# (4) When a package is specified in safe mode, we should use the 46*e0c4386eSCy Schubert# default safe root, after aliasing to the specified package 47*e0c4386eSCy SchubertTODO: { 48*e0c4386eSCy Schubert local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION; 49*e0c4386eSCy Schubert $text = $t->fill_in(SAFE => $c, PACKAGE => 'Q'); 50*e0c4386eSCy Schubert is $text, 'package is Q'; 51*e0c4386eSCy Schubert} 52*e0c4386eSCy Schubert 53*e0c4386eSCy Schubert# Now let's see if hash vars are installed properly into safe templates 54*e0c4386eSCy Schubert$t = Text::Template->new( 55*e0c4386eSCy Schubert TYPE => 'STRING', 56*e0c4386eSCy Schubert SOURCE => 'hash is {$H}') or die; 57*e0c4386eSCy Schubert 58*e0c4386eSCy Schubert# (5) First in default mode 59*e0c4386eSCy Schubert$text = $t->fill_in(HASH => { H => 'good5' }); 60*e0c4386eSCy Schubertis $text, 'hash is good5'; 61*e0c4386eSCy Schubert 62*e0c4386eSCy Schubert# suppress "once" warnings 63*e0c4386eSCy Schubert$Q::H = $Q2::H = undef; 64*e0c4386eSCy Schubert 65*e0c4386eSCy Schubert# (6) Now in packages 66*e0c4386eSCy Schubert$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q'); 67*e0c4386eSCy Schubertis $text, 'hash is good6'; 68*e0c4386eSCy Schubert 69*e0c4386eSCy Schubert# (7) Now in the default root of the safe compartment 70*e0c4386eSCy SchubertTODO: { 71*e0c4386eSCy Schubert local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION; 72*e0c4386eSCy Schubert $text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c); 73*e0c4386eSCy Schubert is $text, 'hash is good7'; 74*e0c4386eSCy Schubert} 75*e0c4386eSCy Schubert 76*e0c4386eSCy Schubert# (8) Now in the default root after aliasing to a package that 77*e0c4386eSCy Schubert# got the hash stuffed in 78*e0c4386eSCy Schubertour $H; 79*e0c4386eSCy SchubertTODO: { 80*e0c4386eSCy Schubert local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION; 81*e0c4386eSCy Schubert $text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2'); 82*e0c4386eSCy Schubert is $text, 'hash is good8'; 83*e0c4386eSCy Schubert} 84*e0c4386eSCy Schubert 85*e0c4386eSCy Schubert# Now let's make sure that none of the packages leaked on each other. 86*e0c4386eSCy Schubert# (9) This var should NOT have been installed into the main package 87*e0c4386eSCy Schubertok !defined $H; 88*e0c4386eSCy Schubert$H = $H; 89*e0c4386eSCy Schubert 90*e0c4386eSCy Schubert# (11) this value overwrote the one from test 6. 91*e0c4386eSCy Schubertis $Q::H, 'good7'; 92*e0c4386eSCy Schubert 93*e0c4386eSCy Schubert# (12) 94*e0c4386eSCy Schubertis $Q2::H, 'good8'; 95