123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- use strict;
- use warnings;
- use Test::More tests => 34;
- use File::Temp;
- my $tmpfile = File::Temp->new;
- use_ok 'Text::Template' or exit 1;
- $X::v = $Y::v = 0;
- my $template_1 = <<EOM;
- We will put value of \$v (which is "abc") here -> {\$v}
- We will evaluate 1+1 here -> {1 + 1}
- EOM
- my $TEMPFILE = $tmpfile->filename;
- eval {
- open my $tmp, '>', $TEMPFILE
- or die "Couldn't write tempfile $TEMPFILE: $!";
- print $tmp $template_1;
- close $tmp;
- pass;
- };
- if ($@) {
- fail $@;
- }
- my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
- ok(defined $template) or diag $Text::Template::ERROR;
- $X::v = "abc";
- my $resultX = <<EOM;
- We will put value of \$v (which is "abc") here -> abc
- We will evaluate 1+1 here -> 2
- EOM
- $Y::v = "ABC";
- my $resultY = <<EOM;
- We will put value of \$v (which is "abc") here -> ABC
- We will evaluate 1+1 here -> 2
- EOM
- my $text = $template->fill_in('package' => 'X');
- is $text, $resultX;
- $text = $template->fill_in('package' => 'Y');
- is $text, $resultY;
- $text = Text::Template->fill_this_in($template_1, 'package' => 'X');
- is $text, $resultX;
- open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
- $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
- ok defined $template or diag $Text::Template::ERROR;
- $text = $template->fill_in('package' => 'X');
- is $text, $resultX;
- $text = $template->fill_in('package' => 'Y');
- is $text, $resultY;
- close $tmpl;
- $template = Text::Template->new(
- type => 'ARRAY',
- source => [
- 'We will put value of $v (which is "abc") here -> {$v}', "\n",
- 'We will evaluate 1+1 here -> {1+1}', "\n"
- ]
- );
- ok defined $template;
- $text = $template->fill_in('package' => 'X');
- is $text, $resultX;
- $text = $template->fill_in('package' => 'Y');
- is $text, $resultY;
- $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D');
- $text = $tmpl->fill_in();
- is $text, 'B}C{D';
- $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B});
- $text = $tmpl->fill_in();
- is $text, "A\tB";
- my @tests = (
- '{""}' => '',
- '{"}"}' => undef,
- '{"\\}"}' => '}',
- '{"\\\\}"}' => undef,
- '{"\\\\\\}"}' => '}',
- '{"\\\\\\\\}"}' => undef,
- '{"\\\\\\\\\\}"}' => '\}',
- '{"x20"}' => 'x20',
- '{"\\x20"}' => ' ',
- '{"\\\\x20"}' => '\\x20',
- '{"\\\\\\x20"}' => '\\ ',
- '{"\\\\\\\\x20"}' => '\\\\x20',
- '{"\\\\\\\\\\x20"}' => '\\\\ ',
- '{"\\x20\\}"}' => ' }',
- );
- while (my ($test, $result) = splice @tests, 0, 2) {
- my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
- my $text = $tmpl->fill_in;
- ok(!defined $text && !defined $result || $text eq $result)
- or diag "expected .$result. got .$text.";
- }
- $tmpl = undef;
- ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
- $template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
- ok(defined $template) or diag $Text::Template::ERROR;
- $text = $template->fill_in('package' => 'X');
- is $text, $resultX;
- $text = $template->fill_in('package' => 'Y');
- is $text, $resultY;
- close $tmpl;
- $Text::Template::GEN0::test = 1;
- Text::Template::_scrubpkg('Text::Template::GEN0');
- ok !($Text::Template::GEN0::test
- || exists $Text::Template::GEN0::{test}
- || exists $Text::Template::{'GEN0::'});
- $text = Text::Template->new(
- TYPE => 'string',
- SOURCE => 'Hello {1/0}'
- )->fill_in(FILENAME => 'foo.txt');
- like $text, qr/division by zero at foo\.txt line 1/;
|