3 # Tests of basic, essential functionality
8 use Test::More tests => 34;
11 my $tmpfile = File::Temp->new;
13 use_ok 'Text::Template' or exit 1;
15 $X::v = $Y::v = 0; # Suppress `var used only once'
17 my $template_1 = <<EOM;
18 We will put value of \$v (which is "abc") here -> {\$v}
19 We will evaluate 1+1 here -> {1 + 1}
22 # (1) Construct temporary template file for testing
24 my $TEMPFILE = $tmpfile->filename;
27 open my $tmp, '>', $TEMPFILE
28 or die "Couldn't write tempfile $TEMPFILE: $!";
30 print $tmp $template_1;
39 # (2) Build template from file
40 my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
41 ok(defined $template) or diag $Text::Template::ERROR;
43 # (3) Fill in template from file
46 We will put value of \$v (which is "abc") here -> abc
47 We will evaluate 1+1 here -> 2
51 We will put value of \$v (which is "abc") here -> ABC
52 We will evaluate 1+1 here -> 2
55 my $text = $template->fill_in('package' => 'X');
58 # (4) Fill in same template again
59 $text = $template->fill_in('package' => 'Y');
62 # (5) Simple test of `fill_this_in'
63 $text = Text::Template->fill_this_in($template_1, 'package' => 'X');
66 # (6) test creation of template from filehandle
67 open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
69 $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
70 ok defined $template or diag $Text::Template::ERROR;
72 # (7) test filling in of template from filehandle
73 $text = $template->fill_in('package' => 'X');
76 # (8) test second fill_in on same template object
77 $text = $template->fill_in('package' => 'Y');
82 # (9) test creation of template from array
83 $template = Text::Template->new(
86 'We will put value of $v (which is "abc") here -> {$v}', "\n",
87 'We will evaluate 1+1 here -> {1+1}', "\n"
91 ok defined $template; # or diag $Text::Template::ERROR;
93 # (10) test filling in of template from array
94 $text = $template->fill_in('package' => 'X');
97 # (11) test second fill_in on same array template object
98 $text = $template->fill_in('package' => 'Y');
101 # (12) Make sure \ is working properly
102 # Test added for version 1.11
103 $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D');
105 # This should fail if the \ are not interpreted properly.
106 $text = $tmpl->fill_in();
109 # (13) Make sure \ is working properly
110 # Test added for version 1.11
111 $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B});
113 # Symptom of old problem: ALL \ were special in templates, so
114 # The lexer would return (A, PROGTEXT("t"), B), and the
115 # result text would be AtB instead of A(tab)B.
116 $text = $tmpl->fill_in();
120 # (14-27) Make sure \ is working properly
121 # Test added for version 1.11
122 # This is a sort of general test.
125 '{"}"}' => undef, # (15)
126 '{"\\}"}' => '}', # One backslash
127 '{"\\\\}"}' => undef, # Two backslashes
128 '{"\\\\\\}"}' => '}', # Three backslashes
129 '{"\\\\\\\\}"}' => undef, # Four backslashes
130 '{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
132 '{"\\x20"}' => ' ', # One backslash
133 '{"\\\\x20"}' => '\\x20', # Two backslashes
134 '{"\\\\\\x20"}' => '\\ ', # Three backslashes
135 '{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
136 '{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
137 '{"\\x20\\}"}' => ' }', # (27)
140 while (my ($test, $result) = splice @tests, 0, 2) {
141 my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
142 my $text = $tmpl->fill_in;
144 ok(!defined $text && !defined $result || $text eq $result)
145 or diag "expected .$result. got .$text.";
148 # (28-30) I discovered that you can't pass a glob ref as your filehandle.
150 # (28) test creation of template from filehandle
152 ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
153 $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
154 ok(defined $template) or diag $Text::Template::ERROR;
156 # (29) test filling in of template from filehandle
157 $text = $template->fill_in('package' => 'X');
160 # (30) test second fill_in on same template object
161 $text = $template->fill_in('package' => 'Y');
166 # (31) Test _scrubpkg for leakiness
167 $Text::Template::GEN0::test = 1;
168 Text::Template::_scrubpkg('Text::Template::GEN0');
169 ok !($Text::Template::GEN0::test
170 || exists $Text::Template::GEN0::{test}
171 || exists $Text::Template::{'GEN0::'});
173 # that filename parameter works. we use BROKEN to verify this
174 $text = Text::Template->new(
176 SOURCE => 'Hello {1/0}'
177 )->fill_in(FILENAME => 'foo.txt');
179 like $text, qr/division by zero at foo\.txt line 1/;