xref: /openbsd/gnu/usr.bin/perl/t/mro/next_skip.t (revision cecf84d4)
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6require q(./test.pl); plan(tests => 10);
7
8=pod
9
10This tests the classic diamond inheritance pattern.
11
12   <A>
13  /   \
14<B>   <C>
15  \   /
16   <D>
17
18=cut
19
20{
21    package Diamond_A;
22    use mro 'c3';
23    sub bar { 'Diamond_A::bar' }
24    sub baz { 'Diamond_A::baz' }
25}
26{
27    package Diamond_B;
28    use base 'Diamond_A';
29    use mro 'c3';
30    sub baz { 'Diamond_B::baz => ' . (shift)->next::method() }
31}
32{
33    package Diamond_C;
34    use mro 'c3';
35    use base 'Diamond_A';
36    sub foo { 'Diamond_C::foo' }
37    sub buz { 'Diamond_C::buz' }
38
39    sub woz { 'Diamond_C::woz' }
40    sub maybe { 'Diamond_C::maybe' }
41}
42{
43    package Diamond_D;
44    use base ('Diamond_B', 'Diamond_C');
45    use mro 'c3';
46    sub foo { 'Diamond_D::foo => ' . (shift)->next::method() }
47    sub bar { 'Diamond_D::bar => ' . (shift)->next::method() }
48    sub buz { 'Diamond_D::buz => ' . (shift)->baz() }
49    sub fuz { 'Diamond_D::fuz => ' . (shift)->next::method() }
50
51    sub woz { 'Diamond_D::woz can => ' . ((shift)->next::can() ? 1 : 0) }
52    sub noz { 'Diamond_D::noz can => ' . ((shift)->next::can() ? 1 : 0) }
53
54    sub maybe { 'Diamond_D::maybe => ' . ((shift)->maybe::next::method() || 0) }
55    sub moybe { 'Diamond_D::moybe => ' . ((shift)->maybe::next::method() || 0) }
56
57}
58
59ok(eq_array(
60    mro::get_linear_isa('Diamond_D'),
61    [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ]
62), '... got the right MRO for Diamond_D');
63
64is(Diamond_D->foo, 'Diamond_D::foo => Diamond_C::foo', '... skipped B and went to C correctly');
65is(Diamond_D->bar, 'Diamond_D::bar => Diamond_A::bar', '... skipped B & C and went to A correctly');
66is(Diamond_D->baz, 'Diamond_B::baz => Diamond_A::baz', '... called B method, skipped C and went to A correctly');
67is(Diamond_D->buz, 'Diamond_D::buz => Diamond_B::baz => Diamond_A::baz', '... called D method dispatched to , different method correctly');
68eval { Diamond_D->fuz };
69like($@, qr/^No next::method 'fuz' found for Diamond_D/, '... cannot re-dispatch to a method which is not there');
70
71is(Diamond_D->woz, 'Diamond_D::woz can => 1', '... can re-dispatch figured out correctly');
72is(Diamond_D->noz, 'Diamond_D::noz can => 0', '... cannot re-dispatch figured out correctly');
73
74is(Diamond_D->maybe, 'Diamond_D::maybe => Diamond_C::maybe', '... redispatched D to C when it exists');
75is(Diamond_D->moybe, 'Diamond_D::moybe => 0', '... quietly failed redispatch from D');
76