6 use Test::More tests => 23;
11 use_ok('Class::MOP::Method');
14 # test before and afters
18 my $method = Class::MOP::Method->new(sub { $trace .= 'primary' });
19 isa_ok($method, 'Class::MOP::Method');
22 is($trace, 'primary', '... got the right return value from method');
25 my $wrapped = $method->wrap();
26 isa_ok($wrapped, 'Class::MOP::Method');
29 is($trace, 'primary', '... got the right return value from the wrapped method');
33 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
34 } '... added the before modifier okay';
37 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
41 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
42 } '... added the after modifier okay';
45 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
51 my $method = Class::MOP::Method->new(sub { 4 });
52 isa_ok($method, 'Class::MOP::Method');
54 is($method->(), 4, '... got the right value from the wrapped method');
56 my $wrapped = $method->wrap;
57 isa_ok($wrapped, 'Class::MOP::Method');
59 is($wrapped->(), 4, '... got the right value from the wrapped method');
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 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
66 } '... added the around modifier okay';
71 '... got the right results back from the around methods (in list context)');
73 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
79 my $method = Class::MOP::Method->new(sub { push @tracelog => 'primary' });
80 isa_ok($method, 'Class::MOP::Method');
82 my $wrapped = $method->wrap();
83 isa_ok($wrapped, 'Class::MOP::Method');
86 $wrapped->add_before_modifier(sub { push @tracelog => 'before 1' });
87 $wrapped->add_before_modifier(sub { push @tracelog => 'before 2' });
88 $wrapped->add_before_modifier(sub { push @tracelog => 'before 3' });
89 } '... added the before modifier okay';
92 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });
93 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
94 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });
95 } '... added the around modifier okay';
98 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
99 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
100 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
101 } '... added the after modifier okay';
107 'before 3', 'before 2', 'before 1', # last-in-first-out order
108 'around 1', 'around 2', 'around 3', # last-in-first-out order
110 'after 3', 'after 2', 'after 1', # first-in-first-out order
112 '... got the right tracelog from all our before/around/after methods');