1#!perl 2# 3# test apparatus for Text::Template module 4# still incomplete. 5 6use Text::Template; 7 8die "This is the test program for Text::Template version 1.46. 9You are using version $Text::Template::VERSION instead. 10That does not make sense.\n 11Aborting" 12 unless $Text::Template::VERSION == 1.46; 13 14 15print "1..12\n"; 16 17$n=1; 18 19$template = 'We will put value of $v (which is "good") here -> {$v}'; 20 21$v = 'oops (main)'; 22$Q::v = 'oops (Q)'; 23 24$vars = { 'v' => \'good' }; 25 26# (1) Build template from string 27$template = new Text::Template ('type' => 'STRING', 'source' => $template); 28print +($template ? '' : 'not '), "ok $n\n"; 29$n++; 30 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); 34print +($text eq $result2 ? '' : 'not '), "ok $n\n"; 35$n++; 36 37# (3) Did we clobber the main variable? 38print +($v eq 'oops (main)' ? '' : 'not '), "ok $n\n"; 39$n++; 40 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); 44print +($text eq $result4 ? '' : 'not '), "ok $n\n"; 45$n++; 46 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'); 50print +($text eq $result5 ? '' : 'not '), "ok $n\n"; 51$n++; 52 53# (6) We expect to have clobbered the Q variable. 54print +($Q::v eq 'good' ? '' : 'not '), "ok $n\n"; 55$n++; 56 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); 60print +($text eq $result7 ? '' : 'not '), "ok $n\n"; 61$n++; 62 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.) 65my $WARNINGS = 0; 66{ 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'; 71 my $template = 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"; 76 $n++; 77 78 # (9) Was the output correct? 79 print +($text eq $result8 ? '' : 'not '), "ok $n\n"; 80 $n++; 81 82 # (10-11) Let's try that again, with a twist this time 83 $WARNINGS = 0; 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"; 87 $n++; 88 89 # (11) Was the output correct? 90 if ($] < 5.005) { 91 print "ok $n # skipped -- not supported before 5.005\n"; 92 } else { 93 print +($text eq $result8 ? '' : 'not '), "ok $n\n"; 94 } 95 $n++; 96} 97 98 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']}, 103 {'v' => \23}, 104 ]); 105$result = q{$v: 23. @v: [a b c].}; 106print +($text eq $result ? '' : 'not '), "ok $n\n"; 107$n++; 108 109 110exit; 111 112