gotta have a plan
[p5sagit/Devel-Declare.git] / t / methinstaller-simple.t
CommitLineData
a664754d 1#!/usr/bin/perl -w
2
3use strict;
b52072dc 4use Test::More;
a664754d 5
6my $Have_Devel_BeginLift;
7BEGIN {
8 # setup_for_cv() introduced in 0.001001
9 $Have_Devel_BeginLift = eval q{ use Devel::BeginLift 0.001001; 1 };
10}
11
e7be1784 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
a664754d 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 }
e7be1784 48}
49
50my ($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__,
5b27c9b2 59 );
e7be1784 60 }
61
a664754d 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
e7be1784 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
ab449c2e 98 method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
e7be1784 99}
100
e7be1784 101
102my $o = DeclareTest->new(attr => "value");
103
104isa_ok($o, 'DeclareTest');
105
106is($o->{attr}, 'value', '@_ args ok');
107
108is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
109
110is($o->main, 'main', 'declaration of package named method ok');
111
ab449c2e 112$o->leftie( 'attributes work' );
b0a89632 113is($o->leftie, 'attributes work', 'code attributes intact');
114
e7be1784 115$o->upgrade;
116
117isa_ok($o, 'DeclareTest2');
118
119is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
120
121is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
122
123is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
124
125is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');
126
b52072dc 127done_testing;