branches/context_object: reformatted with 2-space indent, and "if ("
[p5sagit/Devel-Declare.git] / t / methinstaller-simple.t
CommitLineData
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
28my ($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
71use Test::More 'no_plan';
72
73my $o = DeclareTest->new(attr => "value");
74
75isa_ok($o, 'DeclareTest');
76
77is($o->{attr}, 'value', '@_ args ok');
78
79is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');
80
81is($o->main, 'main', 'declaration of package named method ok');
82
83$o->upgrade;
84
85isa_ok($o, 'DeclareTest2');
86
87is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');
88
89is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');
90
91is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');
92
93is_deeply([ map { $_->() } @test_list ], [ 1, 2, 3, 4], 'binding ok');
94
95__END__
96/home/rhesa/perl/t/methinstaller-simple....
97ok 1 - The object isa DeclareTest
98ok 2 - @_ args ok
99ok 3 - method with argument ok
100ok 4 - declaration of package named method ok
101ok 5 - The object isa DeclareTest2
102ok 6 - absolute method declaration ok
103ok 7 - anon method with @_ ok
104ok 8 - anon method with proto ok
105ok 9 - binding ok
1061..9
107ok
108All tests successful.
109Files=1, Tests=9, 0 wallclock secs ( 0.04 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.09 CPU)
110Result: PASS