4242aee97b8880bf441d4ecb57a96f7adc1679d2
[p5sagit/Function-Parameters.git] / t / foreign / Method-Signatures / invocant.t
1 #!perl
2
3 # Test that you can change the invocant.
4
5 use strict;
6 use warnings FATAL => 'all';
7
8 use Test::More 'no_plan';
9
10 our $skip_no_invocants;
11
12 {
13     package Stuff;
14
15     use Test::More;
16     use Function::Parameters qw(:strict);
17
18     sub new { bless {}, __PACKAGE__ }
19
20     method bar($arg) {
21         return ref $arg || $arg;
22     }
23
24     method invocant($class:) {
25         $class->bar(0);
26     }
27
28     method with_arg($class: $arg) {
29         $class->bar($arg);
30     }
31
32     method without_space($class:$arg) {
33         $class->bar($arg);
34     }
35
36     eval q{
37
38         method no_invocant_class_type($arg) {
39             $self->bar($arg);
40         }
41
42         method no_invocant_named_param(:$arg) {
43             $self->bar($arg);
44         }
45
46     };
47     is $@, '', 'compiles without invocant';
48 }
49
50 {
51     package Foo;
52     sub new { bless {}, __PACKAGE__ }
53 }
54
55 {
56     package Foo::Bar;
57     sub new { bless {}, __PACKAGE__ }
58 }
59
60
61 is( Stuff->invocant,                0 );
62 is( Stuff->with_arg(42),            42 );
63 is( Stuff->without_space(42),       42 );
64
65 my $stuff = Stuff->new;
66 is( $stuff->no_invocant_class_type(Foo::Bar->new),     'Foo::Bar' );
67 is( $stuff->no_invocant_named_param(arg => Foo->new),  'Foo' );