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