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