6 use Test::More tests => 28;
10 use Class::MOP::Method;
12 # test before and afters
16 my $method = Class::MOP::Method->wrap(
17 body => sub { $trace .= 'primary' },
18 package_name => 'main',
21 isa_ok( $method, 'Class::MOP::Method' );
24 is( $trace, 'primary', '... got the right return value from method' );
27 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
28 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
29 isa_ok( $wrapped, 'Class::MOP::Method' );
32 is( $trace, 'primary',
33 '... got the right return value from the wrapped method' );
37 $wrapped->add_before_modifier( sub { $trace .= 'before -> ' } );
39 '... added the before modifier okay';
42 is( $trace, 'before -> primary',
43 '... got the right return value from the wrapped method (w/ before)'
48 $wrapped->add_after_modifier( sub { $trace .= ' -> after' } );
50 '... added the after modifier okay';
53 is( $trace, 'before -> primary -> after',
54 '... got the right return value from the wrapped method (w/ before)'
61 my $method = Class::MOP::Method->wrap(
63 package_name => 'main',
66 isa_ok( $method, 'Class::MOP::Method' );
68 is( $method->(), 4, '... got the right value from the wrapped method' );
70 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
71 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
72 isa_ok( $wrapped, 'Class::MOP::Method' );
74 is( $wrapped->(), 4, '... got the right value from the wrapped method' );
77 $wrapped->add_around_modifier( sub { ( 3, $_[0]->() ) } );
78 $wrapped->add_around_modifier( sub { ( 2, $_[0]->() ) } );
79 $wrapped->add_around_modifier( sub { ( 1, $_[0]->() ) } );
80 $wrapped->add_around_modifier( sub { ( 0, $_[0]->() ) } );
82 '... added the around modifier okay';
87 '... got the right results back from the around methods (in list context)'
90 is( scalar $wrapped->(), 4,
91 '... got the right results back from the around methods (in scalar context)'
98 my $method = Class::MOP::Method->wrap(
99 sub { push @tracelog => 'primary' },
100 package_name => 'main',
103 isa_ok( $method, 'Class::MOP::Method' );
105 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
106 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
107 isa_ok( $wrapped, 'Class::MOP::Method' );
110 $wrapped->add_before_modifier( sub { push @tracelog => 'before 1' } );
111 $wrapped->add_before_modifier( sub { push @tracelog => 'before 2' } );
112 $wrapped->add_before_modifier( sub { push @tracelog => 'before 3' } );
114 '... added the before modifier okay';
117 $wrapped->add_around_modifier(
118 sub { push @tracelog => 'around 1'; $_[0]->(); } );
119 $wrapped->add_around_modifier(
120 sub { push @tracelog => 'around 2'; $_[0]->(); } );
121 $wrapped->add_around_modifier(
122 sub { push @tracelog => 'around 3'; $_[0]->(); } );
124 '... added the around modifier okay';
127 $wrapped->add_after_modifier( sub { push @tracelog => 'after 1' } );
128 $wrapped->add_after_modifier( sub { push @tracelog => 'after 2' } );
129 $wrapped->add_after_modifier( sub { push @tracelog => 'after 3' } );
131 '... added the after modifier okay';
137 'before 3', 'before 2', 'before 1', # last-in-first-out order
138 'around 3', 'around 2', 'around 1', # last-in-first-out order
140 'after 1', 'after 2', 'after 3', # first-in-first-out order
142 '... got the right tracelog from all our before/around/after methods'
178 my $method = Class::MOP::Method->wrap(
180 package_name => 'main',
184 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
186 $wrapped->add_before_modifier($_)
187 for \&before1, \&before2, \&before3;
189 $wrapped->add_after_modifier($_)
190 for \&after1, \&after2, \&after3;
192 $wrapped->add_around_modifier($_)
193 for \&around1, \&around2, \&around3;
195 is( $wrapped->get_original_method, $method,
196 'check get_original_method' );
198 is_deeply( [ $wrapped->before_modifiers ],
199 [ \&before3, \&before2, \&before1 ],
200 'check before_modifiers' );
202 is_deeply( [ $wrapped->after_modifiers ],
203 [ \&after1, \&after2, \&after3 ],
204 'check after_modifiers' );
206 is_deeply( [ $wrapped->around_modifiers ],
207 [ \&around3, \&around2, \&around1 ],
208 'check around_modifiers' );