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