Update the bundled external perl module Text-Template to version 1.56
[oweals/openssl.git] / external / perl / Text-Template-1.56 / t / basic.t
1 #!perl
2 #
3 # Tests of basic, essential functionality
4 #
5
6 use strict;
7 use warnings;
8 use Test::More tests => 34;
9 use File::Temp;
10
11 my $tmpfile = File::Temp->new;
12
13 use_ok 'Text::Template' or exit 1;
14
15 $X::v = $Y::v = 0;    # Suppress `var used only once'
16
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}
20 EOM
21
22 # (1) Construct temporary template file for testing
23 # file operations
24 my $TEMPFILE = $tmpfile->filename;
25
26 eval {
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 };
35 if ($@) {
36     fail $@;
37 }
38
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;
42
43 # (3) Fill in template from file
44 $X::v = "abc";
45 my $resultX = <<EOM;
46 We will put value of \$v (which is "abc") here -> abc
47 We will evaluate 1+1 here -> 2
48 EOM
49 $Y::v = "ABC";
50 my $resultY = <<EOM;
51 We will put value of \$v (which is "abc") here -> ABC
52 We will evaluate 1+1 here -> 2
53 EOM
54
55 my $text = $template->fill_in('package' => 'X');
56 is $text, $resultX;
57
58 # (4) Fill in same template again
59 $text = $template->fill_in('package' => 'Y');
60 is $text, $resultY;
61
62 # (5) Simple test of `fill_this_in'
63 $text = Text::Template->fill_this_in($template_1, 'package' => 'X');
64 is $text, $resultX;
65
66 # (6) test creation of template from filehandle
67 open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
68
69 $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
70 ok defined $template or diag $Text::Template::ERROR;
71
72 # (7) test filling in of template from filehandle
73 $text = $template->fill_in('package' => 'X');
74 is $text, $resultX;
75
76 # (8) test second fill_in on same template object
77 $text = $template->fill_in('package' => 'Y');
78 is $text, $resultY;
79
80 close $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
91 ok defined $template;    # or diag $Text::Template::ERROR;
92
93 # (10) test filling in of template from array
94 $text = $template->fill_in('package' => 'X');
95 is $text, $resultX;
96
97 # (11) test second fill_in on same array template object
98 $text = $template->fill_in('package' => 'Y');
99 is $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();
107 is $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
118 is $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.
123 my @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
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;
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;
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;
155
156 # (29) test filling in of template from filehandle
157 $text = $template->fill_in('package' => 'X');
158 is $text, $resultX;
159
160 # (30) test second fill_in on same template object
161 $text = $template->fill_in('package' => 'Y');
162 is $text, $resultY;
163
164 close $tmpl;
165
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::'});
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
179 like $text, qr/division by zero at foo\.txt line 1/;