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