From: Florian Ragwitz Date: Fri, 26 Jun 2009 19:45:30 +0000 (+0000) Subject: Add test for ContextClosure. X-Git-Tag: 5.80006~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=d9d8aa5102e2b961d4f5f0bdd76d9dbcbcefc1dd Add test for ContextClosure. --- diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm index e9e7e59..e14e3dc 100644 --- a/t/lib/TestApp.pm +++ b/t/lib/TestApp.pm @@ -13,10 +13,32 @@ use Catalyst qw/ /; use Catalyst::Utils; +use Moose; +use namespace::autoclean; + our $VERSION = '0.01'; TestApp->config( name => 'TestApp', root => '/some/dir' ); +if (eval { Class::MOP::load_class('CatalystX::LeakChecker'); 1 }) { + with 'CatalystX::LeakChecker'; + + has leaks => ( + is => 'ro', + default => sub { [] }, + ); +} + +sub found_leaks { + my ($ctx, @leaks) = @_; + push @{ $ctx->leaks }, @leaks; +} + +sub count_leaks { + my ($ctx) = @_; + return scalar @{ $ctx->leaks }; +} + TestApp->setup; sub index : Private { diff --git a/t/lib/TestApp/Controller/ContextClosure.pm b/t/lib/TestApp/Controller/ContextClosure.pm new file mode 100644 index 0000000..c81ffc9 --- /dev/null +++ b/t/lib/TestApp/Controller/ContextClosure.pm @@ -0,0 +1,28 @@ +package TestApp::Controller::ContextClosure; + +use Moose; + +BEGIN { + extends 'Catalyst::Controller'; + with 'Catalyst::Component::ContextClosure'; +} + +sub normal_closure : Local { + my ($self, $ctx) = @_; + $ctx->stash(closure => sub { + $ctx->response->body('from normal closure'); + }); + $ctx->response->body('stashed normal closure'); +} + +sub context_closure : Local { + my ($self, $ctx) = @_; + $ctx->stash(closure => $self->make_context_closure(sub { + $ctx->response->body('from context closure'); + }, $ctx)); + $ctx->response->body('stashed context closure'); +} + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/t/live_component_controller_context_closure.t b/t/live_component_controller_context_closure.t new file mode 100644 index 0000000..94d2ef1 --- /dev/null +++ b/t/live_component_controller_context_closure.t @@ -0,0 +1,28 @@ +use strict; +use warnings; +use Test::More; + +BEGIN { + unless (eval 'use CatalystX::LeakChecker; 1') { + plan skip_all => 'CatalystX::LeakChecker required for this test'; + } + + plan tests => 4; +} + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Catalyst::Test 'TestApp'; + +{ + my ($resp, $ctx) = ctx_request('/contextclosure/normal_closure'); + ok($resp->is_success); + is($ctx->count_leaks, 1); +} + +{ + my ($resp, $ctx) = ctx_request('/contextclosure/context_closure'); + ok($resp->is_success); + is($ctx->count_leaks, 0); +}