6 use Test::More tests => 24;
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', '... got the right return value from the wrapped method');
36 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
37 } '... added the before modifier okay';
40 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
44 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
45 } '... added the after modifier okay';
48 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
54 my $method = Class::MOP::Method->wrap(
56 package_name => 'main',
59 isa_ok($method, 'Class::MOP::Method');
61 is($method->(), 4, '... got the right value from the wrapped method');
63 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
64 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
65 isa_ok($wrapped, 'Class::MOP::Method');
67 is($wrapped->(), 4, '... got the right value from the wrapped method');
70 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });
71 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
72 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });
73 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
74 } '... added the around modifier okay';
79 '... got the right results back from the around methods (in list context)');
81 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
87 my $method = Class::MOP::Method->wrap(
88 sub { push @tracelog => 'primary' },
89 package_name => 'main',
92 isa_ok($method, 'Class::MOP::Method');
94 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
95 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
96 isa_ok($wrapped, 'Class::MOP::Method');
99 $wrapped->add_before_modifier(sub { push @tracelog => 'before 1' });
100 $wrapped->add_before_modifier(sub { push @tracelog => 'before 2' });
101 $wrapped->add_before_modifier(sub { push @tracelog => 'before 3' });
102 } '... added the before modifier okay';
105 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });
106 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
107 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });
108 } '... added the around modifier okay';
111 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
112 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
113 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
114 } '... added the after modifier okay';
120 'before 3', 'before 2', 'before 1', # last-in-first-out order
121 'around 3', 'around 2', 'around 1', # last-in-first-out order
123 'after 1', 'after 2', 'after 3', # first-in-first-out order
125 '... got the right tracelog from all our before/around/after methods');