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 -> ' } );
36 }, undef, '... added the before modifier okay' );
39 is( $trace, 'before -> primary',
40 '... got the right return value from the wrapped method (w/ before)'
45 $wrapped->add_after_modifier( sub { $trace .= ' -> after' } );
46 }, undef, '... added the after modifier okay' );
49 is( $trace, 'before -> primary -> after',
50 '... got the right return value from the wrapped method (w/ before)'
57 my $method = Class::MOP::Method->wrap(
59 package_name => 'main',
62 isa_ok( $method, 'Class::MOP::Method' );
64 is( $method->(), 4, '... got the right value from the wrapped method' );
66 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
67 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
68 isa_ok( $wrapped, 'Class::MOP::Method' );
70 is( $wrapped->(), 4, '... got the right value from the wrapped method' );
73 $wrapped->add_around_modifier( sub { ( 3, $_[0]->() ) } );
74 $wrapped->add_around_modifier( sub { ( 2, $_[0]->() ) } );
75 $wrapped->add_around_modifier( sub { ( 1, $_[0]->() ) } );
76 $wrapped->add_around_modifier( sub { ( 0, $_[0]->() ) } );
77 }, undef, '... added the around modifier okay' );
82 '... got the right results back from the around methods (in list context)'
85 is( scalar $wrapped->(), 4,
86 '... got the right results back from the around methods (in scalar context)'
93 my $method = Class::MOP::Method->wrap(
94 sub { push @tracelog => 'primary' },
95 package_name => 'main',
98 isa_ok( $method, 'Class::MOP::Method' );
100 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
101 isa_ok( $wrapped, 'Class::MOP::Method::Wrapped' );
102 isa_ok( $wrapped, 'Class::MOP::Method' );
105 $wrapped->add_before_modifier( sub { push @tracelog => 'before 1' } );
106 $wrapped->add_before_modifier( sub { push @tracelog => 'before 2' } );
107 $wrapped->add_before_modifier( sub { push @tracelog => 'before 3' } );
108 }, undef, '... added the before modifier okay' );
111 $wrapped->add_around_modifier(
112 sub { push @tracelog => 'around 1'; $_[0]->(); } );
113 $wrapped->add_around_modifier(
114 sub { push @tracelog => 'around 2'; $_[0]->(); } );
115 $wrapped->add_around_modifier(
116 sub { push @tracelog => 'around 3'; $_[0]->(); } );
117 }, undef, '... added the around modifier okay' );
120 $wrapped->add_after_modifier( sub { push @tracelog => 'after 1' } );
121 $wrapped->add_after_modifier( sub { push @tracelog => 'after 2' } );
122 $wrapped->add_after_modifier( sub { push @tracelog => 'after 3' } );
123 }, undef, '... added the after modifier okay' );
129 'before 3', 'before 2', 'before 1', # last-in-first-out order
130 'around 3', 'around 2', 'around 1', # last-in-first-out order
132 'after 1', 'after 2', 'after 3', # first-in-first-out order
134 '... got the right tracelog from all our before/around/after methods'
170 my $method = Class::MOP::Method->wrap(
172 package_name => 'main',
176 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
178 $wrapped->add_before_modifier($_)
179 for \&before1, \&before2, \&before3;
181 $wrapped->add_after_modifier($_)
182 for \&after1, \&after2, \&after3;
184 $wrapped->add_around_modifier($_)
185 for \&around1, \&around2, \&around3;
187 is( $wrapped->get_original_method, $method,
188 'check get_original_method' );
190 is_deeply( [ $wrapped->before_modifiers ],
191 [ \&before3, \&before2, \&before1 ],
192 'check before_modifiers' );
194 is_deeply( [ $wrapped->after_modifiers ],
195 [ \&after1, \&after2, \&after3 ],
196 'check after_modifiers' );
198 is_deeply( [ $wrapped->around_modifiers ],
199 [ \&around3, \&around2, \&around1 ],
200 'check around_modifiers' );