add tests for wrapping a method metaobject
[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;
6use Test::More qw/no_plan/;
7use Test::Exception;
8
9my %results;
10
11{
12 package Base;
13 use metaclass;
14 sub hey { $results{base}++ }
15}
16
17for my $wrap (qw(before after)) {
18 my $meta = Class::MOP::Class->create_anon_class(
19 superclasses => ['Base', 'Class::MOP::Object']
20 );
21 my $alter = "add_${wrap}_method_modifier";
22 $meta->$alter('hey' => sub {
23 $results{wrapped}++;
24 $_ = 'barf'; # 'barf' would replace the cached wrapper subref
25 });
26
27 %results = ();
28 my $o = $meta->get_meta_instance->create_instance;
29 isa_ok($o, 'Base');
30 lives_ok {
31 $o->hey;
32 $o->hey; # this would die with 'Can't use string ("barf") as a subroutine ref while "strict refs" in use'
33 } 'wrapped doesn\'t die when $_ gets changed';
34 is_deeply(\%results, {base=>2,wrapped=>2});
35}
36
37{
38 my $meta = Class::MOP::Class->create_anon_class(
39 superclasses => ['Base', 'Class::MOP::Object']
40 );
41 for my $wrap (qw(before after)) {
42 my $alter = "add_${wrap}_method_modifier";
43 $meta->$alter('hey' => sub {
44 $results{wrapped}++;
45 $_ = 'barf'; # 'barf' would replace the cached wrapper subref
46 });
47 }
48
49 %results = ();
50 my $o = $meta->get_meta_instance->create_instance;
51 isa_ok($o, 'Base');
52 lives_ok {
53 $o->hey;
54 $o->hey; # this would die with 'Can't use string ("barf") as a subroutine ref while "strict refs" in use'
55 } 'double-wrapped doesn\'t die when $_ gets changed';
56 is_deeply(\%results, {base=>2,wrapped=>4});
57}