Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more empty controller tests
  • Loading branch information
kraih committed Oct 19, 2011
1 parent f717f50 commit 60f016a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
38 changes: 12 additions & 26 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -86,23 +86,22 @@ sub render {

# Extract important stash values
my $template = delete $stash->{template};
my $class = $stash->{template_class};
my $format = $stash->{format} || $self->default_format;
my $handler = $stash->{handler};
my $data = delete $stash->{data};
my $json = delete $stash->{json};
my $text = delete $stash->{text};
my $inline = delete $stash->{inline};

# Pick handler
my $handler = $stash->{handler};
$handler = $self->default_handler if defined $inline && !defined $handler;
my $options = {
template => $template,
format => $format,
handler => $handler,
encoding => $self->encoding,
inline => $inline,
template_class => $class
template_class => $stash->{template_class}
};

# Text
Expand Down Expand Up @@ -138,15 +137,10 @@ sub render {

# Extends
while ((my $extends = $self->_extends($c)) && !$json && !$data) {
my $stash = $c->stash;
$class = $stash->{template_class};
$options->{template_class} = $class;
$handler = $stash->{handler};
$options->{handler} = $handler;
$format = $stash->{format} || $self->default_format;
$options->{format} = $format;
$options->{template_class} = $stash->{template_class};
$options->{handler} = $stash->{handler};
$options->{format} = $stash->{format} || $self->default_format;
$options->{template} = $extends;

$self->_render_template($c, \$output, $options);
}

Expand Down Expand Up @@ -233,25 +227,20 @@ sub _list_data_templates {
sub _render_template {
my ($self, $c, $output, $options) = @_;

# Renderer
# Render
my $handler =
$options->{handler}
|| $self->_detect_handler($options)
|| $self->default_handler;
$options->{handler} = $handler;
my $renderer = $self->handlers->{$handler};

# No handler
unless ($renderer) {
$c->app->log->error(qq/No handler for "$handler" available./);
return;
if (my $renderer = $self->handlers->{$handler}) {
return 1 if $renderer->($self, $c, $output, $options);
}

# Render
return unless $renderer->($self, $c, $output, $options);
# No handler
else { $c->app->log->error(qq/No handler for "$handler" available./) }

# Success!
return 1;
return;
}

1;
Expand All @@ -265,12 +254,9 @@ Mojolicious::Renderer - MIME type based renderer
use Mojolicious::Renderer;
my $renderer = Mojolicious::Renderer->new;
=head1 DESCRIPTION
L<Mojolicious::Renderer> is the standard L<Mojolicious> renderer.
It turns your stashed data structures into content.
See L<Mojolicious::Guides::Rendering> for more.
=head1 ATTRIBUTES
Expand Down Expand Up @@ -410,7 +396,7 @@ Render output through one of the Mojo renderers.
This renderer requires some configuration, at the very least you will need to
have a default C<format> and a default C<handler> as well as a C<template> or
C<text>/C<json>.
See L<Mojolicious::Controller> for a more user friendly interface.
See L<Mojolicious::Controller/"render"> for a more user friendly interface.
=head2 C<template_name>
Expand Down
10 changes: 5 additions & 5 deletions t/mojolicious/dispatch.t
Expand Up @@ -34,8 +34,11 @@ use Mojo::Transaction::HTTP;
use Mojolicious::Controller;
use Mojolicious::Routes;

# Set
# Fresh controller
my $c = Mojolicious::Controller->new;
is $c->url_for('/'), '/', 'routes are working';

# Set
$c->stash(foo => 'bar');
is $c->stash('foo'), 'bar', 'set and return a stash value';

Expand Down Expand Up @@ -74,15 +77,12 @@ $c->stash({a => 1, b => 2});
$stash = $c->stash;
is_deeply $stash, {a => 1, b => 2}, 'set via hashref';

# Rendering
is $c->render(text => 'works', partial => 1), 'works', 'rendering works';

# Controller with application and routes
$c = Test::Controller->new(app => Mojolicious->new);
$c->app->log->path(undef);
$c->app->log->level('fatal');
my $d = Mojolicious::Routes->new;
ok $d, 'initialized';

$d->namespace('Test');
$d->route('/')->to(controller => 'foo', action => 'home');
$d->route('/foo/(capture)')->to(controller => 'foo', action => 'bar');
Expand Down
13 changes: 9 additions & 4 deletions t/mojolicious/renderer.t
@@ -1,15 +1,20 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 5;
use Test::More tests => 6;

# "Actually, she wasn't really my girlfriend,
# she just lived nextdoor and never closed her curtains."
use Mojolicious;
use Mojolicious::Controller;
use Mojolicious::Renderer;

# "Actually, she wasn't really my girlfriend,
# she just lived nextdoor and never closed her curtains."
my $c = Mojolicious::Controller->new(app => Mojolicious->new);
# Fresh controller
my $c = Mojolicious::Controller->new;
is $c->render(text => 'works', partial => 1), 'works', 'renderer is working';

# Controller with application
$c = Mojolicious::Controller->new(app => Mojolicious->new);
$c->app->log->path(undef);
$c->app->log->level('fatal');
my $r = Mojolicious::Renderer->new(default_format => 'debug');
Expand Down

0 comments on commit 60f016a

Please sign in to comment.