Skip to content

Commit

Permalink
Merge pull request #6 from heikojansen/patch-1
Browse files Browse the repository at this point in the history
Test Xslate as default handler; all previously available tests now also pass when Xslate is selected as default handler for the Mojolicious renderer.
  • Loading branch information
heikojansen committed Apr 26, 2016
2 parents 6b66783 + 213e6f6 commit 9f734cf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
26 changes: 22 additions & 4 deletions lib/MojoX/Renderer/Xslate.pm
Expand Up @@ -56,16 +56,34 @@ sub _render {
|| $renderer->template_name($options);
my %params = (%{$c->stash}, c => $c);

my $orig_err = $@;
my $xslate_err;

local $@;
if (defined(my $inline = $options->{inline})) {
$$output = $self->xslate->render_string($inline, \%params);
eval {
local $SIG{__DIE__} = sub { $xslate_err = shift };
$$output = $self->xslate->render_string($inline, \%params);
};
}
else {
$$output = $self->xslate->render($name, \%params);
eval {
local $SIG{__DIE__} = sub { $xslate_err = shift };
$$output = $self->xslate->render($name, \%params);
};
}
$@ = $xslate_err if $xslate_err;

if ($@) {
$$output = undef;
if ( ( index( $@, 'Text::Xslate: LoadError: Cannot find \'exception.' ) < 0 )
&& ( index( $@, 'Text::Xslate: LoadError: Cannot find \'not_found.' ) < 0 )
) {
die $@ unless $orig_err;
}
}
die $@ if $@;

return 1;
return;
}


Expand Down
35 changes: 33 additions & 2 deletions t/lite_app.t
Expand Up @@ -23,7 +23,8 @@ app->renderer->add_handler(tt => $xslate);
app->helper(die => sub { die 'died in helper' });

get '/exception' => 'error';
get '/die' => 'die';
get '/die_tpl' => 'die';
get '/die_code' => sub { die };
get '/with_include' => 'include';
get '/with_wrapper' => 'wrapper';
get '/foo/:message' => 'index';
Expand All @@ -32,13 +33,43 @@ get '/on-disk' => 'foo';
my $t = Test::Mojo->new;

$t->get_ok('/exception')->status_is(500)->content_like(qr/error|^$/i);
$t->get_ok('/die')->status_is(500)->content_like(qr/error|^$/i);

# will "die" inside Xslate execution
$t->get_ok('/die_tpl')->status_is(500)->content_like(qr/error|^$/i);

# dies before Xslate does anything and Xslate will not be used since
# internal mojo diagnostic templates are handled by default renderer
# (i.e.: EP renderer)
$t->get_ok('/die_code')->status_is(500)->content_like(qr/error|^$/i);

$t->get_ok('/foo/hello')->content_like(qr/^hello\s*$/);
$t->get_ok('/with_include')->content_like(qr/^Hello\s*Include!Hallo\s*$/);
$t->get_ok('/with_wrapper')->content_like(qr/^wrapped\s*$/);
$t->get_ok('/on-disk')->content_is(4);
$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);

{
my $old_default = app->renderer->default_handler();
app->renderer->default_handler('tt');

$t->get_ok('/exception')->status_is(500)->content_like(qr/error|^$/i);

$t->get_ok('/die_tpl')->status_is(500)->content_like(qr/error|^$/i);

# dies before Xslate does anything but then mojolicious wants to
# render the (optional and here unavailable) exception template
# using Xslate engine which will not be able to locate that file
$t->get_ok('/die_code')->status_is(500)->content_like(qr/error|^$/i);

$t->get_ok('/foo/hello')->content_like(qr/^hello\s*$/);
$t->get_ok('/with_include')->content_like(qr/^Hello\s*Include!Hallo\s*$/);
$t->get_ok('/with_wrapper')->content_like(qr/^wrapped\s*$/);
$t->get_ok('/on-disk')->content_is(4);
$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);

app->renderer->default_handler($old_default);
}

done_testing;

__DATA__
Expand Down

0 comments on commit 9f734cf

Please sign in to comment.