When from_psgi_response, a bug when headers are already output and charset is set...
[catagits/Catalyst-Runtime.git] / t / arg_constraints.t
index 8bfa085..05278d1 100644 (file)
@@ -5,7 +5,7 @@ use utf8;
 
 BEGIN {
   use Test::More;
-  eval "use Type::Tiny; 1" || do {
+  eval "use Type::Tiny 1.000005; 1" || do {
     plan skip_all => "Trouble loading Type::Tiny and friends => $@";
   };
 }
@@ -16,14 +16,14 @@ BEGIN {
 
   use strict;
   use warnings;
+
   use Type::Utils -all;
   use Types::Standard -types;
   use Type::Library
    -base,
    -declare => qw( UserId Heart User ContextLike );
 
-  extends "Types::Standard"; 
+  extends "Types::Standard";
 
   class_type User, { class => "MyApp::Model::User::user" };
   duck_type ContextLike, [qw/model/];
@@ -43,6 +43,23 @@ BEGIN {
 }
 
 {
+  package MyApp::Role::Controller;
+  $INC{'MyApp/Role/Controller.pm'} = __FILE__;
+
+  use Moose::Role;
+  use MooseX::MethodAttributes::Role;
+  use MyApp::Types qw/Int Str/;
+
+  sub role_str :Path('role_test') Args(Str) {
+    my ($self, $c, $arg) = @_;
+    $c->res->body('role_str'.$arg);
+  }
+
+  sub role_int :Path('role_test') Args(Int) {
+    my ($self, $c, $arg) = @_;
+    $c->res->body('role_int'.$arg);
+  }
+
   package MyApp::Model::User;
   $INC{'MyApp/Model/User.pm'} = __FILE__;
 
@@ -66,9 +83,12 @@ BEGIN {
 
   use Moose;
   use MooseX::MethodAttributes;
+  use Types::Standard qw/slurpy/;
   use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/;
 
   extends 'Catalyst::Controller';
+  with 'MyApp::Role::Controller';
+
 
   sub user :Local Args(UserId) {
     my ($self, $c, $int) = @_;
@@ -82,6 +102,11 @@ BEGIN {
     $c->res->body("name: $user->{name}, age: $user->{age}");
   }
 
+  sub stringy_enum :Local Args('Int',Int) {
+    my ($self, $c) = @_;
+    $c->res->body('enum');
+  }
+
   sub an_int :Local Args(Int) {
     my ($self, $c, $int) = @_;
     $c->res->body('an_int');
@@ -93,7 +118,7 @@ BEGIN {
   }
 
   sub many_ints :Local Args(ArrayRef[Int]) {
-    my ($self, $c, $int) = @_;
+    my ($self, $c, @ints) = @_;
     $c->res->body('many_ints');
   }
 
@@ -102,6 +127,11 @@ BEGIN {
     $c->res->body('tuple');
   }
 
+  sub slurpy_tuple :Local Args(Tuple[Str,Int, slurpy ArrayRef[Int]]) {
+    my ($self, $c, $str, $int) = @_;
+    $c->res->body('tuple');
+  }
+
   sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
     my ($self, $c, $int) = @_;
     $c->res->body('match');
@@ -122,7 +152,7 @@ BEGIN {
       sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
 
       sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
-    
+
     sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
 
       sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
@@ -168,10 +198,71 @@ BEGIN {
 
   MyApp::Controller::Root->config(namespace=>'');
 
+  package MyApp::Controller::Autoclean;
+  $INC{'MyApp/Controller/Autoclean.pm'} = __FILE__;
+
+  use Moose;
+  use MooseX::MethodAttributes;
+  use namespace::clean -except => [ 'meta' ];
+
+  use MyApp::Types qw/Int/;
+
+  extends 'Catalyst::Controller';
+
+  sub an_int :Local Args(Int) {
+    my ($self, $c, $int) = @_;
+    $c->res->body('an_int (autoclean)');
+  }
+
+  MyApp::Controller::Autoclean->config(namespace=>'autoclean');
+
+  package MyApp::Role;
+  $INC{'MyApp/Role.pm'} = __FILE__;
+
+  use Moose::Role;
+  use MooseX::MethodAttributes::Role;
+  use MyApp::Types qw/Int/;
+
+  sub an_int :Local Args(Int) {
+    my ($self, $c, $int) = @_;
+    $c->res->body('an_int (withrole)');
+  }
+
+  sub an_int_ns :Local Args(MyApp::Types::Int) {
+    my ($self, $c, $int) = @_;
+    $c->res->body('an_int (withrole)');
+  }
+
+  package MyApp::BaseController;
+  $INC{'MyApp/BaseController.pm'} = __FILE__;
+
+  use Moose;
+  use MooseX::MethodAttributes;
+  use MyApp::Types qw/Int/;
+
+  extends 'Catalyst::Controller';
+
+  sub from_parent :Local Args(Int) {
+    my ($self, $c, $id) = @_;
+    $c->res->body("from_parent $id");
+  }
+
+  package MyApp::Controller::WithRole;
+  $INC{'MyApp/Controller/WithRole.pm'} = __FILE__;
+
+  use Moose;
+  use MooseX::MethodAttributes;
+
+  extends 'MyApp::BaseController';
+
+  with 'MyApp::Role';
+
+  MyApp::Controller::WithRole->config(namespace=>'withrole');
+
   package MyApp;
   use Catalyst;
 
-  MyApp->setup;
+  MyApp->setup('-Log=fatal');
 }
 
 use Catalyst::Test 'MyApp';
@@ -295,6 +386,17 @@ SKIP: {
 }
 
 {
+  my $res = request '/tuple/aaa/111/111/111';
+  is $res->content, 'default';
+}
+
+{
+  my $res = request '/slurpy_tuple/aaa/111/111/111';
+  is $res->content, 'tuple';
+}
+
+
+{
   my $res = request '/many_ints/1/2/a';
   is $res->content, 'default';
 }
@@ -365,6 +467,21 @@ SKIP: {
     is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
 }
 
+{
+    my $res = request '/stringy_enum/1/2';
+    is $res->content, 'enum', "request '/stringy_enum/a'";
+}
+
+{
+    my $res = request '/stringy_enum/b/2';
+    is $res->content, 'default', "request '/stringy_enum/a'";
+}
+
+{
+    my $res = request '/stringy_enum/1/a';
+    is $res->content, 'default', "request '/stringy_enum/a'";
+}
+
 =over
 
 | /chain_base/*/*/*/*/*/*                 | /chain_base (1)
@@ -439,4 +556,41 @@ SKIP: {
 
 }
 
+# Test Roles
+
+{
+    my $res = request '/role_test/1';
+    is $res->content, 'role_int1';
+}
+
+{
+    my $res = request '/role_test/a';
+    is $res->content, 'role_stra';
+}
+
+
+{
+  my $res = request '/autoclean/an_int/1';
+  is $res->content, 'an_int (autoclean)';
+}
+
+{
+  my $res = request '/withrole/an_int_ns/S';
+  is $res->content, 'default';
+}
+
+{
+  my $res = request '/withrole/an_int_ns/111';
+  is $res->content, 'an_int (withrole)';
+}
+
+{
+  my $res = request '/withrole/an_int/1';
+  is $res->content, 'an_int (withrole)';
+}
+
+{
+  my $res = request '/withrole/from_parent/1';
+  is $res->content, 'from_parent 1';
+}
 done_testing;