• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!perl
2#
3# Tests of basic, essential functionality
4#
5
6use Text::Template;
7$X::v = $Y::v = 0;		# Suppress `var used only once'
8
9print "1..31\n";
10
11$n=1;
12
13$template_1 = <<EOM;
14We will put value of \$v (which is "abc") here -> {\$v}
15We will evaluate 1+1 here -> {1 + 1}
16EOM
17
18# (1) Construct temporary template file for testing
19# file operations
20$TEMPFILE = "tt$$";
21open(TMP, "> $TEMPFILE") or print "not ok $n\n" && &abort("Couldn\'t write tempfile $TEMPFILE: $!");
22print TMP $template_1;
23close TMP;
24print "ok $n\n"; $n++;
25
26# (2) Build template from file
27$template = new Text::Template ('type' => 'FILE', 'source' => $TEMPFILE);
28if (defined($template)) {
29  print "ok $n\n";
30} else {
31  print "not ok $n $Text::Template::ERROR\n";
32}
33$n++;
34
35# (3) Fill in template from file
36$X::v = "abc";
37$resultX = <<EOM;
38We will put value of \$v (which is "abc") here -> abc
39We will evaluate 1+1 here -> 2
40EOM
41$Y::v = "ABC";
42$resultY = <<EOM;
43We will put value of \$v (which is "abc") here -> ABC
44We will evaluate 1+1 here -> 2
45EOM
46
47$text = $template->fill_in('package' => X);
48if ($text eq $resultX) {
49  print "ok $n\n";
50} else {
51  print "not ok $n\n";
52}
53$n++;
54
55# (4) Fill in same template again
56$text = $template->fill_in('package' => Y);
57if ($text eq $resultY) {
58  print "ok $n\n";
59} else {
60  print "not ok $n\n";
61}
62$n++;
63
64
65
66# (5) Simple test of `fill_this_in'
67$text = Text::Template->fill_this_in( $template_1, 'package' => X);
68if ($text eq $resultX) {
69  print "ok $n\n";
70} else {
71  print "not ok $n\n";
72}
73$n++;
74
75# (6) test creation of template from filehandle
76if (open (TMPL, "< $TEMPFILE")) {
77  $template = new Text::Template ('type' => 'FILEHANDLE',
78				  'source' => *TMPL);
79  if (defined($template)) {
80    print "ok $n\n";
81  } else {
82    print "not ok $n $Text::Template::ERROR\n";
83  }
84  $n++;
85
86# (7) test filling in of template from filehandle
87  $text = $template->fill_in('package' => X);
88  if ($text eq $resultX) {
89    print "ok $n\n";
90  } else {
91    print "not ok $n\n";
92  }
93  $n++;
94
95# (8) test second fill_in on same template object
96  $text = $template->fill_in('package' => Y);
97  if ($text eq $resultY) {
98    print "ok $n\n";
99  } else {
100    print "not ok $n\n";
101  }
102  $n++;
103  close TMPL;
104} else {
105  print "not ok $n\n";  $n++;
106  print "not ok $n\n";  $n++;
107  print "not ok $n\n";  $n++;
108}
109
110
111# (9) test creation of template from array
112$template = new Text::Template
113    ('type' => 'ARRAY',
114     'source' => [
115		  'We will put value of $v (which is "abc") here -> {$v}',
116		  "\n",
117		  'We will evaluate 1+1 here -> {1+1}',
118		  "\n",
119		  ]);
120if (defined($template)) {
121  print "ok $n\n";
122} else {
123  print "not ok $n $Text::Template::ERROR\n";
124}
125$n++;
126
127# (10) test filling in of template from array
128$text = $template->fill_in('package' => X);
129if ($text eq $resultX) {
130  print "ok $n\n";
131} else {
132  print "not ok $n\n";
133}
134$n++;
135
136# (11) test second fill_in on same array template object
137$text = $template->fill_in('package' => Y);
138if ($text eq $resultY) {
139  print "ok $n\n";
140} else {
141  print "not ok $n\n";
142  print STDERR "$resultX\n---\n$text";
143  unless (!defined($text)) { print STDERR "ERROR: $Text::Template::ERROR\n"};
144}
145$n++;
146
147
148
149# (12) Make sure \ is working properly
150# Test added for version 1.11
151my $tmpl = Text::Template->new(TYPE => 'STRING',
152			       SOURCE => 'B{"\\}"}C{"\\{"}D',
153			       );
154# This should fail if the \ are not interpreted properly.
155my $text = $tmpl->fill_in();
156print +($text eq "B}C{D" ? '' : 'not '), "ok $n\n";
157$n++;
158
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},
163			   );
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();
168
169print +($text eq "A\tB" ? '' : 'not '), "ok $n\n";
170$n++;
171
172# (14-27) Make sure \ is working properly
173# Test added for version 1.11
174# This is a sort of general test.
175my @tests = ('{""}' => '',	# (14)
176	     '{"}"}' => undef,  # (15)
177	     '{"\\}"}' => '}',	# One backslash
178	     '{"\\\\}"}' => undef, # Two backslashes
179	     '{"\\\\\\}"}' => '}', # Three backslashes
180	     '{"\\\\\\\\}"}' => undef, # Four backslashes
181	     '{"\\\\\\\\\\}"}' => '\}', # Five backslashes  (20)
182	     '{"x20"}' => 'x20',
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)
189	    );
190
191my $i;
192for ($i=0; $i<@tests; $i+=2) {
193  my $tmpl = Text::Template->new(TYPE => 'STRING',
194				 SOURCE => $tests[$i],
195				);
196  my $text = $tmpl->fill_in;
197  my $result = $tests[$i+1];
198  my $ok = (! defined $text && ! defined $result
199	    || $text eq $result);
200  unless ($ok) {
201    print STDERR "($n) expected .$result., got .$text.\n";
202  }
203  print +($ok ? '' : 'not '), "ok $n\n";
204  $n++;
205}
206
207
208# (28-30) I discovered that you can't pass a glob ref as your filehandle.
209# MJD 20010827
210# (28) test creation of template from filehandle
211if (open (TMPL, "< $TEMPFILE")) {
212  $template = new Text::Template ('type' => 'FILEHANDLE',
213				  'source' => \*TMPL);
214  if (defined($template)) {
215    print "ok $n\n";
216  } else {
217    print "not ok $n $Text::Template::ERROR\n";
218  }
219  $n++;
220
221# (29) test filling in of template from filehandle
222  $text = $template->fill_in('package' => X);
223  if ($text eq $resultX) {
224    print "ok $n\n";
225  } else {
226    print "not ok $n\n";
227  }
228  $n++;
229
230# (30) test second fill_in on same template object
231  $text = $template->fill_in('package' => Y);
232  if ($text eq $resultY) {
233    print "ok $n\n";
234  } else {
235    print "not ok $n\n";
236  }
237  $n++;
238  close TMPL;
239} else {
240  print "not ok $n\n";  $n++;
241  print "not ok $n\n";  $n++;
242  print "not ok $n\n";  $n++;
243}
244
245# (31) Test _scrubpkg for leakiness
246$Text::Template::GEN0::test = 1;
247Text::Template::_scrubpkg('Text::Template::GEN0');
248if ($Text::Template::GEN0::test) {
249  print "not ok $n\n";
250} else {
251  print "ok $n\n";
252}
253$n++;
254
255
256END {unlink $TEMPFILE;}
257
258exit;
259
260
261
262
263sub abort {
264  unlink $TEMPFILE;
265  die $_[0];
266}
267