xref: /freebsd-src/crypto/openssl/external/perl/Text-Template-1.56/t/delimiters.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#!perl
2*e0c4386eSCy Schubert#
3*e0c4386eSCy Schubert# Tests for user-specified delimiter functions
4*e0c4386eSCy Schubert# These tests first appeared in version 1.20.
5*e0c4386eSCy Schubert
6*e0c4386eSCy Schubertuse strict;
7*e0c4386eSCy Schubertuse warnings;
8*e0c4386eSCy Schubertuse Test::More tests => 19;
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse_ok 'Text::Template' or exit 1;
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubert# (1) Try a simple delimiter: <<..>>
13*e0c4386eSCy Schubert# First with the delimiters specified at object creation time
14*e0c4386eSCy Schubertour $V = $V = 119;
15*e0c4386eSCy Schubertmy $template  = q{The value of $V is <<$V>>.};
16*e0c4386eSCy Schubertmy $result    = q{The value of $V is 119.};
17*e0c4386eSCy Schubertmy $template1 = Text::Template->new(
18*e0c4386eSCy Schubert    TYPE       => 'STRING',
19*e0c4386eSCy Schubert    SOURCE     => $template,
20*e0c4386eSCy Schubert    DELIMITERS => [ '<<', '>>' ])
21*e0c4386eSCy Schubert        or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertmy $text = $template1->fill_in();
24*e0c4386eSCy Schubertis $text, $result;
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert# (2) Now with delimiter choice deferred until fill-in time.
27*e0c4386eSCy Schubert$template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
28*e0c4386eSCy Schubert$text = $template1->fill_in(DELIMITERS => [ '<<', '>>' ]);
29*e0c4386eSCy Schubertis $text, $result;
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert# (3) Now we'll try using regex metacharacters
32*e0c4386eSCy Schubert# First with the delimiters specified at object creation time
33*e0c4386eSCy Schubert$template  = q{The value of $V is [$V].};
34*e0c4386eSCy Schubert$template1 = Text::Template->new(
35*e0c4386eSCy Schubert    TYPE       => 'STRING',
36*e0c4386eSCy Schubert    SOURCE     => $template,
37*e0c4386eSCy Schubert    DELIMITERS => [ '[', ']' ])
38*e0c4386eSCy Schubert        or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert$text = $template1->fill_in();
41*e0c4386eSCy Schubertis $text, $result;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert# (4) Now with delimiter choice deferred until fill-in time.
44*e0c4386eSCy Schubert$template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
45*e0c4386eSCy Schubert$text = $template1->fill_in(DELIMITERS => [ '[', ']' ]);
46*e0c4386eSCy Schubertis $text, $result;
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert# (5-18) Make sure \ is working properly
49*e0c4386eSCy Schubert# (That is to say, it is ignored.)
50*e0c4386eSCy Schubert# These tests are similar to those in 01-basic.t.
51*e0c4386eSCy Schubertmy @tests = (
52*e0c4386eSCy Schubert    '{""}' => '',    # (5)
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubert    # Backslashes don't matter
55*e0c4386eSCy Schubert    '{"}"}'           => undef,
56*e0c4386eSCy Schubert    '{"\\}"}'         => undef,    # One backslash
57*e0c4386eSCy Schubert    '{"\\\\}"}'       => undef,    # Two backslashes
58*e0c4386eSCy Schubert    '{"\\\\\\}"}'     => undef,    # Three backslashes
59*e0c4386eSCy Schubert    '{"\\\\\\\\}"}'   => undef,    # Four backslashes (10)
60*e0c4386eSCy Schubert    '{"\\\\\\\\\\}"}' => undef,    # Five backslashes
61*e0c4386eSCy Schubert
62*e0c4386eSCy Schubert    # Backslashes are always passed directly to Perl
63*e0c4386eSCy Schubert    '{"x20"}'           => 'x20',
64*e0c4386eSCy Schubert    '{"\\x20"}'         => ' ',          # One backslash
65*e0c4386eSCy Schubert    '{"\\\\x20"}'       => '\\x20',      # Two backslashes
66*e0c4386eSCy Schubert    '{"\\\\\\x20"}'     => '\\ ',        # Three backslashes (15)
67*e0c4386eSCy Schubert    '{"\\\\\\\\x20"}'   => '\\\\x20',    # Four backslashes
68*e0c4386eSCy Schubert    '{"\\\\\\\\\\x20"}' => '\\\\ ',      # Five backslashes
69*e0c4386eSCy Schubert    '{"\\x20\\}"}'      => undef,        # (18)
70*e0c4386eSCy Schubert);
71*e0c4386eSCy Schubert
72*e0c4386eSCy Schubertwhile (my ($test, $result) = splice @tests, 0, 2) {
73*e0c4386eSCy Schubert    my $tmpl = Text::Template->new(
74*e0c4386eSCy Schubert        TYPE       => 'STRING',
75*e0c4386eSCy Schubert        SOURCE     => $test,
76*e0c4386eSCy Schubert        DELIMITERS => [ '{', '}' ]);
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert    my $text = $tmpl->fill_in;
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert    my $ok = (!defined $text && !defined $result || $text eq $result);
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert    ok($ok) or diag "expected .$result., got .$text.";
83*e0c4386eSCy Schubert}
84