Skip to content

Commit

Permalink
improved tests for many environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 20, 2011
1 parent f396164 commit 77bb626
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 28 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -7,6 +7,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL close method to Mojo::IOLoop::Stream.
- Added "hello.pl" and "fast.pl" to example scripts.
- Improved documentation. (marcus, vervain, sri)
- Improved tests for many environment variables.
- Fixed small argument bug in client method of Mojo::IOLoop.
- Fixed small memory leak in Mojo::IOLoop::Stream.

Expand Down
26 changes: 12 additions & 14 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -9,14 +9,15 @@ use Mojo::JSON;
use Mojo::Util 'encode';

has cache => sub { Mojo::Cache->new };
has default_format => 'html';
has detect_templates => 1;
has encoding => 'UTF-8';
has handlers => sub { {} };
has helpers => sub { {} };
has layout_prefix => 'layouts';
has root => '/';
has [qw/default_handler default_template_class/];
has default_format => 'html';
has 'default_handler';
has default_template_class => sub { $ENV{MOJO_TEMPLATE_CLASS} || 'main' };
has detect_templates => 1;
has encoding => 'UTF-8';
has handlers => sub { {} };
has helpers => sub { {} };
has layout_prefix => 'layouts';
has root => '/';

# "This is not how Xmas is supposed to be.
# In my day Xmas was about bringing people together,
Expand Down Expand Up @@ -206,10 +207,7 @@ sub _detect_handler {
# Please line up in order of how much beryllium it takes to kill you."
sub _detect_template_class {
my ($self, $options) = @_;
$options->{template_class}
|| $ENV{MOJO_TEMPLATE_CLASS}
|| $self->default_template_class
|| 'main';
return $options->{template_class} || $self->default_template_class;
}

sub _extends {
Expand Down Expand Up @@ -309,8 +307,8 @@ C<Embedded Perl> handled by L<Mojolicious::Plugin::EPRenderer>.
my $default = $renderer->default_template_class;
$renderer = $renderer->default_template_class('main');
The renderer will use this class to look for templates in the C<DATA>
section.
Class to use for finding templates in C<DATA> section, defaults to the value
of C<MOJO_TEMPLATE_CLASS> or C<main>.
=head2 C<detect_templates>
Expand Down
12 changes: 5 additions & 7 deletions lib/Mojolicious/Static.pm
Expand Up @@ -9,7 +9,8 @@ use Mojo::Content::Single;
use Mojo::Home;
use Mojo::Path;

has [qw/default_static_class root/];
has default_static_class => sub { $ENV{MOJO_STATIC_CLASS} || 'main' };
has 'root';

# "Valentine's Day's coming? Aw crap! I forgot to get a girlfriend again!"
sub dispatch {
Expand Down Expand Up @@ -134,11 +135,7 @@ sub _get_data_file {
return if $rel =~ /\.\w+\.\w+$/;

# Detect DATA class
my $class =
$c->stash->{static_class}
|| $ENV{MOJO_STATIC_CLASS}
|| $self->default_static_class
|| 'main';
my $class = $c->stash->{static_class} || $self->default_static_class;

# Find DATA file
my $data = $self->{data_files}->{$class}
Expand Down Expand Up @@ -184,7 +181,8 @@ L<Mojolicious::Static> implements the following attributes.
my $class = $static->default_static_class;
$static = $static->default_static_class('main');
The dispatcher will use this class to look for files in the C<DATA> section.
Class to use for finding files in C<DATA> section, defaults to the value of
C<MOJO_STATIC_CLASS> or C<main>.
=head2 C<root>
Expand Down
8 changes: 7 additions & 1 deletion t/mojo/asset.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 56;
use Test::More tests => 57;

# "And now, in the spirit of the season: start shopping.
# And for every dollar of Krusty merchandise you buy,
Expand Down Expand Up @@ -133,6 +133,12 @@ ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

# Temporary directory
$backup = $ENV{MOJO_TMPDIR} || '';
$ENV{MOJO_TMPDIR} = '/does/not/exist';
is(Mojo::Asset::File->new->tmpdir, '/does/not/exist', 'right value');
$ENV{MOJO_TMPDIR} = $backup;

# Temporary file without cleanup
$file = Mojo::Asset::File->new(cleanup => 0)->add_chunk('test');
ok $file->is_file, 'stored in file';
Expand Down
8 changes: 7 additions & 1 deletion t/mojo/user_agent.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 70;
use Test::More tests => 71;

# "The strong must protect the sweet."
use Mojo::IOLoop;
Expand Down Expand Up @@ -88,6 +88,12 @@ $ENV{http_proxy} = $backup4;
$ENV{https_proxy} = $backup5;
$ENV{no_proxy} = $backup6;

# Max redirects
$backup = $ENV{MOJO_MAX_REDIRECTS} || '';
$ENV{MOJO_MAX_REDIRECTS} = 25;
is(Mojo::UserAgent->new->max_redirects, 25, 'right value');
$ENV{MOJO_MAX_REDIRECTS} = $backup;

# User agent
$ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);

Expand Down
10 changes: 9 additions & 1 deletion t/mojo/websocket.t
Expand Up @@ -6,14 +6,22 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 46;
use Test::More tests => 47;

# "I can't believe it! Reading and writing actually paid off!"
use IO::Socket::INET;
use Mojo::IOLoop;
use Mojo::Transaction::WebSocket;
use Mojo::UserAgent;
use Mojolicious::Lite;

# Max WebSocket size
my $backup = $ENV{MOJO_MAX_WEBSOCKET_SIZE} || '';
$ENV{MOJO_MAX_WEBSOCKET_SIZE} = 1024;
is(Mojo::Transaction::WebSocket->new->max_websocket_size, 1024,
'right value');
$ENV{MOJO_MAX_WEBSOCKET_SIZE} = $backup;

# User agent
my $ua = app->ua;

Expand Down
10 changes: 7 additions & 3 deletions t/mojolicious/json_config_lite_app.t
Expand Up @@ -8,7 +8,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 16;
use Test::More tests => 17;

# "Oh, I always feared he might run off like this.
# Why, why, why didn't I break his legs?"
Expand Down Expand Up @@ -38,13 +38,17 @@ $t->get_ok('/')->status_is(200)->content_is("barbarbar\n");

# No config file, default only
$config =
plugin JSONConfig => {file => 'nonexisted', default => {foo => 'qux'}};
plugin JSONConfig => {file => 'nonexistent', default => {foo => 'qux'}};
is $config->{foo}, 'qux', 'right value';
is app->config->{foo}, 'qux', 'right value';
is app->config('foo'), 'qux', 'right value';

# No config file, no default
ok !(eval { plugin JSONConfig => {file => 'nonexisted'} }), 'no config file';
ok !(eval { plugin JSONConfig => {file => 'nonexistent'} }), 'no config file';
my $backup = $ENV{MOJO_CONFIG} || '';
$ENV{MOJO_CONFIG} = 'nonexistent';
ok !(eval { plugin 'JSONConfig' }), 'no config file';
$ENV{MOJO_CONFIG} = $backup;

__DATA__
@@ index.html.ep
Expand Down
15 changes: 14 additions & 1 deletion t/mojolicious/renderer.t
@@ -1,12 +1,25 @@
use Mojo::Base -strict;

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

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

# Template class
my $backup = $ENV{MOJO_TEMPLATE_CLASS} || '';
$ENV{MOJO_TEMPLATE_CLASS} = 'Foo';
is(Mojolicious::Renderer->new->default_template_class, 'Foo', 'right value');
$ENV{MOJO_TEMPLATE_CLASS} = $backup;

# Static class
$backup = $ENV{MOJO_STATIC_CLASS} || '';
$ENV{MOJO_STATIC_CLASS} = 'Bar';
is(Mojolicious::Static->new->default_static_class, 'Bar', 'right value');
$ENV{MOJO_STATIC_CLASS} = $backup;

# Fresh controller
my $c = Mojolicious::Controller->new;
Expand Down

0 comments on commit 77bb626

Please sign in to comment.