3 # test apparatus for Text::Template module
8 die "This is the test program for Text::Template version 1.46.
9 You are using version $Text::Template::VERSION instead.
10 That does not make sense.\n
12 unless $Text::Template::VERSION == 1.46;
19 $template = 'We will put value of $v (which is "good") here -> {$v}';
24 $vars = { 'v' => \'good' };
26 # (1) Build template from string
27 $template = new Text::Template ('type' => 'STRING', 'source' => $template);
28 print +($template ? '' : 'not '), "ok $n\n";
31 # (2) Fill in template in anonymous package
32 $result2 = 'We will put value of $v (which is "good") here -> good';
33 $text = $template->fill_in(HASH => $vars);
34 print +($text eq $result2 ? '' : 'not '), "ok $n\n";
37 # (3) Did we clobber the main variable?
38 print +($v eq 'oops (main)' ? '' : 'not '), "ok $n\n";
41 # (4) Fill in same template again
42 $result4 = 'We will put value of $v (which is "good") here -> good';
43 $text = $template->fill_in(HASH => $vars);
44 print +($text eq $result4 ? '' : 'not '), "ok $n\n";
47 # (5) Now with a package
48 $result5 = 'We will put value of $v (which is "good") here -> good';
49 $text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
50 print +($text eq $result5 ? '' : 'not '), "ok $n\n";
53 # (6) We expect to have clobbered the Q variable.
54 print +($Q::v eq 'good' ? '' : 'not '), "ok $n\n";
57 # (7) Now let's try it without a package
58 $result7 = 'We will put value of $v (which is "good") here -> good';
59 $text = $template->fill_in(HASH => $vars);
60 print +($text eq $result7 ? '' : 'not '), "ok $n\n";
63 # (8-11) Now what does it do when we pass a hash with undefined values?
64 # Roy says it does something bad. (Added for 1.20.)
67 local $SIG{__WARN__} = sub {$WARNINGS++};
68 local $^W = 1; # Make sure this is on for this test
69 $template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
70 $result8 = 'We will put value of $v (which is "good") here -> good';
72 new Text::Template ('type' => 'STRING', 'source' => $template8);
73 my $text = $template->fill_in(HASH => {'v' => undef});
74 # (8) Did we generate a warning?
75 print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
78 # (9) Was the output correct?
79 print +($text eq $result8 ? '' : 'not '), "ok $n\n";
82 # (10-11) Let's try that again, with a twist this time
84 $text = $template->fill_in(HASH => [{'v' => 17}, {'v' => undef}]);
85 # (10) Did we generate a warning?
86 print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
89 # (11) Was the output correct?
91 print "ok $n # skipped -- not supported before 5.005\n";
93 print +($text eq $result8 ? '' : 'not '), "ok $n\n";
99 # (12) Now we'll test the multiple-hash option (Added for 1.20.)
100 $text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].},
101 HASH => [{'v' => 17},
102 {'v' => ['a', 'b', 'c']},
105 $result = q{$v: 23. @v: [a b c].};
106 print +($text eq $result ? '' : 'not '), "ok $n\n";