cleanups and tests
Robert 'phaylon' Sedlacek [Fri, 8 May 2009 23:51:38 +0000 (01:51 +0200)]
lib/CatalystX/Declarative.pm
lib/CatalystX/Declarative/Keyword/Action.pm
lib/CatalystX/Declarative/Keyword/Controller.pm
t/001_basic.t [new file with mode: 0644]
t/lib/TestApp/Controller/Foo.pm
t/lib/TestApp/Controller/Root.pm [new file with mode: 0644]

index cd07514..855109f 100644 (file)
@@ -2,14 +2,11 @@ use MooseX::Declare;
 
 class CatalystX::Declarative extends MooseX::Declare {
 
-    use aliased 'CatalystX::Declarative::Keyword::Controller',  'ControllerKeyword';
+    use aliased 'CatalystX::Declarative::Keyword::Controller', 'ControllerKeyword';
 
     around keywords {
-
-        return(
-            $self->$orig(),
-            ControllerKeyword->new(identifier => 'controller'),
-        );
+        $self->$orig,
+        ControllerKeyword->new(identifier => 'controller'),
     }
 }
 
index 2154c35..54a648a 100644 (file)
@@ -21,7 +21,6 @@ class CatalystX::Declarative::Keyword::Action
 
         # parse declarations
         until (do { $ctx->skipspace; $ctx->peek_next_char } eq any qw( ; { } )) {
-            warn "LINESTR[" . pp($ctx->get_linestr) . "]\n";
 
             $ctx->skipspace;
             
@@ -87,8 +86,6 @@ class CatalystX::Declarative::Keyword::Action
             );
         }
 
-        pp \%attributes;
-
         AttributeRole->meta->apply($method);
 
         my @attributes = map { 
@@ -203,7 +200,7 @@ class CatalystX::Declarative::Keyword::Action
             $ctx->set_linestr($linestr);
             return $1;
         }
-        elsif ($rest =~ /^ ' ( (?:[a-z0-9]|\/)* ) ' /ix) {
+        elsif ($rest =~ /^ ' ( (?:[.:;,_a-z0-9]|\/)* ) ' /ix) {
             substr($linestr, $ctx->offset, length($1) + 2) = '';
             $ctx->set_linestr($linestr);
             return $1;
index 6116452..f633735 100644 (file)
@@ -10,8 +10,9 @@ class CatalystX::Declarative::Keyword::Controller
 
 
     before add_namespace_customizations (Object $ctx, Str $package) {
+
         MooseX::MethodAttributes->init_meta(for_class => $package);
-        #$ctx->add_preamble_code_parts('use MooseX::MethodAttributes');
+        $ctx->add_preamble_code_parts('use CLASS');
     }
 
     method default_superclasses { 'Catalyst::Controller' }
diff --git a/t/001_basic.t b/t/001_basic.t
new file mode 100644 (file)
index 0000000..a54e2ca
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 7;
+use Catalyst::Test 'TestApp';
+
+# simple stuff
+is get('/foo'), 'Welcome to TestApp!', 'simple root action';
+
+# with arguments
+is get('/foo/with_args/hello/cthulhu'), 'Hello, Cthulhu!', 'simple argument';
+is get('/foo/with_args/at_end/2/3'), 6, 'two arguments at the end';
+is get('/foo/with_args/in_the_middle/3/4/end_of_the_middle'), 24, 'two arguments in the middle';
+is get('/foo/with_args/4/8/fhtagn/15/16/23/42'), '4, 8, 15, 16, 23, 42', 'complex arguments in both';
+
+# under keyword
+is get('/foo/under/23'), 'under 23', 'under as keyword';
+
+# comma separation
+is get('/foo/,comma/iaia'), 'iaia', 'comma separation';
index 60f2a18..4a5091a 100644 (file)
@@ -2,22 +2,94 @@ use CatalystX::Declarative;
 
 controller TestApp::Controller::Foo {
 
-    has bar => (is => 'rw');
 
-    method baz { }
+    #
+    #   look, a Moose!
+    #
 
-    under '/', action base, as '';
+    has title => (
+        is      => 'ro',
+        isa     => 'Str',
+        default => 'TestApp',
+    );
 
-    action root under base as '' is final { }
 
-    action list under base is final { }
+    #
+    #   normal methods are very useful too
+    #
 
-    under base, as 'id', action object ($id) { }
+    method welcome_message { sprintf 'Welcome to %s!', $self->title }
 
-    action view under object is final { $ctx->res->body('Hello World!') }
+    method greet (Str $name) { "Hello, $name!" }
 
-    action edit under object is final { }
 
-    action tag (@tags) under object is final { }
+    #
+    #   the simple stuff
+    #
+
+    action base under '/base' as 'foo';
+
+    action root under base as '' is final {
+        $ctx->response->body( $self->welcome_message );
+    }
+
+    
+    #
+    #   with arguments
+    #
+
+    action with_args under base;
+
+    action hello (Str $name) under with_args is final {
+        $ctx->response->body($self->greet(ucfirst $name));
+    }
+
+    action at_end (Int $x, Int $y) under with_args is final { 
+        $ctx->response->body( $x * $y );
+    }
+
+    action in_the_middle (Int $x, Int $y) under with_args {
+        $ctx->stash(result => $x * $y);
+    }
+    action end_of_the_middle under in_the_middle is final {
+        $ctx->response->body($ctx->stash->{result} * 2);
+    }
+
+    action all_the_way (Int $x) under with_args as '' {
+        $ctx->stash(x => $x);
+    }
+    action through_the_sky (Int $y) under all_the_way as '' {
+        $ctx->stash(y => $y);
+    }
+    action and_beyond (@rest) under through_the_sky as fhtagn is final {
+        $ctx->response->body(join ', ', 
+            $ctx->stash->{x},
+            $ctx->stash->{y},
+            @rest,
+        );
+    }
+
+
+    #
+    #   under is also a valid keyword
+    #
+
+    under base action under_base as under;
+
+    under under_base as '' action even_more_under (Int $i) is final {
+        $ctx->response->body("under $i");
+    }
+
+
+    #
+    #   too many words? go comma go!
+    #
+
+    action comma_base, as '', under base;
+
+    under comma_base, is final, action comma ($str), as ',comma' {
+        $ctx->response->body($str);
+    }
+
 }
 
diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm
new file mode 100644 (file)
index 0000000..95afd0a
--- /dev/null
@@ -0,0 +1,8 @@
+use CatalystX::Declarative;
+
+controller TestApp::Controller::Root {
+    CLASS->config(namespace => '');
+
+    action base under '/' as '';
+}
+