preserving call context
[gitmo/Class-MOP.git] / t / 031_method_modifiers.t
CommitLineData
de19f115 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 18;
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
855d2774 18 my $method = Class::MOP::Method->new(sub { $trace .= 'primary' });
19 isa_ok($method, 'Class::MOP::Method');
de19f115 20
855d2774 21 $method->();
22 is($trace, 'primary', '... got the right return value from method');
23 $trace = '';
de19f115 24
855d2774 25 my $wrapped = $method->wrap();
26 isa_ok($wrapped, 'Class::MOP::Method');
27
28 $wrapped->();
29 is($trace, 'primary', '... got the right return value from the wrapped method');
30 $trace = '';
31
32 lives_ok {
33 $wrapped->add_before_modifier(sub { $trace .= 'before -> ' });
34 } '... added the before modifier okay';
35
36 $wrapped->();
37 is($trace, 'before -> primary', '... got the right return value from the wrapped method (w/ before)');
38 $trace = '';
39
40 lives_ok {
41 $wrapped->add_after_modifier(sub { $trace .= ' -> after' });
42 } '... added the after modifier okay';
43
44 $wrapped->();
45 is($trace, 'before -> primary -> after', '... got the right return value from the wrapped method (w/ before)');
46 $trace = '';
47}
48
49# test around method
50{
51 my $method = Class::MOP::Method->new(sub { 4 });
52 isa_ok($method, 'Class::MOP::Method');
53
54 is($method->(), 4, '... got the right value from the wrapped method');
55
56 my $wrapped = $method->wrap;
57 isa_ok($wrapped, 'Class::MOP::Method');
58
59 is($wrapped->(), 4, '... got the right value from the wrapped method');
60
61 lives_ok {
62 $wrapped->add_around_modifier(sub { (3, $_[0]->()) });
63 $wrapped->add_around_modifier(sub { (2, $_[0]->()) });
64 $wrapped->add_around_modifier(sub { (1, $_[0]->()) });
8768d570 65 $wrapped->add_around_modifier(sub { (0, $_[0]->()) });
855d2774 66 } '... added the around modifier okay';
67
68 is_deeply(
69 [ $wrapped->() ],
8768d570 70 [ 0, 1, 2, 3, 4 ],
71 '... got the right results back from the around methods (in list context)');
72
73 is(scalar $wrapped->(), 4, '... got the right results back from the around methods (in scalar context)');
855d2774 74}
de19f115 75
de19f115 76
de19f115 77
de19f115 78
de19f115 79