Skip to content
Browse files

more template exception tests

  • Loading branch information...
1 parent d7bd288 commit df01b48e65979beb748c587566e5ca509c4378b0 @kraih committed May 11, 2012
Showing with 31 additions and 15 deletions.
  1. +13 −11 lib/Mojo/Template.pm
  2. +18 −4 t/mojo/template.t
View
24 lib/Mojo/Template.pm
@@ -674,9 +674,9 @@ Compile template.
Interpret template.
# Reuse template
- say $mt->render('Result <%= $_[0] + $_[1] %>', 1, 1);
- say $mt->interpret(2, 2);
- say $mt->interpret(3, 3);
+ say $mt->render('Hello <%= $_[0] %>!', 'Bender');
+ say $mt->interpret('Fry');
+ say $mt->interpret('Leela');
=head2 C<parse>
@@ -691,27 +691,29 @@ Parse template.
Render template.
+ say $mt->render('Hello <%= $_[0] %>!', 'Bender');
+
=head2 C<render_file>
- my $output = $mt->render_file($template_file);
- my $output = $mt->render_file($template_file, @args);
+ my $output = $mt->render_file('/tmp/foo.mt');
+ my $output = $mt->render_file('/tmp/foo.mt', @args);
Render template file.
=head2 C<render_file_to_file>
- my $exception = $mt->render_file_to_file($template_file, $output_file);
+ my $exception = $mt->render_file_to_file('/tmp/foo.mt', '/tmp/foo.txt');
my $exception
- = $mt->render_file_to_file($template_file, $output_file, @args);
+ = $mt->render_file_to_file('/tmp/foo.mt', '/tmp/foo.txt', @args);
-Render template file to a specific file.
+Render template file to another file.
=head2 C<render_to_file>
- my $exception = $mt->render_to_file($template, $output_file);
- my $exception = $mt->render_to_file($template, $output_file, @args);
+ my $exception = $mt->render_to_file($template, '/tmp/foo.txt');
+ my $exception = $mt->render_to_file($template, '/tmp/foo.txt', @args);
-Render template to a specific file.
+Render template to a file.
=head1 SEE ALSO
View
22 t/mojo/template.t
@@ -17,7 +17,7 @@ use Mojo::Base -strict;
use utf8;
-use Test::More tests => 204;
+use Test::More tests => 214;
# "When I held that gun in my hand, I felt a surge of power...
# like God must feel when he's holding a gun."
@@ -1050,7 +1050,6 @@ like "$output", qr/exception\.mt line 2/, 'right result';
# Exception in file (different name)
$mt = Mojo::Template->new;
-$file = catfile(splitdir($FindBin::Bin), qw(lib exception.mt));
$output = $mt->name('foo.mt')->render_file($file);
isa_ok $output, 'Mojo::Exception', 'right exception';
like $output->message, qr/foo\.mt line 2/, 'message contains file name';
@@ -1062,19 +1061,34 @@ is $output->lines_after->[0]->[0], 3, 'right number';
is $output->lines_after->[0]->[1], '123', 'right line';
like "$output", qr/foo\.mt line 2/, 'right result';
+# Exception in file (rendering to file)
+$mt = Mojo::Template->new;
+my $dir = File::Temp::tempdir(CLEANUP => 1);
+my $file2 = catfile $dir, 'test1.mt';
+$output = $mt->render_file_to_file($file, $file2);
+ok !-e $file2, 'file has not been rendered';
+isa_ok $output, 'Mojo::Exception', 'right exception';
+like $output->message, qr/exception\.mt line 2/, 'message contains file name';
+is $output->lines_before->[0]->[0], 1, 'right number';
+is $output->lines_before->[0]->[1], 'test', 'right line';
+is $output->line->[0], 2, 'right number';
+is $output->line->[1], '% die;', 'right line';
+is $output->lines_after->[0]->[0], 3, 'right number';
+is $output->lines_after->[0]->[1], '123', 'right line';
+like "$output", qr/exception\.mt line 2/, 'right result';
+
# File to file with utf8 data
$mt = Mojo::Template->new;
$mt->tag_start('[$-');
$mt->tag_end('-$]');
-my $dir = File::Temp::tempdir(CLEANUP => 1);
$file = catfile $dir, 'test.mt';
is $mt->render_to_file(<<"EOF", $file), undef, 'file rendered';
<% my \$i = 23; %> foo bar
\x{df}\x{0100}bar\x{263a} <%= \$i %>
test
EOF
$mt = Mojo::Template->new;
-my $file2 = catfile $dir, 'test2.mt';
+$file2 = catfile $dir, 'test2.mt';
is $mt->render_file_to_file($file, $file2), undef, 'file rendered to file';
$mt = Mojo::Template->new;
$output = $mt->render_file($file2);

0 comments on commit df01b48

Please sign in to comment.
Something went wrong with that request. Please try again.