method modifiers
[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 no_plan => 18;
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         } '... added the around modifier okay'; 
66
67         is_deeply(
68                 [ $wrapped->() ],
69                 [ 1, 2, 3, 4 ],
70                 '... got the right results back from the around methods');
71 }
72
73
74
75
76