avoid indirect object syntax
[p5sagit/Devel-Declare.git] / t / ctx-simple.t
index 938afc7..5b1db18 100644 (file)
@@ -1,3 +1,5 @@
+use strict;
+use warnings;
 use Devel::Declare ();
 
 {
@@ -15,6 +17,7 @@ use Devel::Declare ();
     my ($proto) = @_;
     my $inject = 'my ($self';
     if (defined $proto) {
+      $proto =~ s/[\r\n\s]+/ /g;
       $inject .= ", $proto" if length($proto);
       $inject .= ') = @_; ';
     } else {
@@ -29,6 +32,14 @@ use Devel::Declare ();
     $ctx->skip_declarator;
     my $name = $ctx->strip_name;
     my $proto = $ctx->strip_proto;
+
+    # Check for an 'is' to test strip_name_and_args
+    my $word = $ctx->strip_name;
+    my $traits;
+    if (defined($word) && ($word eq 'is')) {
+      $traits = $ctx->strip_names_and_args;
+    }
+
     my $inject = make_proto_unwrap($proto);
     if (defined $name) {
       $inject = $ctx->scope_injector_call().$inject;
@@ -37,7 +48,14 @@ use Devel::Declare ();
     if (defined $name) {
       $name = join('::', Devel::Declare::get_curstash_name(), $name)
         unless ($name =~ /::/);
-      $ctx->shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
+      # for trait testing we're just interested in the trait parse result, not
+      # the method body and its injections
+      $ctx->shadow(sub (&) {
+        no strict 'refs';
+        *{$name} = $traits
+          ? sub { $traits }
+          : shift;
+      });
     } else {
       $ctx->shadow(sub (&) { shift });
     }
@@ -68,6 +86,14 @@ my ($test_method1, $test_method2, @test_list);
     return (ref $self).': Foo: '.$foo;
   }
 
+  method has_many_traits() is (Trait1, Trait2(foo => 'bar'), Baz(one, two)) {
+    return 1;
+  }
+
+  method has_a_trait() is Foo1 {
+    return 1;
+  }
+
   method upgrade(){ # no spaces to make case pathological
     bless($self, 'DeclareTest2');
   }
@@ -88,9 +114,26 @@ my ($test_method1, $test_method2, @test_list);
 
   @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 });
 
+  method multiline1(
+  $foo
+  )
+  {
+    return "$foo$foo";
+  }
+
+  method multiline2(
+    $foo, $bar
+  ) { return "$foo $bar"; }
+
+  method 
+    multiline3 ($foo,
+        $bar) {
+    return "$bar $foo";
+  }
+
 }
 
-use Test::More 'no_plan';
+use Test::More 0.88;
 
 my $o = DeclareTest->new(attr => "value");
 
@@ -102,6 +145,22 @@ is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
 
 is($o->main, 'main', 'declaration of package named method ok');
 
+is($o->multiline1(3), '33', 'multiline1 proto ok');
+is($o->multiline2(1,2), '1 2', 'multiline2 proto ok');
+is($o->multiline3(4,5), '5 4', 'multiline3 proto ok');
+
+is_deeply(
+  $o->has_many_traits,
+  [['Trait1', undef], ['Trait2', q[foo => 'bar']], ['Baz', 'one, two']],
+  'extracting multiple traits',
+);
+
+is_deeply(
+  $o->has_a_trait,
+  [['Foo1', undef]],
+  'extract one trait without arguments',
+);
+
 $o->upgrade;
 
 isa_ok($o, 'DeclareTest2');
@@ -114,19 +173,4 @@ is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok')
 
 is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');
 
-__END__
-/home/rhesa/perl/t/method-no-semi....
-ok 1 - The object isa DeclareTest
-ok 2 - @_ args ok
-ok 3 - method with argument ok
-ok 4 - declaration of package named method ok
-ok 5 - The object isa DeclareTest2
-ok 6 - absolute method declaration ok
-ok 7 - anon method with @_ ok
-ok 8 - anon method with proto ok
-ok 9 - binding ok
-1..9
-ok
-All tests successful.
-Files=1, Tests=9,  0 wallclock secs ( 0.04 usr  0.00 sys +  0.05 cusr  0.00 csys =  0.09 CPU)
-Result: PASS
+done_testing;