also need to remove methods from the map when it's invalidated
[gitmo/Class-MOP.git] / t / 313_before_after_dollar_under.t
CommitLineData
342c0218 1use strict;
2use warnings;
3
4use Class::MOP;
5use Class::MOP::Class;
86a4d873 6use Test::More;
871e9eb5 7use Test::Fatal;
342c0218 8
9my %results;
10
11{
76e0d9d9 12
342c0218 13 package Base;
14 use metaclass;
15 sub hey { $results{base}++ }
16}
17
18for my $wrap (qw(before after)) {
19 my $meta = Class::MOP::Class->create_anon_class(
76e0d9d9 20 superclasses => [ 'Base', 'Class::MOP::Object' ] );
342c0218 21 my $alter = "add_${wrap}_method_modifier";
76e0d9d9 22 $meta->$alter(
23 'hey' => sub {
24 $results{wrapped}++;
25 $_ = 'barf'; # 'barf' would replace the cached wrapper subref
26 }
27 );
342c0218 28
29 %results = ();
30 my $o = $meta->get_meta_instance->create_instance;
76e0d9d9 31 isa_ok( $o, 'Base' );
871e9eb5 32 is( exception {
342c0218 33 $o->hey;
76e0d9d9 34 $o->hey
35 ; # this would die with 'Can't use string ("barf") as a subroutine ref while "strict refs" in use'
871e9eb5 36 }, undef, 'wrapped doesn\'t die when $_ gets changed' );
76e0d9d9 37 is_deeply(
38 \%results, { base => 2, wrapped => 2 },
39 'saw expected calls to wrappers'
40 );
342c0218 41}
42
43{
44 my $meta = Class::MOP::Class->create_anon_class(
76e0d9d9 45 superclasses => [ 'Base', 'Class::MOP::Object' ] );
342c0218 46 for my $wrap (qw(before after)) {
47 my $alter = "add_${wrap}_method_modifier";
76e0d9d9 48 $meta->$alter(
49 'hey' => sub {
50 $results{wrapped}++;
51 $_ = 'barf'; # 'barf' would replace the cached wrapper subref
52 }
53 );
342c0218 54 }
55
56 %results = ();
57 my $o = $meta->get_meta_instance->create_instance;
76e0d9d9 58 isa_ok( $o, 'Base' );
871e9eb5 59 is( exception {
342c0218 60 $o->hey;
76e0d9d9 61 $o->hey
62 ; # this would die with 'Can't use string ("barf") as a subroutine ref while "strict refs" in use'
871e9eb5 63 }, undef, 'double-wrapped doesn\'t die when $_ gets changed' );
76e0d9d9 64 is_deeply(
65 \%results, { base => 2, wrapped => 4 },
66 'saw expected calls to wrappers'
67 );
342c0218 68}
86a4d873 69
70done_testing;