Commit | Line | Data |
de19f115 |
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 | |
855d2774 |
14 | # test before and afters |
15 | { |
16 | my $trace = ''; |
de19f115 |
17 | |
855d2774 |
18 | my $method = Class::MOP::Method->new(sub { $trace .= 'primary' }); |
19 | isa_ok($method, 'Class::MOP::Method'); |
de19f115 |
20 | |
855d2774 |
21 | $method->(); |
22 | is($trace, 'primary', '... got the right return value from method'); |
23 | $trace = ''; |
de19f115 |
24 | |
855d2774 |
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 | } |
de19f115 |
72 | |
de19f115 |
73 | |
de19f115 |
74 | |
de19f115 |
75 | |
de19f115 |
76 | |