3 # test apparatus for Text::Template module
8 use Test::More tests => 13;
10 use_ok 'Text::Template' or exit 1;
12 my $template = 'We will put value of $v (which is "good") here -> {$v}';
14 my $v = 'oops (main)';
17 my $vars = { 'v' => \'good' };
19 # (1) Build template from string
20 $template = Text::Template->new('type' => 'STRING', 'source' => $template);
21 isa_ok $template, 'Text::Template';
23 # (2) Fill in template in anonymous package
24 my $result2 = 'We will put value of $v (which is "good") here -> good';
25 my $text = $template->fill_in(HASH => $vars);
28 # (3) Did we clobber the main variable?
31 # (4) Fill in same template again
32 my $result4 = 'We will put value of $v (which is "good") here -> good';
33 $text = $template->fill_in(HASH => $vars);
36 # (5) Now with a package
37 my $result5 = 'We will put value of $v (which is "good") here -> good';
38 $text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
41 # (6) We expect to have clobbered the Q variable.
44 # (7) Now let's try it without a package
45 my $result7 = 'We will put value of $v (which is "good") here -> good';
46 $text = $template->fill_in(HASH => $vars);
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.)
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 });
60 # (8) Did we generate a warning?
61 cmp_ok $WARNINGS, '==', 0;
63 # (9) Was the output correct?
66 # (10-11) Let's try that again, with a twist this time
68 $text = $template->fill_in(HASH => [ { 'v' => 17 }, { 'v' => undef } ]);
70 # (10) Did we generate a warning?
71 cmp_ok $WARNINGS, '==', 0;
73 # (11) Was the output correct?
75 skip 'not supported before 5.005', 1 unless $] >= 5.005;
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"}].},
85 { 'v' => [ 'a', 'b', 'c' ] },
90 my $result = q{$v: 23. @v: [a b c].};