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