xref: /freebsd/crypto/openssl/external/perl/Text-Template-1.56/t/basic.t (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!perl
2#
3# Tests of basic, essential functionality
4#
5
6use strict;
7use warnings;
8use Test::More tests => 34;
9use File::Temp;
10
11my $tmpfile = File::Temp->new;
12
13use_ok 'Text::Template' or exit 1;
14
15$X::v = $Y::v = 0;    # Suppress `var used only once'
16
17my $template_1 = <<EOM;
18We will put value of \$v (which is "abc") here -> {\$v}
19We will evaluate 1+1 here -> {1 + 1}
20EOM
21
22# (1) Construct temporary template file for testing
23# file operations
24my $TEMPFILE = $tmpfile->filename;
25
26eval {
27    open my $tmp, '>', $TEMPFILE
28        or die "Couldn't write tempfile $TEMPFILE: $!";
29
30    print $tmp $template_1;
31    close $tmp;
32
33    pass;
34};
35if ($@) {
36    fail $@;
37}
38
39# (2) Build template from file
40my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
41ok(defined $template) or diag $Text::Template::ERROR;
42
43# (3) Fill in template from file
44$X::v = "abc";
45my $resultX = <<EOM;
46We will put value of \$v (which is "abc") here -> abc
47We will evaluate 1+1 here -> 2
48EOM
49$Y::v = "ABC";
50my $resultY = <<EOM;
51We will put value of \$v (which is "abc") here -> ABC
52We will evaluate 1+1 here -> 2
53EOM
54
55my $text = $template->fill_in('package' => 'X');
56is $text, $resultX;
57
58# (4) Fill in same template again
59$text = $template->fill_in('package' => 'Y');
60is $text, $resultY;
61
62# (5) Simple test of `fill_this_in'
63$text = Text::Template->fill_this_in($template_1, 'package' => 'X');
64is $text, $resultX;
65
66# (6) test creation of template from filehandle
67open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
68
69$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
70ok defined $template or diag $Text::Template::ERROR;
71
72# (7) test filling in of template from filehandle
73$text = $template->fill_in('package' => 'X');
74is $text, $resultX;
75
76# (8) test second fill_in on same template object
77$text = $template->fill_in('package' => 'Y');
78is $text, $resultY;
79
80close $tmpl;
81
82# (9) test creation of template from array
83$template = Text::Template->new(
84    type   => 'ARRAY',
85    source => [
86        'We will put value of $v (which is "abc") here -> {$v}', "\n",
87        'We will evaluate 1+1 here -> {1+1}',                    "\n"
88    ]
89);
90
91ok defined $template;    # or diag $Text::Template::ERROR;
92
93# (10) test filling in of template from array
94$text = $template->fill_in('package' => 'X');
95is $text, $resultX;
96
97# (11) test second fill_in on same array template object
98$text = $template->fill_in('package' => 'Y');
99is $text, $resultY;
100
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');
104
105# This should fail if the \ are not interpreted properly.
106$text = $tmpl->fill_in();
107is $text, 'B}C{D';
108
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});
112
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();
117
118is $text, "A\tB";
119
120# (14-27) Make sure \ is working properly
121# Test added for version 1.11
122# This is a sort of general test.
123my @tests = (
124    '{""}'              => '',           # (14)
125    '{"}"}'             => undef,        # (15)
126    '{"\\}"}'           => '}',          # One backslash
127    '{"\\\\}"}'         => undef,        # Two backslashes
128    '{"\\\\\\}"}'       => '}',          # Three backslashes
129    '{"\\\\\\\\}"}'     => undef,        # Four backslashes
130    '{"\\\\\\\\\\}"}'   => '\}',         # Five backslashes  (20)
131    '{"x20"}'           => 'x20',
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)
138);
139
140while (my ($test, $result) = splice @tests, 0, 2) {
141    my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
142    my $text = $tmpl->fill_in;
143
144    ok(!defined $text && !defined $result || $text eq $result)
145        or diag "expected .$result. got .$text.";
146}
147
148# (28-30) I discovered that you can't pass a glob ref as your filehandle.
149# MJD 20010827
150# (28) test creation of template from filehandle
151$tmpl = undef;
152ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
153$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
154ok(defined $template) or diag $Text::Template::ERROR;
155
156# (29) test filling in of template from filehandle
157$text = $template->fill_in('package' => 'X');
158is $text, $resultX;
159
160# (30) test second fill_in on same template object
161$text = $template->fill_in('package' => 'Y');
162is $text, $resultY;
163
164close $tmpl;
165
166# (31) Test _scrubpkg for leakiness
167$Text::Template::GEN0::test = 1;
168Text::Template::_scrubpkg('Text::Template::GEN0');
169ok !($Text::Template::GEN0::test
170    || exists $Text::Template::GEN0::{test}
171    || exists $Text::Template::{'GEN0::'});
172
173# that filename parameter works. we use BROKEN to verify this
174$text = Text::Template->new(
175    TYPE   => 'string',
176    SOURCE => 'Hello {1/0}'
177)->fill_in(FILENAME => 'foo.txt');
178
179like $text, qr/division by zero at foo\.txt line 1/;
180