Commit | Line | Data |
342c0218 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Class::MOP; |
5 | use Class::MOP::Class; |
86a4d873 |
6 | use Test::More; |
342c0218 |
7 | use Test::Exception; |
8 | |
9 | my %results; |
10 | |
11 | { |
76e0d9d9 |
12 | |
342c0218 |
13 | package Base; |
14 | use metaclass; |
15 | sub hey { $results{base}++ } |
16 | } |
17 | |
18 | for 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' ); |
342c0218 |
32 | lives_ok { |
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' |
36 | } |
37 | 'wrapped doesn\'t die when $_ gets changed'; |
38 | is_deeply( |
39 | \%results, { base => 2, wrapped => 2 }, |
40 | 'saw expected calls to wrappers' |
41 | ); |
342c0218 |
42 | } |
43 | |
44 | { |
45 | my $meta = Class::MOP::Class->create_anon_class( |
76e0d9d9 |
46 | superclasses => [ 'Base', 'Class::MOP::Object' ] ); |
342c0218 |
47 | for my $wrap (qw(before after)) { |
48 | my $alter = "add_${wrap}_method_modifier"; |
76e0d9d9 |
49 | $meta->$alter( |
50 | 'hey' => sub { |
51 | $results{wrapped}++; |
52 | $_ = 'barf'; # 'barf' would replace the cached wrapper subref |
53 | } |
54 | ); |
342c0218 |
55 | } |
56 | |
57 | %results = (); |
58 | my $o = $meta->get_meta_instance->create_instance; |
76e0d9d9 |
59 | isa_ok( $o, 'Base' ); |
342c0218 |
60 | lives_ok { |
61 | $o->hey; |
76e0d9d9 |
62 | $o->hey |
63 | ; # this would die with 'Can't use string ("barf") as a subroutine ref while "strict refs" in use' |
64 | } |
65 | 'double-wrapped doesn\'t die when $_ gets changed'; |
66 | is_deeply( |
67 | \%results, { base => 2, wrapped => 4 }, |
68 | 'saw expected calls to wrappers' |
69 | ); |
342c0218 |
70 | } |
86a4d873 |
71 | |
72 | done_testing; |