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