release 0.20
[gitmo/Class-MOP.git] / t / 031_method_modifiers.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 26;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Class::MOP');    
11     use_ok('Class::MOP::Method');
12 }
13
14 # test before and afters
15 {
16         my $trace = '';
17
18         my $method = Class::MOP::Method->wrap(sub { $trace .= 'primary' });
19         isa_ok($method, 'Class::MOP::Method');
20
21         $method->();
22         is($trace, 'primary', '... got the right return value from method');
23         $trace = '';
24
25         my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
26         isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
27         isa_ok($wrapped, 'Class::MOP::Method');
28
29         $wrapped->();
30         is($trace, 'primary', '... got the right return value from the wrapped method');
31         $trace = '';
32
33         lives_ok {
34                 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
35         } '... added the before modifier okay';
36
37         $wrapped->();
38         is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
39         $trace = '';
40
41         lives_ok {
42                 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
43         } '... added the after modifier okay';
44
45         $wrapped->();
46         is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
47         $trace = '';
48 }
49
50 # test around method
51 {
52         my $method = Class::MOP::Method->wrap(sub { 4 });
53         isa_ok($method, 'Class::MOP::Method');
54         
55         is($method->(), 4, '... got the right value from the wrapped method');  
56
57         my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
58         isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
59         isa_ok($wrapped, 'Class::MOP::Method');
60
61         is($wrapped->(), 4, '... got the right value from the wrapped method');
62         
63         lives_ok {
64                 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });          
65                 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
66                 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });          
67                 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });                          
68         } '... added the around modifier okay'; 
69
70         is_deeply(
71                 [ $wrapped->() ],
72                 [ 0, 1, 2, 3, 4 ],
73                 '... got the right results back from the around methods (in list context)');
74                 
75         is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');               
76 }
77
78 {
79         my @tracelog;
80         
81         my $method = Class::MOP::Method->wrap(sub { push @tracelog => 'primary' });
82         isa_ok($method, 'Class::MOP::Method');
83         
84         my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
85         isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
86         isa_ok($wrapped, 'Class::MOP::Method'); 
87         
88         lives_ok {
89                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 1' });
90                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 2' });            
91                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 3' });            
92         } '... added the before modifier okay';
93         
94         lives_ok {
95                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });                
96                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
97                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });                                                
98         } '... added the around modifier okay'; 
99         
100         lives_ok {
101                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
102                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
103                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });                              
104         } '... added the after modifier okay';  
105         
106         $wrapped->();
107         is_deeply(
108                 \@tracelog,
109                 [ 
110                   'before 3', 'before 2', 'before 1',  # last-in-first-out order
111                   'around 3', 'around 2', 'around 1',  # last-in-first-out order
112                   'primary',
113                   'after 1', 'after 2', 'after 3',     # first-in-first-out order
114                 ],
115                 '... got the right tracelog from all our before/around/after methods');
116 }
117
118
119