more method modifier stuff
[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 => 23;
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->new(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 = $method->wrap();
26         isa_ok($wrapped, 'Class::MOP::Method');
27
28         $wrapped->();
29         is($trace, 'primary', '... got the right return value from the wrapped method');
30         $trace = '';
31
32         lives_ok {
33                 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
34         } '... added the before modifier okay';
35
36         $wrapped->();
37         is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
38         $trace = '';
39
40         lives_ok {
41                 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
42         } '... added the after modifier okay';
43
44         $wrapped->();
45         is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
46         $trace = '';
47 }
48
49 # test around method
50 {
51         my $method = Class::MOP::Method->new(sub { 4 });
52         isa_ok($method, 'Class::MOP::Method');
53         
54         is($method->(), 4, '... got the right value from the wrapped method');  
55
56         my $wrapped = $method->wrap;
57         isa_ok($wrapped, 'Class::MOP::Method');
58
59         is($wrapped->(), 4, '... got the right value from the wrapped method');
60         
61         lives_ok {
62                 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });          
63                 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
64                 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });          
65                 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });                          
66         } '... added the around modifier okay'; 
67
68         is_deeply(
69                 [ $wrapped->() ],
70                 [ 0, 1, 2, 3, 4 ],
71                 '... got the right results back from the around methods (in list context)');
72                 
73         is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');               
74 }
75
76 {
77         my @tracelog;
78         
79         my $method = Class::MOP::Method->new(sub { push @tracelog => 'primary' });
80         isa_ok($method, 'Class::MOP::Method');
81         
82         my $wrapped = $method->wrap();
83         isa_ok($wrapped, 'Class::MOP::Method'); 
84         
85         lives_ok {
86                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 1' });
87                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 2' });            
88                 $wrapped->add_before_modifier(sub { push @tracelog => 'before 3' });            
89         } '... added the before modifier okay';
90         
91         lives_ok {
92                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });                
93                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
94                 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });                                                
95         } '... added the around modifier okay'; 
96         
97         lives_ok {
98                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
99                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
100                 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });                              
101         } '... added the after modifier okay';  
102         
103         $wrapped->();
104         is_deeply(
105                 \@tracelog,
106                 [ 
107                   'before 3', 'before 2', 'before 1',  # last-in-first-out order
108                   'around 1', 'around 2', 'around 3',  # last-in-first-out order
109                   'primary',
110                   'after 3', 'after 2', 'after 1',     # first-in-first-out order
111                 ],
112                 '... got the right tracelog from all our before/around/after methods');
113 }
114
115
116