private_path method for actions that returns a string suitable for use in forward...
Guillermo Roditi [Sat, 11 Jul 2009 22:07:47 +0000 (22:07 +0000)]
Changes
lib/Catalyst/Action.pm
t/unit_core_action.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 1f7df0a..70a4777 100644 (file)
--- a/Changes
+++ b/Changes
@@ -9,6 +9,10 @@
   Refactoring / cleanups:
        - Deleted the Restarter engine and its Watcher code. Use the
          new Catalyst::Restarter in a recent Catalyst::Devel instead.
+       - New unit test for Catalyst::Action 'unit_core_action.t' (groditi)
+
+  New features:
+       - private_path method for Catalyst::Action + docs + tests (groditi)
 
 5.80007 2009-06-30 23:54:34
 
index cd2d008..e9c9363 100644 (file)
@@ -8,6 +8,8 @@ Catalyst::Action - Catalyst Action
 
     <form action="[%c.uri_for(c.action)%]">
 
+    $c->forward( $action->private_path );
+
 =head1 DESCRIPTION
 
 This class represents a Catalyst Action. You can access the object for the
@@ -28,6 +30,13 @@ has 'reverse' => (is => 'rw');
 has attributes => (is => 'rw');
 has name => (is => 'rw');
 has code => (is => 'rw');
+has private_path => (
+  reader => 'private_path',
+  isa => 'Str',
+  lazy => 1,
+  required => 1,
+  default => sub { '/'.shift->reverse },
+);
 
 use overload (
 
@@ -129,6 +138,11 @@ Returns the private namespace this action lives in.
 
 Returns the private path for this action.
 
+=head2 private_path
+
+Returns absolute private path for this action. Unlike C<reverse>, the
+C<private_path> of an action is always suitable for passing to C<forward>.
+
 =head2 name
 
 returns the sub name of this action.
diff --git a/t/unit_core_action.t b/t/unit_core_action.t
new file mode 100644 (file)
index 0000000..ca84422
--- /dev/null
@@ -0,0 +1,54 @@
+use Test::More tests => 6;
+use strict;
+use warnings;
+use Moose::Meta::Class;
+#use Moose::Meta::Attribute;
+use Catalyst::Request;
+
+use_ok('Catalyst::Action');
+
+my $action_1 = Catalyst::Action->new(
+  name => 'foo',
+  code => sub { "DUMMY" },
+  reverse => 'bar/foo',
+  namespace => 'bar',
+  attributes => {
+    Args => [ 1 ],
+    attr2 => [ 2 ],
+  },
+);
+
+my $action_2 = Catalyst::Action->new(
+  name => 'foo',
+  code => sub { "DUMMY" },
+  reverse => 'bar/foo',
+  namespace => 'bar',
+  attributes => {
+    Args => [ 2 ],
+    attr2 => [ 2 ],
+  },
+);
+
+is("${action_1}", $action_1->reverse, 'overload string');
+is($action_1->(), 'DUMMY', 'overload code');
+
+my $anon_meta = Moose::Meta::Class->create_anon_class(
+  attributes => [
+    Moose::Meta::Attribute->new(
+      request => (
+        reader => 'request',
+        required => 1,
+        default => sub { Catalyst::Request->new(arguments => [qw/one two/]) },
+      ),
+    ),
+  ],
+  methods => { req => sub { shift->request(@_) } }
+);
+
+my $mock_c = $anon_meta->new_object();
+$mock_c->request;
+
+ok(!$action_1->match($mock_c), 'bad match fails');
+ok($action_2->match($mock_c), 'good match works');
+
+ok($action_2->compare( $action_1 ), 'compare works');