5af7004ce0c6ec0914d16da1ae2276d140c7103a
[gitmo/Class-C3-XS.git] / t / 32_next_method_edge_cases.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 12;
7
8 BEGIN {   
9     use_ok('Class::C3');
10 }
11
12 {
13
14     {
15         package Foo;
16         use strict;
17         use warnings;
18         use Class::C3;
19         sub new { bless {}, $_[0] }
20         sub bar { 'Foo::bar' }
21     }
22
23     # call the submethod in the direct instance
24
25     my $foo = Foo->new();
26     isa_ok($foo, 'Foo');
27
28     can_ok($foo, 'bar');
29     is($foo->bar(), 'Foo::bar', '... got the right return value');    
30
31     # fail calling it from a subclass
32
33     {
34         package Bar;
35         use strict;
36         use warnings;
37         use Class::C3;
38         our @ISA = ('Foo');
39     }  
40     
41     my $bar = Bar->new();
42     isa_ok($bar, 'Bar');
43     isa_ok($bar, 'Foo');    
44     
45     # test it working with with Sub::Name
46     SKIP: {    
47         eval 'use Sub::Name';
48         skip "Sub::Name is required for this test", 3 if $@;
49     
50         my $m = sub { (shift)->next::method() };
51         Sub::Name::subname('Bar::bar', $m);
52         {
53             no strict 'refs';
54             *{'Bar::bar'} = $m;
55         }
56
57         Class::C3::initialize();  
58
59         can_ok($bar, 'bar');
60         my $value = eval { $bar->bar() };
61         ok(!$@, '... calling bar() succedded') || diag $@;
62         is($value, 'Foo::bar', '... got the right return value too');
63     }
64     
65     # test it failing without Sub::Name
66     {
67         package Baz;
68         use strict;
69         use warnings;
70         use Class::C3;
71         our @ISA = ('Foo');
72     }      
73     
74     my $baz = Baz->new();
75     isa_ok($baz, 'Baz');
76     isa_ok($baz, 'Foo');    
77     
78     {
79         my $m = sub { (shift)->next::method() };
80         {
81             no strict 'refs';
82             *{'Baz::bar'} = $m;
83         }
84
85         Class::C3::initialize();  
86
87         eval { $baz->bar() };
88         ok($@, '... calling bar() with next::method failed') || diag $@;
89     }    
90 }