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;
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ciunless (eval { require Safe; 1 }) {
11e1051a39Sopenharmony_ci    plan skip_all => 'Safe.pm is required for this test';
12e1051a39Sopenharmony_ci}
13e1051a39Sopenharmony_cielse {
14e1051a39Sopenharmony_ci    plan tests => 12;
15e1051a39Sopenharmony_ci}
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciuse_ok 'Text::Template' or exit 1;
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cimy $c = Safe->new or die;
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci# Test handling of packages and importing.
22e1051a39Sopenharmony_ci$c->reval('$P = "safe root"');
23e1051a39Sopenharmony_ciour $P = 'main';
24e1051a39Sopenharmony_ci$Q::P = $Q::P = 'Q';
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci# How to effectively test the gensymming?
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cimy $t = Text::Template->new(
29e1051a39Sopenharmony_ci    TYPE   => 'STRING',
30e1051a39Sopenharmony_ci    SOURCE => 'package is {$P}') or die;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci# (1) Default behavior: Inherit from calling package, `main' in this case.
33e1051a39Sopenharmony_cimy $text = $t->fill_in();
34e1051a39Sopenharmony_ciis $text, 'package is main';
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci# (2) When a package is specified, we should use that package instead.
37e1051a39Sopenharmony_ci$text = $t->fill_in(PACKAGE => 'Q');
38e1051a39Sopenharmony_ciis $text, 'package is Q';
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_ci# (3) When no package is specified in safe mode, we should use the
41e1051a39Sopenharmony_ci# default safe root.
42e1051a39Sopenharmony_ci$text = $t->fill_in(SAFE => $c);
43e1051a39Sopenharmony_ciis $text, 'package is safe root';
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci# (4) When a package is specified in safe mode, we should use the
46e1051a39Sopenharmony_ci# default safe root, after aliasing to the specified package
47e1051a39Sopenharmony_ciTODO: {
48e1051a39Sopenharmony_ci    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
49e1051a39Sopenharmony_ci    $text = $t->fill_in(SAFE => $c, PACKAGE => 'Q');
50e1051a39Sopenharmony_ci    is $text, 'package is Q';
51e1051a39Sopenharmony_ci}
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci# Now let's see if hash vars are installed properly into safe templates
54e1051a39Sopenharmony_ci$t = Text::Template->new(
55e1051a39Sopenharmony_ci    TYPE   => 'STRING',
56e1051a39Sopenharmony_ci    SOURCE => 'hash is {$H}') or die;
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci# (5) First in default mode
59e1051a39Sopenharmony_ci$text = $t->fill_in(HASH => { H => 'good5' });
60e1051a39Sopenharmony_ciis $text, 'hash is good5';
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci# suppress "once" warnings
63e1051a39Sopenharmony_ci$Q::H = $Q2::H = undef;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci# (6) Now in packages
66e1051a39Sopenharmony_ci$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q');
67e1051a39Sopenharmony_ciis $text, 'hash is good6';
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci# (7) Now in the default root of the safe compartment
70e1051a39Sopenharmony_ciTODO: {
71e1051a39Sopenharmony_ci    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
72e1051a39Sopenharmony_ci    $text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c);
73e1051a39Sopenharmony_ci    is $text, 'hash is good7';
74e1051a39Sopenharmony_ci}
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci# (8) Now in the default root after aliasing to a package that
77e1051a39Sopenharmony_ci# got the hash stuffed in
78e1051a39Sopenharmony_ciour $H;
79e1051a39Sopenharmony_ciTODO: {
80e1051a39Sopenharmony_ci    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
81e1051a39Sopenharmony_ci    $text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2');
82e1051a39Sopenharmony_ci    is $text, 'hash is good8';
83e1051a39Sopenharmony_ci}
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci# Now let's make sure that none of the packages leaked on each other.
86e1051a39Sopenharmony_ci# (9) This var should NOT have been installed into the main package
87e1051a39Sopenharmony_ciok !defined $H;
88e1051a39Sopenharmony_ci$H = $H;
89e1051a39Sopenharmony_ci
90e1051a39Sopenharmony_ci# (11) this value overwrote the one from test 6.
91e1051a39Sopenharmony_ciis $Q::H, 'good7';
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ci# (12)
94e1051a39Sopenharmony_ciis $Q2::H, 'good8';
95