8 use Class::MOP::Method;
10 # test before and afters
14 my $method = Class::MOP::Method->wrap(
15 body => sub { $trace .= 'primary' },
16 package_name => 'main',
19 isa_ok( $method, 'Class::MOP::Method' );
22 is( $trace, 'primary', '... got the right return value from method' );
25 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
26 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
27 isa_ok( $wrapped, 'Class::MOP::Method' );
30 is( $trace, 'primary',
31 '... got the right return value from the wrapped method' );
35 $wrapped->add_before_modifier( sub { $trace .= 'before -> ' } );
37 '... added the before modifier okay';
40 is( $trace, 'before -> primary',
41 '... got the right return value from the wrapped method (w/ before)'
46 $wrapped->add_after_modifier( sub { $trace .= ' -> after' } );
48 '... added the after modifier okay';
51 is( $trace, 'before -> primary -> after',
52 '... got the right return value from the wrapped method (w/ before)'
59 my $method = Class::MOP::Method->wrap(
61 package_name => 'main',
64 isa_ok( $method, 'Class::MOP::Method' );
66 is( $method->(), 4, '... got the right value from the wrapped method' );
68 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
69 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
70 isa_ok( $wrapped, 'Class::MOP::Method' );
72 is( $wrapped->(), 4, '... got the right value from the wrapped method' );
75 $wrapped->add_around_modifier( sub { ( 3, $_[0]->() ) } );
76 $wrapped->add_around_modifier( sub { ( 2, $_[0]->() ) } );
77 $wrapped->add_around_modifier( sub { ( 1, $_[0]->() ) } );
78 $wrapped->add_around_modifier( sub { ( 0, $_[0]->() ) } );
80 '... added the around modifier okay';
85 '... got the right results back from the around methods (in list context)'
88 is( scalar $wrapped->(), 4,
89 '... got the right results back from the around methods (in scalar context)'
96 my $method = Class::MOP::Method->wrap(
97 sub { push @tracelog => 'primary' },
98 package_name => 'main',
101 isa_ok( $method, 'Class::MOP::Method' );
103 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
104 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
105 isa_ok( $wrapped, 'Class::MOP::Method' );
108 $wrapped->add_before_modifier( sub { push @tracelog => 'before 1' } );
109 $wrapped->add_before_modifier( sub { push @tracelog => 'before 2' } );
110 $wrapped->add_before_modifier( sub { push @tracelog => 'before 3' } );
112 '... added the before modifier okay';
115 $wrapped->add_around_modifier(
116 sub { push @tracelog => 'around 1'; $_[0]->(); } );
117 $wrapped->add_around_modifier(
118 sub { push @tracelog => 'around 2'; $_[0]->(); } );
119 $wrapped->add_around_modifier(
120 sub { push @tracelog => 'around 3'; $_[0]->(); } );
122 '... added the around modifier okay';
125 $wrapped->add_after_modifier( sub { push @tracelog => 'after 1' } );
126 $wrapped->add_after_modifier( sub { push @tracelog => 'after 2' } );
127 $wrapped->add_after_modifier( sub { push @tracelog => 'after 3' } );
129 '... added the after modifier okay';
135 'before 3', 'before 2', 'before 1', # last-in-first-out order
136 'around 3', 'around 2', 'around 1', # last-in-first-out order
138 'after 1', 'after 2', 'after 3', # first-in-first-out order
140 '... got the right tracelog from all our before/around/after methods'
176 my $method = Class::MOP::Method->wrap(
178 package_name => 'main',
182 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
184 $wrapped->add_before_modifier($_)
185 for \&before1, \&before2, \&before3;
187 $wrapped->add_after_modifier($_)
188 for \&after1, \&after2, \&after3;
190 $wrapped->add_around_modifier($_)
191 for \&around1, \&around2, \&around3;
193 is( $wrapped->get_original_method, $method,
194 'check get_original_method' );
196 is_deeply( [ $wrapped->before_modifiers ],
197 [ \&before3, \&before2, \&before1 ],
198 'check before_modifiers' );
200 is_deeply( [ $wrapped->after_modifiers ],
201 [ \&after1, \&after2, \&after3 ],
202 'check after_modifiers' );
204 is_deeply( [ $wrapped->around_modifiers ],
205 [ \&around3, \&around2, \&around1 ],
206 'check around_modifiers' );