Skip to content

Commit

Permalink
added "caching_lite_app.t" test
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 21, 2012
1 parent 0c3f2f9 commit 3f219e5
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 81 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

2.65 2012-03-21 00:00:00
- Improved documentation.
- Improved tests.

2.64 2012-03-21 00:00:00
- Deprecated Mojolicious::Routes->controller_base_class in favor of
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -38,7 +38,7 @@ sub capture;
*capture = sub { shift->(@_) };
sub escape;
*escape = sub {
return $_[0] if ref $_[0] && ref $_[0] eq 'Mojo::ByteStream';
return $_[0] if (ref $_[0] || '') eq 'Mojo::ByteStream';
no warnings 'uninitialized';
Mojo::Util::xml_escape "$_[0]";
};
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -227,7 +227,7 @@ sub send {
my ($self, $frame, $cb) = @_;

# Binary or raw text
if (ref $frame && ref $frame eq 'HASH') {
if ((ref $frame || '') eq 'HASH') {
$frame =
exists $frame->{text}
? [1, 0, 0, 0, TEXT, $frame->{text}]
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/URL.pm
Expand Up @@ -162,7 +162,7 @@ sub query {
}

# Merge with array
elsif (ref $_[0] && ref $_[0] eq 'ARRAY') {
elsif ((ref $_[0] || '') eq 'ARRAY') {
my $q = $self->{query} ||= Mojo::Parameters->new;
while (my $name = shift @{$_[0]}) {
my $value = shift @{$_[0]};
Expand All @@ -171,7 +171,7 @@ sub query {
}

# Append hash
elsif (ref $_[0] && ref $_[0] eq 'HASH') {
elsif ((ref $_[0] || '') eq 'HASH') {
($self->{query} ||= Mojo::Parameters->new)->append(%{$_[0]});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -15,7 +15,7 @@ sub form {
my ($self, $url) = (shift, shift);

# Callback
my $cb = pop @_ if ref $_[-1] && ref $_[-1] eq 'CODE';
my $cb = pop @_ if (ref $_[-1] || '') eq 'CODE';

# Form
my $encoding = shift;
Expand Down Expand Up @@ -227,7 +227,7 @@ sub tx {
ref $url ? $req->url($url) : $req->url->parse($url);

# Callback
my $cb = pop @_ if ref $_[-1] && ref $_[-1] eq 'CODE';
my $cb = pop @_ if (ref $_[-1] || '') eq 'CODE';

# Body
$req->body(pop @_)
Expand Down
16 changes: 3 additions & 13 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -167,7 +167,7 @@ sub param {

# Captured unreserved values
if (!$RESERVED{$name} && defined(my $v = $p->{$name})) {
return ref $v && ref $v eq 'ARRAY' ? wantarray ? @$v : $$v[0] : $v;
return (ref $v || '') eq 'ARRAY' ? wantarray ? @$v : $$v[0] : $v;
}

# Upload
Expand Down Expand Up @@ -570,27 +570,17 @@ sub url_for {

sub write {
my ($self, $chunk, $cb) = @_;

if (ref $chunk && ref $chunk eq 'CODE') {
$cb = $chunk;
$chunk = undef;
}
($cb, $chunk) = ($chunk, undef) if (ref $chunk || '') eq 'CODE';
$self->res->write($chunk, sub { shift and $self->$cb(@_) if $cb });
$self->rendered;

return $self;
}

sub write_chunk {
my ($self, $chunk, $cb) = @_;

if (ref $chunk && ref $chunk eq 'CODE') {
$cb = $chunk;
$chunk = undef;
}
($cb, $chunk) = ($chunk, undef) if (ref $chunk || '') eq 'CODE';
$self->res->write_chunk($chunk, sub { shift and $self->$cb(@_) if $cb });
$self->rendered;

return $self;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -29,7 +29,7 @@ sub parse {
die qq/Couldn't parse config file "$file": $@/
unless my $config = eval "sub app { \$app }; $content";
die qq/Config file "$file" did not return a hashref.\n/
unless ref $config && ref $config eq 'HASH';
unless (ref $config || '') eq 'HASH';

return $config;
}
Expand Down
9 changes: 3 additions & 6 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -84,14 +84,11 @@ sub register {
memorize => sub {
shift;
my $cb = pop;
return '' unless ref $cb && ref $cb eq 'CODE';
return '' unless (ref $cb || '') eq 'CODE';
my $name = shift;
my $args;
if (ref $name && ref $name eq 'HASH') {
$args = $name;
$name = undef;
}
else { $args = shift || {} }
if ((ref $name || '') eq 'HASH') { ($args, $name) = ($name, undef) }
else { $args = shift || {} }

# Default name
$name ||= join '', map { $_ || '' } (caller(1))[0 .. 3];
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -75,7 +75,7 @@ sub register {

# CDATA
my $cb = sub {''};
if (ref $_[-1] && ref $_[-1] eq 'CODE') {
if ((ref $_[-1] || '') eq 'CODE') {
my $old = pop;
$cb = sub { "//<![CDATA[\n" . $old->() . "\n//]]>" }
}
Expand Down Expand Up @@ -189,7 +189,7 @@ sub register {

# CDATA
my $cb;
if (ref $_[-1] && ref $_[-1] eq 'CODE') {
if ((ref $_[-1] || '') eq 'CODE') {
my $old = pop;
$cb = sub { "/*<![CDATA[*/\n" . $old->() . "\n/*]]>*/" }
}
Expand Down Expand Up @@ -240,8 +240,8 @@ sub register {
my $cb = sub {''};
my $value;
if (@_ % 2) {
if (ref $_[-1] && ref $_[-1] eq 'CODE') { $cb = pop }
else { $value = shift }
if ((ref $_[-1] || '') eq 'CODE') { $cb = pop }
else { $value = shift }
}

# Make sure value is wrapped
Expand Down
4 changes: 0 additions & 4 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -246,10 +246,6 @@ sub _extends {
return delete $stash->{extends};
}

# "I want to see the edge of the universe.
# Ooh, that sounds cool.
# It's funny, you live in the universe, but you never get to do this things
# until someone comes to visit."
sub _render_template {
my ($self, $c, $output, $options) = @_;

Expand Down
68 changes: 68 additions & 0 deletions t/mojolicious/caching_lite_app.t
@@ -0,0 +1,68 @@
use Mojo::Base -strict;

# Disable Bonjour, IPv6 and libev
BEGIN {
$ENV{MOJO_NO_BONJOUR} = $ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor';
}

use Test::More;
plan skip_all => 'set TEST_CACHING to enable this test (developer only!)'
unless $ENV{TEST_CACHING};
plan tests => 21;

# "I want to see the edge of the universe.
# Ooh, that sounds cool.
# It's funny, you live in the universe, but you never get to do this things
# until someone comes to visit."
use Mojolicious::Lite;
use Test::Mojo;

# GET /memorized
get '/memorized' => 'memorized';

# GET /memorized
my $t = Test::Mojo->new;
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/\d+a\d+b\d+c\d+\nd\d+\ne\d+/);
my $memorized = $t->tx->res->body;

# GET /memorized
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($memorized);

# GET /memorized
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($memorized);

# GET /memorized (expired)
sleep 2;
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/\d+a\d+b\d+c\d+\nd\d+\ne\d+/)->content_isnt($memorized);

__DATA__
@@ memorized.html.ep
<%= memorize begin =%>
<%= time =%>
<% end =%>
<%= memorize begin =%>
<%= 'a' . time =%>
<% end =%><%= memorize begin =%>
<%= 'b' . time =%>
<% end =%>
<%= memorize test => begin =%>
<%= 'c' . time =%>
<% end =%>
<%= memorize expiry => {expires => time + 1} => begin %>
<%= 'd' . time =%>
<% end =%>
<%= memorize {expires => time + 1} => begin %>
<%= 'e' . time =%>
<% end =%>
48 changes: 1 addition & 47 deletions t/mojolicious/lite_app.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor';
}

use Test::More tests => 730;
use Test::More tests => 709;

# "Wait you're the only friend I have...
# You really want a robot for a friend?
Expand Down Expand Up @@ -337,9 +337,6 @@ get '/layout' => sub {
# POST /template
post '/template' => 'index';

# GET /memorized
get '/memorized' => 'memorized';

# * /something
any '/something' => sub {
my $self = shift;
Expand Down Expand Up @@ -1050,30 +1047,6 @@ $t->post_ok('/template')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('Just works!');

# GET /memorized
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/\d+a\d+b\d+c\d+\nd\d+\ne\d+/);
my $memorized = $t->tx->res->body;

# GET /memorized
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($memorized);

# GET /memorized
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is($memorized);

# GET /memorized (expired)
sleep 2;
$t->get_ok('/memorized')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/\d+a\d+b\d+c\d+\nd\d+\ne\d+/)->content_isnt($memorized);

# GET /something
$t->get_ok('/something')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down Expand Up @@ -1455,25 +1428,6 @@ text!
@@ template.txt.epl
<div id="foo">Redirect works!</div>
@@ memorized.html.ep
<%= memorize begin =%>
<%= time =%>
<% end =%>
<%= memorize begin =%>
<%= 'a' . time =%>
<% end =%><%= memorize begin =%>
<%= 'b' . time =%>
<% end =%>
<%= memorize test => begin =%>
<%= 'c' . time =%>
<% end =%>
<%= memorize expiry => {expires => time + 1} => begin %>
<%= 'd' . time =%>
<% end =%>
<%= memorize {expires => time + 1} => begin %>
<%= 'e' . time =%>
<% end =%>
@@ test(test)(\Qtest\E)(.html.ep
<%= $self->match->endpoint->name %>
Expand Down

0 comments on commit 3f219e5

Please sign in to comment.