File: safe2.t

package info (click to toggle)
libtext-template-perl 1.61-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 288 kB
  • sloc: perl: 1,320; makefile: 2
file content (94 lines) | stat: -rwxr-xr-x 2,568 bytes parent folder | download | duplicates (12)
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
#!perl
#
# test apparatus for Text::Template module
# still incomplete.

use strict;
use warnings;
use Test::More;

unless (eval { require Safe; 1 }) {
    plan skip_all => 'Safe.pm is required for this test';
}
else {
    plan tests => 12;
}

use_ok 'Text::Template' or exit 1;

my $c = Safe->new or die;

# Test handling of packages and importing.
$c->reval('$P = "safe root"');
our $P = 'main';
$Q::P = $Q::P = 'Q';

# How to effectively test the gensymming?

my $t = Text::Template->new(
    TYPE   => 'STRING',
    SOURCE => 'package is {$P}') or die;

# (1) Default behavior: Inherit from calling package, `main' in this case.
my $text = $t->fill_in();
is $text, 'package is main';

# (2) When a package is specified, we should use that package instead.
$text = $t->fill_in(PACKAGE => 'Q');
is $text, 'package is Q';

# (3) When no package is specified in safe mode, we should use the
# default safe root.
$text = $t->fill_in(SAFE => $c);
is $text, 'package is safe root';

# (4) When a package is specified in safe mode, we should use the
# default safe root, after aliasing to the specified package
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(SAFE => $c, PACKAGE => 'Q');
    is $text, 'package is Q';
}

# Now let's see if hash vars are installed properly into safe templates
$t = Text::Template->new(
    TYPE   => 'STRING',
    SOURCE => 'hash is {$H}') or die;

# (5) First in default mode
$text = $t->fill_in(HASH => { H => 'good5' });
is $text, 'hash is good5';

# suppress "once" warnings
$Q::H = $Q2::H = undef;

# (6) Now in packages
$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q');
is $text, 'hash is good6';

# (7) Now in the default root of the safe compartment
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c);
    is $text, 'hash is good7';
}

# (8) Now in the default root after aliasing to a package that
# got the hash stuffed in
our $H;
TODO: {
    local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
    $text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2');
    is $text, 'hash is good8';
}

# Now let's make sure that none of the packages leaked on each other.
# (9) This var should NOT have been installed into the main package
ok !defined $H;
$H = $H;

# (11) this value overwrote the one from test 6.
is $Q::H, 'good7';

# (12)
is $Q2::H, 'good8';