File: basic.t

package info (click to toggle)
libtext-template-perl 1.61-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 288 kB
  • sloc: perl: 1,320; makefile: 2
file content (179 lines) | stat: -rwxr-xr-x 5,314 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!perl
#
# Tests of basic, essential functionality
#

use strict;
use warnings;
use Test::More tests => 34;
use File::Temp;

my $tmpfile = File::Temp->new;

use_ok 'Text::Template' or exit 1;

$X::v = $Y::v = 0;    # Suppress `var used only once'

my $template_1 = <<EOM;
We will put value of \$v (which is "abc") here -> {\$v}
We will evaluate 1+1 here -> {1 + 1}
EOM

# (1) Construct temporary template file for testing
# file operations
my $TEMPFILE = $tmpfile->filename;

eval {
    open my $tmp, '>', $TEMPFILE
        or die "Couldn't write tempfile $TEMPFILE: $!";

    print $tmp $template_1;
    close $tmp;

    pass;
};
if ($@) {
    fail $@;
}

# (2) Build template from file
my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
ok(defined $template) or diag $Text::Template::ERROR;

# (3) Fill in template from file
$X::v = "abc";
my $resultX = <<EOM;
We will put value of \$v (which is "abc") here -> abc
We will evaluate 1+1 here -> 2
EOM
$Y::v = "ABC";
my $resultY = <<EOM;
We will put value of \$v (which is "abc") here -> ABC
We will evaluate 1+1 here -> 2
EOM

my $text = $template->fill_in('package' => 'X');
is $text, $resultX;

# (4) Fill in same template again
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;

# (5) Simple test of `fill_this_in'
$text = Text::Template->fill_this_in($template_1, 'package' => 'X');
is $text, $resultX;

# (6) test creation of template from filehandle
open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";

$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
ok defined $template or diag $Text::Template::ERROR;

# (7) test filling in of template from filehandle
$text = $template->fill_in('package' => 'X');
is $text, $resultX;

# (8) test second fill_in on same template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;

close $tmpl;

# (9) test creation of template from array
$template = Text::Template->new(
    type   => 'ARRAY',
    source => [
        'We will put value of $v (which is "abc") here -> {$v}', "\n",
        'We will evaluate 1+1 here -> {1+1}',                    "\n"
    ]
);

ok defined $template;    # or diag $Text::Template::ERROR;

# (10) test filling in of template from array
$text = $template->fill_in('package' => 'X');
is $text, $resultX;

# (11) test second fill_in on same array template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;

# (12) Make sure \ is working properly
# Test added for version 1.11
$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D');

# This should fail if the \ are not interpreted properly.
$text = $tmpl->fill_in();
is $text, 'B}C{D';

# (13) Make sure \ is working properly
# Test added for version 1.11
$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B});

# Symptom of old problem:  ALL \ were special in templates, so
# The lexer would return (A, PROGTEXT("t"), B), and the
# result text would be AtB instead of A(tab)B.
$text = $tmpl->fill_in();

is $text, "A\tB";

# (14-27) Make sure \ is working properly
# Test added for version 1.11
# This is a sort of general test.
my @tests = (
    '{""}'              => '',           # (14)
    '{"}"}'             => undef,        # (15)
    '{"\\}"}'           => '}',          # One backslash
    '{"\\\\}"}'         => undef,        # Two backslashes
    '{"\\\\\\}"}'       => '}',          # Three backslashes
    '{"\\\\\\\\}"}'     => undef,        # Four backslashes
    '{"\\\\\\\\\\}"}'   => '\}',         # Five backslashes  (20)
    '{"x20"}'           => 'x20',
    '{"\\x20"}'         => ' ',          # One backslash
    '{"\\\\x20"}'       => '\\x20',      # Two backslashes
    '{"\\\\\\x20"}'     => '\\ ',        # Three backslashes
    '{"\\\\\\\\x20"}'   => '\\\\x20',    # Four backslashes  (25)
    '{"\\\\\\\\\\x20"}' => '\\\\ ',      # Five backslashes
    '{"\\x20\\}"}'      => ' }',         # (27)
);

while (my ($test, $result) = splice @tests, 0, 2) {
    my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
    my $text = $tmpl->fill_in;

    ok(!defined $text && !defined $result || $text eq $result)
        or diag "expected .$result. got .$text.";
}

# (28-30) I discovered that you can't pass a glob ref as your filehandle.
# MJD 20010827
# (28) test creation of template from filehandle
$tmpl = undef;
ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
ok(defined $template) or diag $Text::Template::ERROR;

# (29) test filling in of template from filehandle
$text = $template->fill_in('package' => 'X');
is $text, $resultX;

# (30) test second fill_in on same template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;

close $tmpl;

# (31) Test _scrubpkg for leakiness
$Text::Template::GEN0::test = 1;
Text::Template::_scrubpkg('Text::Template::GEN0');
ok !($Text::Template::GEN0::test
    || exists $Text::Template::GEN0::{test}
    || exists $Text::Template::{'GEN0::'});

# that filename parameter works. we use BROKEN to verify this
$text = Text::Template->new(
    TYPE   => 'string',
    SOURCE => 'Hello {1/0}'
)->fill_in(FILENAME => 'foo.txt');

like $text, qr/division by zero at foo\.txt line 1/;