File: 05-safe2.t

package info (click to toggle)
libtext-template-perl 1.42-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 232 kB
  • ctags: 27
  • sloc: perl: 1,289; makefile: 46
file content (106 lines) | stat: -rw-r--r-- 2,824 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
#!perl
#
# test apparatus for Text::Template module
# still incomplete.

use lib '../blib/lib';
use Text::Template;

BEGIN {
  eval "use Safe";
  if ($@) {
    print "1..0\n";
    exit 0;
  }
}

die "This is the test program for Text::Template version 1.42.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
  unless $Text::Template::VERSION == 1.42;

print "1..12\n";
$n = 1;

$c = new Safe or die;

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

# How to effectively test the gensymming?

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

# (1) Default behavior: Inherit from calling package, `main' in this case.
$text = $t->fill_in();
print +($text eq 'package is main' ? '' : 'not '), "ok $n\n";
$n++;

# (2) When a package is specified, we should use that package instead.
$text = $t->fill_in(PACKAGE => 'Q');
print +($text eq 'package is Q' ? '' : 'not '), "ok $n\n";
$n++;

# (3) When no package is specified in safe mode, we should use the
# default safe root.
$text = $t->fill_in(SAFE => $c);
print +($text eq 'package is safe root' ? '' : 'not '), "ok $n\n";
$n++;

# (4) When a package is specified in safe mode, we should use the
# default safe root, after aliasing to the specified package
$text = $t->fill_in(SAFE => $c, PACKAGE => Q);
print +($text eq 'package is Q' ? '' : 'not '), "ok $n\n";
$n++;

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

# (5) First in default mode
$text = $t->fill_in(HASH => {H => 'good5'} );
print +($text eq 'hash is good5' ? '' : 'not '), "ok $n\n";
$n++;

# (6) Now in packages
$text = $t->fill_in(HASH => {H => 'good6'}, PACKAGE => 'Q' );
print +($text eq 'hash is good6' ? '' : 'not '), "ok $n\n";
$n++;

# (7) Now in the default root of the safe compartment
$text = $t->fill_in(HASH => {H => 'good7'}, SAFE => $c );
print +($text eq 'hash is good7' ? '' : 'not '), "ok $n\n";
$n++;

# (8) Now in the default root after aliasing to a package that
# got the hash stuffed in
$text = $t->fill_in(HASH => {H => 'good8'}, SAFE => $c, PACKAGE => 'Q2' );
print +($text eq 'hash is good8' ? '' : 'not '), "ok $n\n";
$n++;

# 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
print +(defined $H ? 'not ' : ''), "ok $n\n";
$H=$H;
$n++;

# (10) good6 was overwritten in test 7, so there's nothing to test for here.
print "ok $n\n";
$n++;

# (11) this value overwrote the one from test 6.
print +($Q::H eq 'good7' ? '' : 'not '), "ok $n\n";
$Q::H = $Q::H;
$n++;

# (12) 
print +($Q2::H eq 'good8' ? '' : 'not '), "ok $n\n";
$Q2::H = $Q2::H;
$n++;