3 # Tests of basic, essential functionality
7 $X::v = $Y::v = 0; # Suppress `var used only once'
14 We will put value of \$v (which is "abc") here -> {\$v}
15 We will evaluate 1+1 here -> {1 + 1}
18 # (1) Construct temporary template file for testing
21 open(TMP, "> $TEMPFILE") or print "not ok $n\n" && &abort("Couldn\'t write tempfile $TEMPFILE: $!");
22 print TMP $template_1;
24 print "ok $n\n"; $n++;
26 # (2) Build template from file
27 $template = new Text::Template ('type' => 'FILE', 'source' => $TEMPFILE);
28 if (defined($template)) {
31 print "not ok $n $Text::Template::ERROR\n";
35 # (3) Fill in template from file
38 We will put value of \$v (which is "abc") here -> abc
39 We will evaluate 1+1 here -> 2
43 We will put value of \$v (which is "abc") here -> ABC
44 We will evaluate 1+1 here -> 2
47 $text = $template->fill_in('package' => X);
48 if ($text eq $resultX) {
55 # (4) Fill in same template again
56 $text = $template->fill_in('package' => Y);
57 if ($text eq $resultY) {
66 # (5) Simple test of `fill_this_in'
67 $text = Text::Template->fill_this_in( $template_1, 'package' => X);
68 if ($text eq $resultX) {
75 # (6) test creation of template from filehandle
76 if (open (TMPL, "< $TEMPFILE")) {
77 $template = new Text::Template ('type' => 'FILEHANDLE',
79 if (defined($template)) {
82 print "not ok $n $Text::Template::ERROR\n";
86 # (7) test filling in of template from filehandle
87 $text = $template->fill_in('package' => X);
88 if ($text eq $resultX) {
95 # (8) test second fill_in on same template object
96 $text = $template->fill_in('package' => Y);
97 if ($text eq $resultY) {
105 print "not ok $n\n"; $n++;
106 print "not ok $n\n"; $n++;
107 print "not ok $n\n"; $n++;
111 # (9) test creation of template from array
112 $template = new Text::Template
115 'We will put value of $v (which is "abc") here -> {$v}',
117 'We will evaluate 1+1 here -> {1+1}',
120 if (defined($template)) {
123 print "not ok $n $Text::Template::ERROR\n";
127 # (10) test filling in of template from array
128 $text = $template->fill_in('package' => X);
129 if ($text eq $resultX) {
136 # (11) test second fill_in on same array template object
137 $text = $template->fill_in('package' => Y);
138 if ($text eq $resultY) {
142 print STDERR "$resultX\n---\n$text";
143 unless (!defined($text)) { print STDERR "ERROR: $Text::Template::ERROR\n"};
149 # (12) Make sure \ is working properly
150 # Test added for version 1.11
151 my $tmpl = Text::Template->new(TYPE => 'STRING',
152 SOURCE => 'B{"\\}"}C{"\\{"}D',
154 # This should fail if the \ are not interpreted properly.
155 my $text = $tmpl->fill_in();
156 print +($text eq "B}C{D" ? '' : 'not '), "ok $n\n";
159 # (13) Make sure \ is working properly
160 # Test added for version 1.11
161 $tmpl = Text::Template->new(TYPE => 'STRING',
162 SOURCE => qq{A{"\t"}B},
164 # Symptom of old problem: ALL \ were special in templates, so
165 # The lexer would return (A, PROGTEXT("t"), B), and the
166 # result text would be AtB instead of A(tab)B.
167 $text = $tmpl->fill_in();
169 print +($text eq "A\tB" ? '' : 'not '), "ok $n\n";
172 # (14-27) Make sure \ is working properly
173 # Test added for version 1.11
174 # This is a sort of general test.
175 my @tests = ('{""}' => '', # (14)
176 '{"}"}' => undef, # (15)
177 '{"\\}"}' => '}', # One backslash
178 '{"\\\\}"}' => undef, # Two backslashes
179 '{"\\\\\\}"}' => '}', # Three backslashes
180 '{"\\\\\\\\}"}' => undef, # Four backslashes
181 '{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
183 '{"\\x20"}' => ' ', # One backslash
184 '{"\\\\x20"}' => '\\x20', # Two backslashes
185 '{"\\\\\\x20"}' => '\\ ', # Three backslashes
186 '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
187 '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
188 '{"\\x20\\}"}' => ' }', # (27)
192 for ($i=0; $i<@tests; $i+=2) {
193 my $tmpl = Text::Template->new(TYPE => 'STRING',
194 SOURCE => $tests[$i],
196 my $text = $tmpl->fill_in;
197 my $result = $tests[$i+1];
198 my $ok = (! defined $text && ! defined $result
199 || $text eq $result);
201 print STDERR "($n) expected .$result., got .$text.\n";
203 print +($ok ? '' : 'not '), "ok $n\n";
208 # (28-30) I discovered that you can't pass a glob ref as your filehandle.
210 # (28) test creation of template from filehandle
211 if (open (TMPL, "< $TEMPFILE")) {
212 $template = new Text::Template ('type' => 'FILEHANDLE',
214 if (defined($template)) {
217 print "not ok $n $Text::Template::ERROR\n";
221 # (29) test filling in of template from filehandle
222 $text = $template->fill_in('package' => X);
223 if ($text eq $resultX) {
230 # (30) test second fill_in on same template object
231 $text = $template->fill_in('package' => Y);
232 if ($text eq $resultY) {
240 print "not ok $n\n"; $n++;
241 print "not ok $n\n"; $n++;
242 print "not ok $n\n"; $n++;
245 # (31) Test _scrubpkg for leakiness
246 $Text::Template::GEN0::test = 1;
247 Text::Template::_scrubpkg('Text::Template::GEN0');
248 if ($Text::Template::GEN0::test) {
256 END {unlink $TEMPFILE;}