Update the bundled external perl module Text-Template to version 1.56
[oweals/openssl.git] / external / perl / Text-Template-1.56 / t / safe3.t
1 #!perl
2 #
3 # test apparatus for Text::Template module
4
5 use strict;
6 use warnings;
7 use Test::More;
8
9 unless (eval { require Safe; 1 }) {
10     plan skip_all => 'Safe.pm is required for this test';
11 }
12 else {
13     plan tests => 4;
14 }
15
16 use_ok 'Text::Template' or exit 1;
17
18 # Test the OUT feature with safe compartments
19
20 my $template = q{
21 This line should have a 3: {1+2}
22
23 This line should have several numbers:
24 { $t = ''; foreach $n (1 .. 20) { $t .= $n . ' ' } $t }
25 };
26
27 my $templateOUT = q{
28 This line should have a 3: { $OUT = 1+2 }
29
30 This line should have several numbers:
31 { foreach $n (1 .. 20) { $OUT .= $n . ' ' } }
32 };
33
34 my $c = Safe->new;
35
36 # Build templates from string
37 $template = Text::Template->new(
38     type   => 'STRING',
39     source => $template,
40     SAFE   => $c) or die;
41
42 $templateOUT = Text::Template->new(
43     type   => 'STRING',
44     source => $templateOUT,
45     SAFE    => $c) or die;
46
47 # Fill in templates
48 my $text = $template->fill_in()
49     or die;
50 my $textOUT = $templateOUT->fill_in()
51     or die;
52
53 # (1) They should be the same
54 is $text, $textOUT;
55
56 # (2-3)  "Joel Appelbaum" <joel@orbz.com> <000701c0ac2c$aed1d6e0$0201a8c0@prime>
57 # "Contrary to the documentation the $OUT variable is not always
58 # undefined at the start of each program fragment.  The $OUT variable
59 # is never undefined after it is used once if you are using the SAFE
60 # option.  The result is that every fragment after the fragment that
61 # $OUT was used in is replaced by the old $OUT value instead of the
62 # result of the fragment.  This holds true even after the
63 # Text::Template object goes out of scope and a new one is created!"
64 #
65 # Also reported by Daini Xie.
66
67 {
68     my $template = q{{$OUT = 'x'}y{$OUT .= 'z'}};
69     my $expected = "xyz";
70     my $s        = Safe->new;
71     my $o        = Text::Template->new(
72         type   => 'string',
73         source => $template);
74
75     for (1 .. 2) {
76         my $r = $o->fill_in(SAFE => $s);
77
78         is $r, $expected;
79     }
80 }