Fix some bizarro code left behind by a much earlier change.
[gitmo/Class-MOP.git] / t / 031_method_modifiers.t
CommitLineData
de19f115 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
ddc8edba 6use Test::More tests => 26;
de19f115 7use Test::Exception;
8
9BEGIN {
10 use_ok('Class::MOP');
11 use_ok('Class::MOP::Method');
12}
13
855d2774 14# test before and afters
15{
16 my $trace = '';
de19f115 17
b38f3848 18 my $method = Class::MOP::Method->wrap(
5caf45ce 19 body => sub { $trace .= 'primary' },
b38f3848 20 package_name => 'main',
21 name => '__ANON__',
22 );
855d2774 23 isa_ok($method, 'Class::MOP::Method');
de19f115 24
855d2774 25 $method->();
26 is($trace, 'primary', '... got the right return value from method');
27 $trace = '';
de19f115 28
ddc8edba 29 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
30 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
855d2774 31 isa_ok($wrapped, 'Class::MOP::Method');
32
33 $wrapped->();
34 is($trace, 'primary', '... got the right return value from the wrapped method');
35 $trace = '';
36
37 lives_ok {
38 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
39 } '... added the before modifier okay';
40
41 $wrapped->();
42 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
43 $trace = '';
44
45 lives_ok {
46 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
47 } '... added the after modifier okay';
48
49 $wrapped->();
50 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
51 $trace = '';
52}
53
54# test around method
55{
b38f3848 56 my $method = Class::MOP::Method->wrap(
57 sub { 4 },
58 package_name => 'main',
59 name => '__ANON__',
60 );
855d2774 61 isa_ok($method, 'Class::MOP::Method');
62
63 is($method->(), 4, '... got the right value from the wrapped method');
64
ddc8edba 65 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
66 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
855d2774 67 isa_ok($wrapped, 'Class::MOP::Method');
68
69 is($wrapped->(), 4, '... got the right value from the wrapped method');
70
71 lives_ok {
72 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });
73 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
74 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });
8768d570 75 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
855d2774 76 } '... added the around modifier okay';
77
78 is_deeply(
79 [ $wrapped->() ],
8768d570 80 [ 0, 1, 2, 3, 4 ],
81 '... got the right results back from the around methods (in list context)');
82
83 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
855d2774 84}
de19f115 85
ee5e71d4 86{
87 my @tracelog;
88
b38f3848 89 my $method = Class::MOP::Method->wrap(
90 sub { push @tracelog => 'primary' },
91 package_name => 'main',
92 name => '__ANON__',
93 );
ee5e71d4 94 isa_ok($method, 'Class::MOP::Method');
95
ddc8edba 96 my $wrapped = Class::MOP::Method::Wrapped->wrap($method);
97 isa_ok($wrapped, 'Class::MOP::Method::Wrapped');
ee5e71d4 98 isa_ok($wrapped, 'Class::MOP::Method');
99
100 lives_ok {
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';
105
106 lives_ok {
96ceced8 107 $wrapped->add_around_modifier(sub { push @tracelog => 'around 1'; $_[0]->(); });
ee5e71d4 108 $wrapped->add_around_modifier(sub { push @tracelog => 'around 2'; $_[0]->(); });
96ceced8 109 $wrapped->add_around_modifier(sub { push @tracelog => 'around 3'; $_[0]->(); });
ee5e71d4 110 } '... added the around modifier okay';
111
112 lives_ok {
96ceced8 113 $wrapped->add_after_modifier(sub { push @tracelog => 'after 1' });
ee5e71d4 114 $wrapped->add_after_modifier(sub { push @tracelog => 'after 2' });
96ceced8 115 $wrapped->add_after_modifier(sub { push @tracelog => 'after 3' });
ee5e71d4 116 } '... added the after modifier okay';
117
118 $wrapped->();
119 is_deeply(
120 \@tracelog,
121 [
122 'before 3', 'before 2', 'before 1', # last-in-first-out order
96ceced8 123 'around 3', 'around 2', 'around 1', # last-in-first-out order
ee5e71d4 124 'primary',
96ceced8 125 'after 1', 'after 2', 'after 3', # first-in-first-out order
ee5e71d4 126 ],
127 '... got the right tracelog from all our before/around/after methods');
128}
de19f115 129
de19f115 130
de19f115 131