gotta have a plan
[p5sagit/Devel-Declare.git] / t / methinstaller-simple.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More;
5
6 my $Have_Devel_BeginLift;
7 BEGIN {
8   # setup_for_cv() introduced in 0.001001
9   $Have_Devel_BeginLift = eval q{ use Devel::BeginLift 0.001001; 1 };
10 }
11
12
13 {
14   package MethodHandlers;
15
16   use strict;
17   use warnings;
18   use base 'Devel::Declare::MethodInstaller::Simple';
19
20   # undef  -> my ($self) = shift;
21   # ''     -> my ($self) = @_;
22   # '$foo' -> my ($self, $foo) = @_;
23
24   sub parse_proto {
25     my $ctx = shift;
26     my ($proto) = @_;
27     my $inject = 'my ($self';
28     if (defined $proto) {
29       $inject .= ", $proto" if length($proto);
30       $inject .= ') = @_; ';
31     } else {
32       $inject .= ') = shift;';
33     }
34     return $inject;
35   }
36
37   sub code_for {
38     my($self, $name) = @_;
39
40     my $code = $self->SUPER::code_for($name);
41
42     if( defined $name and $Have_Devel_BeginLift ) {
43       Devel::BeginLift->setup_for_cv($code);
44     }
45
46     return $code;
47   }
48 }
49
50 my ($test_method1, $test_method2, @test_list);
51
52 {
53   package DeclareTest;
54
55   BEGIN { # normally, this'd go in MethodHandlers::import
56   MethodHandlers->install_methodhandler(
57     name => 'method',
58     into => __PACKAGE__,
59   );
60   }
61
62   # Test at_BEGIN
63   SKIP: {
64       ::skip "Need Devel::BeginLift for compile time methods", 1
65         unless $Have_Devel_BeginLift;
66       ::can_ok( "DeclareTest", qw(new foo upgrade) );
67   }
68
69   method new {
70     my $class = ref $self || $self;
71     return bless({ @_ }, $class);
72   }
73
74   method foo ($foo) {
75     return (ref $self).': Foo: '.$foo;
76   }
77
78   method upgrade(){ # no spaces to make case pathological
79     bless($self, 'DeclareTest2');
80   }
81
82   method DeclareTest2::bar () {
83     return 'DeclareTest2: bar';
84   }
85
86   $test_method1 = method {
87     return join(', ', $self->{attr}, $_[1]);
88   };
89
90   $test_method2 = method ($what) {
91     return join(', ', ref $self, $what);
92   };
93
94   method main () { return "main"; }
95
96   @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 });
97
98   method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
99 }
100
101
102 my $o = DeclareTest->new(attr => "value");
103
104 isa_ok($o, 'DeclareTest');
105
106 is($o->{attr}, 'value', '@_ args ok');
107
108 is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
109
110 is($o->main, 'main', 'declaration of package named method ok');
111
112 $o->leftie( 'attributes work' );
113 is($o->leftie, 'attributes work', 'code attributes intact');
114
115 $o->upgrade;
116
117 isa_ok($o, 'DeclareTest2');
118
119 is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
120
121 is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
122
123 is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
124
125 is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');
126
127 done_testing;