4 use Test::More 'no_plan';
6 my $Have_Devel_BeginLift;
8 # setup_for_cv() introduced in 0.001001
9 $Have_Devel_BeginLift = eval q{ use Devel::BeginLift 0.001001; 1 };
14 package MethodHandlers;
18 use base 'Devel::Declare::MethodInstaller::Simple';
20 # undef -> my ($self) = shift;
21 # '' -> my ($self) = @_;
22 # '$foo' -> my ($self, $foo) = @_;
27 my $inject = 'my ($self';
29 $inject .= ", $proto" if length($proto);
30 $inject .= ') = @_; ';
32 $inject .= ') = shift;';
38 my($self, $name) = @_;
40 my $code = $self->SUPER::code_for($name);
42 if( defined $name and $Have_Devel_BeginLift ) {
43 Devel::BeginLift->setup_for_cv($code);
50 my ($test_method1, $test_method2, @test_list);
55 BEGIN { # normally, this'd go in MethodHandlers::import
56 MethodHandlers->install_methodhandler(
64 ::skip "Need Devel::BeginLift for compile time methods", 1
65 unless $Have_Devel_BeginLift;
66 ::can_ok( "DeclareTest", qw(new foo upgrade) );
70 my $class = ref $self || $self;
71 return bless({ @_ }, $class);
75 return (ref $self).': Foo: '.$foo;
78 method upgrade(){ # no spaces to make case pathological
79 bless($self, 'DeclareTest2');
82 method DeclareTest2::bar () {
83 return 'DeclareTest2: bar';
86 $test_method1 = method {
87 return join(', ', $self->{attr}, $_[1]);
90 $test_method2 = method ($what) {
91 return join(', ', ref $self, $what);
94 method main () { return "main"; }
96 @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 });
98 method leftie($left) : method { $self->{left} ||= $left; $self->{left} };
102 my $o = DeclareTest->new(attr => "value");
104 isa_ok($o, 'DeclareTest');
106 is($o->{attr}, 'value', '@_ args ok');
108 is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
110 is($o->main, 'main', 'declaration of package named method ok');
112 $o->leftie( 'attributes work' );
113 is($o->leftie, 'attributes work', 'code attributes intact');
117 isa_ok($o, 'DeclareTest2');
119 is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
121 is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
123 is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
125 is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');