Commit | Line | Data |
e7be1784 |
1 | |
2 | { |
3 | package MethodHandlers; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | use base 'Devel::Declare::MethodInstaller::Simple'; |
8 | |
9 | # undef -> my ($self) = shift; |
10 | # '' -> my ($self) = @_; |
11 | # '$foo' -> my ($self, $foo) = @_; |
12 | |
13 | sub parse_proto { |
14 | my $ctx = shift; |
15 | my ($proto) = @_; |
16 | my $inject = 'my ($self'; |
17 | if (defined $proto) { |
18 | $inject .= ", $proto" if length($proto); |
19 | $inject .= ') = @_; '; |
20 | } else { |
21 | $inject .= ') = shift;'; |
22 | } |
23 | return $inject; |
24 | } |
25 | |
26 | } |
27 | |
28 | my ($test_method1, $test_method2, @test_list); |
29 | |
30 | { |
31 | package DeclareTest; |
32 | |
33 | BEGIN { # normally, this'd go in MethodHandlers::import |
34 | MethodHandlers->install_methodhandler( |
35 | name => 'method', |
36 | into => __PACKAGE__, |
5b27c9b2 |
37 | ); |
e7be1784 |
38 | } |
39 | |
40 | method new { |
41 | my $class = ref $self || $self; |
42 | return bless({ @_ }, $class); |
43 | } |
44 | |
45 | method foo ($foo) { |
46 | return (ref $self).': Foo: '.$foo; |
47 | } |
48 | |
49 | method upgrade(){ # no spaces to make case pathological |
50 | bless($self, 'DeclareTest2'); |
51 | } |
52 | |
53 | method DeclareTest2::bar () { |
54 | return 'DeclareTest2: bar'; |
55 | } |
56 | |
57 | $test_method1 = method { |
58 | return join(', ', $self->{attr}, $_[1]); |
59 | }; |
60 | |
61 | $test_method2 = method ($what) { |
62 | return join(', ', ref $self, $what); |
63 | }; |
64 | |
65 | method main () { return "main"; } |
66 | |
67 | @test_list = (method { 1 }, sub { 2 }, method () { 3 }, sub { 4 }); |
68 | |
69 | } |
70 | |
71 | use Test::More 'no_plan'; |
72 | |
73 | my $o = DeclareTest->new(attr => "value"); |
74 | |
75 | isa_ok($o, 'DeclareTest'); |
76 | |
77 | is($o->{attr}, 'value', '@_ args ok'); |
78 | |
79 | is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok'); |
80 | |
81 | is($o->main, 'main', 'declaration of package named method ok'); |
82 | |
83 | $o->upgrade; |
84 | |
85 | isa_ok($o, 'DeclareTest2'); |
86 | |
87 | is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok'); |
88 | |
89 | is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok'); |
90 | |
91 | is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok'); |
92 | |
93 | is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok'); |
94 | |
95 | __END__ |
96 | /home/rhesa/perl/t/methinstaller-simple.... |
97 | ok 1 - The object isa DeclareTest |
98 | ok 2 - @_ args ok |
99 | ok 3 - method with argument ok |
100 | ok 4 - declaration of package named method ok |
101 | ok 5 - The object isa DeclareTest2 |
102 | ok 6 - absolute method declaration ok |
103 | ok 7 - anon method with @_ ok |
104 | ok 8 - anon method with proto ok |
105 | ok 9 - binding ok |
106 | 1..9 |
107 | ok |
108 | All tests successful. |
109 | Files=1, Tests=9, 0 wallclock secs ( 0.04 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.09 CPU) |
110 | Result: PASS |