[Catalyst-commits] r8546 - Catalyst-Runtime/5.80/trunk/lib/Catalyst
rafl at dev.catalyst.perl.org
rafl at dev.catalyst.perl.org
Fri Oct 17 02:13:31 BST 2008
Author: rafl
Date: 2008-10-17 02:13:31 +0100 (Fri, 17 Oct 2008)
New Revision: 8546
Modified:
Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm
Log:
Port Catalyst::Test to Sub::Exporter.
Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm 2008-10-16 02:24:05 UTC (rev 8545)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm 2008-10-17 01:13:31 UTC (rev 8546)
@@ -2,17 +2,71 @@
use Test::More;
-use Moose;
-
use Catalyst::Exception;
use Catalyst::Utils;
use Class::Inspector;
+use Sub::Exporter;
-extends 'Exporter';
+{
+ my $import = Sub::Exporter::build_exporter({
+ groups => [ all => \&build_exports ],
+ into_level => 1,
+ });
-our @EXPORT=qw/&content_like &action_ok &action_redirect &action_notfound &contenttype_is/;
+ sub import {
+ my ($self, $class) = @_;
+ $import->($self, '-all' => { class => $class });
+ }
+}
+sub build_exports {
+ my ($self, $meth, $args, $defaults) = @_;
+ my $request;
+ my $class = $args->{class};
+
+ if ( $ENV{CATALYST_SERVER} ) {
+ $request = sub { remote_request(@_) };
+ } elsif (! $class) {
+ $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
+ } else {
+ unless( Class::Inspector->loaded( $class ) ) {
+ require Class::Inspector->filename( $class );
+ }
+ $class->import;
+
+ $request = sub { local_request( $class, @_ ) };
+ }
+
+ my $get = sub { $request->(@_)->content };
+
+ return {
+ request => $request,
+ get => $get,
+ content_like => sub {
+ my $action = shift;
+ return Test::More->builder->like($get->($action), at _);
+ },
+ action_ok => sub {
+ my $action = shift;
+ return Test::More->builder->ok($request->($action)->is_success, @_);
+ },
+ action_redirect => sub {
+ my $action = shift;
+ return Test::More->builder->ok($request->($action)->is_redirect, at _);
+ },
+ action_notfound => sub {
+ my $action = shift;
+ return Test::More->builder->is_eq($request->($action)->code,404, at _);
+ },
+ contenttype_is => sub {
+ my $action = shift;
+ my $res = $request->($action);
+ return Test::More->builder->is_eq(scalar($res->content_type), at _);
+ },
+ };
+}
+
=head1 NAME
Catalyst::Test - Test Catalyst Applications
@@ -91,37 +145,6 @@
my $res = request('foo/bar?test=1');
-=cut
-
-sub import {
- my $self = shift;
- my $class = shift;
-
- my ( $get, $request );
-
- if ( $ENV{CATALYST_SERVER} ) {
- $request = sub { remote_request(@_) };
- $get = sub { remote_request(@_)->content };
- } elsif (! $class) {
- $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
- $get = $request;
- } else {
- unless( Class::Inspector->loaded( $class ) ) {
- require Class::Inspector->filename( $class );
- }
- $class->import;
-
- $request = sub { local_request( $class, @_ ) };
- $get = sub { local_request( $class, @_ )->content };
- }
-
- no strict 'refs';
- my $caller = caller(0);
- *{"$caller\::request"} = $request;
- *{"$caller\::get"} = $get;
- __PACKAGE__->export_to_level(1);
-}
-
=head2 local_request
Simulate a request using L<HTTP::Request::AsCGI>.
@@ -223,52 +246,6 @@
Check for given mime type
-=cut
-
-sub content_like {
- my $caller=caller(0);
- no strict 'refs';
- my $get=*{"$caller\::get"};
- my $action=shift;
- return Test::More->builder->like(&$get($action), at _);
-}
-
-sub action_ok {
- my $caller=caller(0);
- no strict 'refs';
- my $request=*{"$caller\::request"};
- my $action=shift;
- return Test::More->builder->ok(&$request($action)->is_success, @_);
-}
-
-sub action_redirect {
- my $caller=caller(0);
- no strict 'refs';
- my $request=*{"$caller\::request"};
- my $action=shift;
- return Test::More->builder->ok(&$request($action)->is_redirect, at _);
-
-}
-
-sub action_notfound {
- my $caller=caller(0);
- no strict 'refs';
- my $request=*{"$caller\::request"};
- my $action=shift;
- return Test::More->builder->is_eq(&$request($action)->code,404, at _);
-
-}
-
-
-sub contenttype_is {
- my $caller=caller(0);
- no strict 'refs';
- my $request=*{"$caller\::request"};
- my $action=shift;
- my $res=&$request($action);
- return Test::More->builder->is_eq(scalar($res->content_type), at _);
-}
-
=head1 SEE ALSO
L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
More information about the Catalyst-commits
mailing list