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