1#!perl 2# 3# test apparatus for Text::Template module 4# still incomplete. 5 6use strict; 7use warnings; 8use Test::More tests => 13; 9 10use_ok 'Text::Template' or exit 1; 11 12my $template = 'We will put value of $v (which is "good") here -> {$v}'; 13 14my $v = 'oops (main)'; 15$Q::v = 'oops (Q)'; 16 17my $vars = { 'v' => \'good' }; 18 19# (1) Build template from string 20$template = Text::Template->new('type' => 'STRING', 'source' => $template); 21isa_ok $template, 'Text::Template'; 22 23# (2) Fill in template in anonymous package 24my $result2 = 'We will put value of $v (which is "good") here -> good'; 25my $text = $template->fill_in(HASH => $vars); 26is $text, $result2; 27 28# (3) Did we clobber the main variable? 29is $v, 'oops (main)'; 30 31# (4) Fill in same template again 32my $result4 = 'We will put value of $v (which is "good") here -> good'; 33$text = $template->fill_in(HASH => $vars); 34is $text, $result4; 35 36# (5) Now with a package 37my $result5 = 'We will put value of $v (which is "good") here -> good'; 38$text = $template->fill_in(HASH => $vars, PACKAGE => 'Q'); 39is $text, $result5; 40 41# (6) We expect to have clobbered the Q variable. 42is $Q::v, 'good'; 43 44# (7) Now let's try it without a package 45my $result7 = 'We will put value of $v (which is "good") here -> good'; 46$text = $template->fill_in(HASH => $vars); 47is $text, $result7; 48 49# (8-11) Now what does it do when we pass a hash with undefined values? 50# Roy says it does something bad. (Added for 1.20.) 51my $WARNINGS = 0; 52{ 53 local $SIG{__WARN__} = sub { $WARNINGS++ }; 54 local $^W = 1; # Make sure this is on for this test 55 my $template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}'; 56 my $result8 = 'We will put value of $v (which is "good") here -> good'; 57 my $template = Text::Template->new('type' => 'STRING', 'source' => $template8); 58 my $text = $template->fill_in(HASH => { 'v' => undef }); 59 60 # (8) Did we generate a warning? 61 cmp_ok $WARNINGS, '==', 0; 62 63 # (9) Was the output correct? 64 is $text, $result8; 65 66 # (10-11) Let's try that again, with a twist this time 67 $WARNINGS = 0; 68 $text = $template->fill_in(HASH => [ { 'v' => 17 }, { 'v' => undef } ]); 69 70 # (10) Did we generate a warning? 71 cmp_ok $WARNINGS, '==', 0; 72 73 # (11) Was the output correct? 74 SKIP: { 75 skip 'not supported before 5.005', 1 unless $] >= 5.005; 76 77 is $text, $result8; 78 } 79} 80 81# (12) Now we'll test the multiple-hash option (Added for 1.20.) 82$text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].}, 83 HASH => [ 84 { 'v' => 17 }, 85 { 'v' => [ 'a', 'b', 'c' ] }, 86 { 'v' => \23 } 87 ] 88); 89 90my $result = q{$v: 23. @v: [a b c].}; 91is $text, $result; 92