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_ci
9e1051a39Sopenharmony_ciuse Test::More;
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ciunless (eval { require Safe; 1 }) {
12e1051a39Sopenharmony_ci    plan skip_all => 'Safe.pm is required for this test';
13e1051a39Sopenharmony_ci}
14e1051a39Sopenharmony_cielse {
15e1051a39Sopenharmony_ci    plan tests => 20;
16e1051a39Sopenharmony_ci}
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ciuse_ok 'Text::Template' or exit 1;
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_cimy ($BADOP, $FAILURE);
21e1051a39Sopenharmony_ciif ($^O eq 'MacOS') {
22e1051a39Sopenharmony_ci    $BADOP   = qq{};
23e1051a39Sopenharmony_ci    $FAILURE = q{};
24e1051a39Sopenharmony_ci}
25e1051a39Sopenharmony_cielse {
26e1051a39Sopenharmony_ci    $BADOP   = qq{kill 0};
27e1051a39Sopenharmony_ci    $FAILURE = q{Program fragment at line 1 delivered error ``kill trapped by operation mask''};
28e1051a39Sopenharmony_ci}
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciour $v = 119;
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_cimy $c = Safe->new or die;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_cimy $goodtemplate = q{This should succeed: { $v }};
35e1051a39Sopenharmony_cimy $goodoutput   = q{This should succeed: 119};
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cimy $template1 = Text::Template->new(type => 'STRING', source => $goodtemplate);
38e1051a39Sopenharmony_cimy $template2 = Text::Template->new(type => 'STRING', source => $goodtemplate);
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_cimy $text1 = $template1->fill_in();
41e1051a39Sopenharmony_ciok defined $text1;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_cimy $text2 = $template1->fill_in(SAFE => $c);
44e1051a39Sopenharmony_ciok defined $text2;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cimy $text3 = $template2->fill_in(SAFE => $c);
47e1051a39Sopenharmony_ciok defined $text3;
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci# (4) Safe and non-safe fills of different template objects with the
50e1051a39Sopenharmony_ci# same template text should yield the same result.
51e1051a39Sopenharmony_ci# print +($text1 eq $text3 ? '' : 'not '), "ok $n\n";
52e1051a39Sopenharmony_ci# (4) voided this test:  it's not true, because the unsafe fill
53e1051a39Sopenharmony_ci# uses package main, while the safe fill uses the secret safe package.
54e1051a39Sopenharmony_ci# We could alias the secret safe package to be identical to main,
55e1051a39Sopenharmony_ci# but that wouldn't be safe.  If you want the aliasing, you have to
56e1051a39Sopenharmony_ci# request it explicitly with `PACKAGE'.
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci# (5) Safe and non-safe fills of the same template object
59e1051a39Sopenharmony_ci# should yield the same result.
60e1051a39Sopenharmony_ci# (5) voided this test for the same reason as #4.
61e1051a39Sopenharmony_ci# print +($text1 eq $text2 ? '' : 'not '), "ok $n\n";
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci# (6) Make sure the output was actually correct
64e1051a39Sopenharmony_ciis $text1, $goodoutput;
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_cimy $badtemplate     = qq{This should fail: { $BADOP; 'NOFAIL' }};
67e1051a39Sopenharmony_cimy $badnosafeoutput = q{This should fail: NOFAIL};
68e1051a39Sopenharmony_cimy $badsafeoutput =
69e1051a39Sopenharmony_ci    q{This should fail: Program fragment delivered error ``kill trapped by operation mask at template line 1.''};
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci$template1 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
72e1051a39Sopenharmony_ciisa_ok $template1, 'Text::Template';
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci$template2 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
75e1051a39Sopenharmony_ciisa_ok $template2, 'Text::Template';
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci# none of these should fail
78e1051a39Sopenharmony_ci$text1 = $template1->fill_in();
79e1051a39Sopenharmony_ciok defined $text1;
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci$text2 = $template1->fill_in(SAFE => $c);
82e1051a39Sopenharmony_ciok defined $text2;
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ci$text3 = $template2->fill_in(SAFE => $c);
85e1051a39Sopenharmony_ciok defined $text3;
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_cimy $text4 = $template1->fill_in();
88e1051a39Sopenharmony_ciok defined $text4;
89e1051a39Sopenharmony_ci
90e1051a39Sopenharmony_ci# (11) text1 and text4 should be the same (using safe in between
91e1051a39Sopenharmony_ci# didn't change anything.)
92e1051a39Sopenharmony_ciis $text1, $text4;
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci# (12) text2 and text3 should be the same (same template text in different
95e1051a39Sopenharmony_ci# objects
96e1051a39Sopenharmony_ciis $text2, $text3;
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci# (13) text1 should yield badnosafeoutput
99e1051a39Sopenharmony_ciis $text1, $badnosafeoutput;
100e1051a39Sopenharmony_ci
101e1051a39Sopenharmony_ci# (14) text2 should yield badsafeoutput
102e1051a39Sopenharmony_ci$text2 =~ s/'kill'/kill/;    # 5.8.1 added quote marks around the op name
103e1051a39Sopenharmony_ciis $text2, $badsafeoutput;
104e1051a39Sopenharmony_ci
105e1051a39Sopenharmony_cimy $template = q{{$x=1}{$x+1}};
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci$template1 = Text::Template->new('type' => 'STRING', 'source' => $template);
108e1051a39Sopenharmony_ciisa_ok $template1, 'Text::Template';
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci$template2 = Text::Template->new('type' => 'STRING', 'source' => $template);
111e1051a39Sopenharmony_ciisa_ok $template2, 'Text::Template';
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci$text1 = $template1->fill_in();
114e1051a39Sopenharmony_ci$text2 = $template1->fill_in(SAFE => Safe->new);
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ci# (15) Do effects persist in safe compartments?
117e1051a39Sopenharmony_ciis $text1, $text2;
118e1051a39Sopenharmony_ci
119e1051a39Sopenharmony_ci# (16) Try the BROKEN routine in safe compartments
120e1051a39Sopenharmony_cisub my_broken {
121e1051a39Sopenharmony_ci    my %a = @_;
122e1051a39Sopenharmony_ci    $a{error} =~ s/ at.*//s;
123e1051a39Sopenharmony_ci    "OK! text:$a{text} error:$a{error} lineno:$a{lineno} arg:$a{arg}";
124e1051a39Sopenharmony_ci}
125e1051a39Sopenharmony_ci
126e1051a39Sopenharmony_cimy $templateB = Text::Template->new(TYPE => 'STRING', SOURCE => '{die}');
127e1051a39Sopenharmony_ciisa_ok $templateB, 'Text::Template';
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ci$text1 = $templateB->fill_in(
130e1051a39Sopenharmony_ci    BROKEN     => \&my_broken,
131e1051a39Sopenharmony_ci    BROKEN_ARG => 'barg',
132e1051a39Sopenharmony_ci    SAFE       => Safe->new);
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_cimy $result1 = qq{OK! text:die error:Died lineno:1 arg:barg};
135e1051a39Sopenharmony_ciis $text1, $result1;
136