6 use Test::More tests => 26;
11 use_ok('Class::MOP::Method');
14 # test before and afters
18 my $method = Class::MOP::Method->wrap(
19 sub { $trace .= 'primary' },
20 package_name => 'main',
23 isa_ok($method, 'Class::MOP::Method');
26 is($trace, 'primary', '... got the right return value from method');
29 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
30 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
31 isa_ok($wrapped, 'Class::MOP::Method');
34 is($trace, 'primary', '... got the right return value from the wrapped method');
38 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
39 } '... added the before modifier okay';
42 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
46 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
47 } '... added the after modifier okay';
50 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
56 my $method = Class::MOP::Method->wrap(
58 package_name => 'main',
61 isa_ok($method, 'Class::MOP::Method');
63 is($method->(), 4, '... got the right value from the wrapped method');
65 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
66 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
67 isa_ok($wrapped, 'Class::MOP::Method');
69 is($wrapped->(), 4, '... got the right value from the wrapped method');
72 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });
73 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
74 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });
75 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
76 } '... added the around modifier okay';
81 '... got the right results back from the around methods (in list context)');
83 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
89 my $method = Class::MOP::Method->wrap(
90 sub { push @tracelog => 'primary' },
91 package_name => 'main',
94 isa_ok($method, 'Class::MOP::Method');
96 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
97 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
98 isa_ok($wrapped, 'Class::MOP::Method');
101 $wrapped->add_before_modifier(sub { push @tracelog => 'before 1' });
102 $wrapped->add_before_modifier(sub { push @tracelog => 'before 2' });
103 $wrapped->add_before_modifier(sub { push @tracelog => 'before 3' });
104 } '... added the before modifier okay';
107 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });
108 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
109 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });
110 } '... added the around modifier okay';
113 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
114 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
115 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
116 } '... added the after modifier okay';
122 'before 3', 'before 2', 'before 1', # last-in-first-out order
123 'around 3', 'around 2', 'around 1', # last-in-first-out order
125 'after 1', 'after 2', 'after 3', # first-in-first-out order
127 '... got the right tracelog from all our before/around/after methods');