02-hash.t
3.14 KB
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
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..12\n";
$n=1;
$template = 'We will put value of $v (which is "good") here -> {$v}';
$v = 'oops (main)';
$Q::v = 'oops (Q)';
$vars = { 'v' => \'good' };
# (1) Build template from string
$template = new Text::Template ('type' => 'STRING', 'source' => $template);
print +($template ? '' : 'not '), "ok $n\n";
$n++;
# (2) Fill in template in anonymous package
$result2 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result2 ? '' : 'not '), "ok $n\n";
$n++;
# (3) Did we clobber the main variable?
print +($v eq 'oops (main)' ? '' : 'not '), "ok $n\n";
$n++;
# (4) Fill in same template again
$result4 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result4 ? '' : 'not '), "ok $n\n";
$n++;
# (5) Now with a package
$result5 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
print +($text eq $result5 ? '' : 'not '), "ok $n\n";
$n++;
# (6) We expect to have clobbered the Q variable.
print +($Q::v eq 'good' ? '' : 'not '), "ok $n\n";
$n++;
# (7) Now let's try it without a package
$result7 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result7 ? '' : 'not '), "ok $n\n";
$n++;
# (8-11) Now what does it do when we pass a hash with undefined values?
# Roy says it does something bad. (Added for 1.20.)
my $WARNINGS = 0;
{
local $SIG{__WARN__} = sub {$WARNINGS++};
local $^W = 1; # Make sure this is on for this test
$template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
$result8 = 'We will put value of $v (which is "good") here -> good';
my $template =
new Text::Template ('type' => 'STRING', 'source' => $template8);
my $text = $template->fill_in(HASH => {'v' => undef});
# (8) Did we generate a warning?
print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
$n++;
# (9) Was the output correct?
print +($text eq $result8 ? '' : 'not '), "ok $n\n";
$n++;
# (10-11) Let's try that again, with a twist this time
$WARNINGS = 0;
$text = $template->fill_in(HASH => [{'v' => 17}, {'v' => undef}]);
# (10) Did we generate a warning?
print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
$n++;
# (11) Was the output correct?
if ($] < 5.005) {
print "ok $n # skipped -- not supported before 5.005\n";
} else {
print +($text eq $result8 ? '' : 'not '), "ok $n\n";
}
$n++;
}
# (12) Now we'll test the multiple-hash option (Added for 1.20.)
$text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].},
HASH => [{'v' => 17},
{'v' => ['a', 'b', 'c']},
{'v' => \23},
]);
$result = q{$v: 23. @v: [a b c].};
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
exit;