xref: /freebsd/crypto/openssl/external/perl/Text-Template-1.56/t/hash.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#!perl
2*e0c4386eSCy Schubert#
3*e0c4386eSCy Schubert# test apparatus for Text::Template module
4*e0c4386eSCy Schubert# still incomplete.
5*e0c4386eSCy Schubert
6*e0c4386eSCy Schubertuse strict;
7*e0c4386eSCy Schubertuse warnings;
8*e0c4386eSCy Schubertuse Test::More tests => 13;
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse_ok 'Text::Template' or exit 1;
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubertmy $template = 'We will put value of $v (which is "good") here -> {$v}';
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubertmy $v = 'oops (main)';
15*e0c4386eSCy Schubert$Q::v = 'oops (Q)';
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubertmy $vars = { 'v' => \'good' };
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert# (1) Build template from string
20*e0c4386eSCy Schubert$template = Text::Template->new('type' => 'STRING', 'source' => $template);
21*e0c4386eSCy Schubertisa_ok $template, 'Text::Template';
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubert# (2) Fill in template in anonymous package
24*e0c4386eSCy Schubertmy $result2 = 'We will put value of $v (which is "good") here -> good';
25*e0c4386eSCy Schubertmy $text = $template->fill_in(HASH => $vars);
26*e0c4386eSCy Schubertis $text, $result2;
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert# (3) Did we clobber the main variable?
29*e0c4386eSCy Schubertis $v, 'oops (main)';
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert# (4) Fill in same template again
32*e0c4386eSCy Schubertmy $result4 = 'We will put value of $v (which is "good") here -> good';
33*e0c4386eSCy Schubert$text = $template->fill_in(HASH => $vars);
34*e0c4386eSCy Schubertis $text, $result4;
35*e0c4386eSCy Schubert
36*e0c4386eSCy Schubert# (5) Now with a package
37*e0c4386eSCy Schubertmy $result5 = 'We will put value of $v (which is "good") here -> good';
38*e0c4386eSCy Schubert$text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
39*e0c4386eSCy Schubertis $text, $result5;
40*e0c4386eSCy Schubert
41*e0c4386eSCy Schubert# (6) We expect to have clobbered the Q variable.
42*e0c4386eSCy Schubertis $Q::v, 'good';
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubert# (7) Now let's try it without a package
45*e0c4386eSCy Schubertmy $result7 = 'We will put value of $v (which is "good") here -> good';
46*e0c4386eSCy Schubert$text = $template->fill_in(HASH => $vars);
47*e0c4386eSCy Schubertis $text, $result7;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert# (8-11) Now what does it do when we pass a hash with undefined values?
50*e0c4386eSCy Schubert# Roy says it does something bad. (Added for 1.20.)
51*e0c4386eSCy Schubertmy $WARNINGS = 0;
52*e0c4386eSCy Schubert{
53*e0c4386eSCy Schubert    local $SIG{__WARN__} = sub { $WARNINGS++ };
54*e0c4386eSCy Schubert    local $^W = 1;    # Make sure this is on for this test
55*e0c4386eSCy Schubert    my $template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
56*e0c4386eSCy Schubert    my $result8   = 'We will put value of $v (which is "good") here -> good';
57*e0c4386eSCy Schubert    my $template  = Text::Template->new('type' => 'STRING', 'source' => $template8);
58*e0c4386eSCy Schubert    my $text = $template->fill_in(HASH => { 'v' => undef });
59*e0c4386eSCy Schubert
60*e0c4386eSCy Schubert    # (8) Did we generate a warning?
61*e0c4386eSCy Schubert    cmp_ok $WARNINGS, '==', 0;
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubert    # (9) Was the output correct?
64*e0c4386eSCy Schubert    is $text, $result8;
65*e0c4386eSCy Schubert
66*e0c4386eSCy Schubert    # (10-11) Let's try that again, with a twist this time
67*e0c4386eSCy Schubert    $WARNINGS = 0;
68*e0c4386eSCy Schubert    $text = $template->fill_in(HASH => [ { 'v' => 17 }, { 'v' => undef } ]);
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert    # (10) Did we generate a warning?
71*e0c4386eSCy Schubert    cmp_ok $WARNINGS, '==', 0;
72*e0c4386eSCy Schubert
73*e0c4386eSCy Schubert    # (11) Was the output correct?
74*e0c4386eSCy Schubert    SKIP: {
75*e0c4386eSCy Schubert        skip 'not supported before 5.005', 1 unless $] >= 5.005;
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert        is $text, $result8;
78*e0c4386eSCy Schubert    }
79*e0c4386eSCy Schubert}
80*e0c4386eSCy Schubert
81*e0c4386eSCy Schubert# (12) Now we'll test the multiple-hash option  (Added for 1.20.)
82*e0c4386eSCy Schubert$text = Text::Template::fill_in_string(q{$v: {$v}.  @v: [{"@v"}].},
83*e0c4386eSCy Schubert    HASH => [
84*e0c4386eSCy Schubert        { 'v' => 17 },
85*e0c4386eSCy Schubert        { 'v' => [ 'a', 'b', 'c' ] },
86*e0c4386eSCy Schubert        { 'v' => \23 }
87*e0c4386eSCy Schubert    ]
88*e0c4386eSCy Schubert);
89*e0c4386eSCy Schubert
90*e0c4386eSCy Schubertmy $result = q{$v: 23.  @v: [a b c].};
91*e0c4386eSCy Schubertis $text, $result;
92