6 use Test::More tests => 26;
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 = 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', '... got the right return value from the wrapped method');
34 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
35 } '... added the before modifier okay';
38 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
42 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
43 } '... added the after modifier okay';
46 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
52 my $method = Class::MOP::Method->new(sub { 4 });
53 isa_ok($method, 'Class::MOP::Method');
55 is($method->(), 4, '... got the right value from the wrapped method');
57 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
58 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
59 isa_ok($wrapped, 'Class::MOP::Method');
61 is($wrapped->(), 4, '... got the right value from the wrapped method');
64 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });
65 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
66 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });
67 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
68 } '... added the around modifier okay';
73 '... got the right results back from the around methods (in list context)');
75 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
81 my $method = Class::MOP::Method->new(sub { push @tracelog => 'primary' });
82 isa_ok($method, 'Class::MOP::Method');
84 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
85 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
86 isa_ok($wrapped, 'Class::MOP::Method');
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';
95 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });
96 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
97 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });
98 } '... added the around modifier okay';
101 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
102 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
103 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
104 } '... added the after modifier okay';
110 'before 3', 'before 2', 'before 1', # last-in-first-out order
111 'around 1', 'around 2', 'around 3', # last-in-first-out order
113 'after 3', 'after 2', 'after 1', # first-in-first-out order
115 '... got the right tracelog from all our before/around/after methods');