xref: /openbsd-src/gnu/usr.bin/perl/t/mro/next_method.t (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1850e2753Smillert#!/usr/bin/perl
2850e2753Smillert
3850e2753Smillertuse strict;
4850e2753Smillertuse warnings;
5850e2753Smillert
6850e2753Smillertrequire q(./test.pl); plan(tests => 5);
7850e2753Smillert
8850e2753Smillert=pod
9850e2753Smillert
10*898184e3SsthenThis tests the classic diamond inheritance pattern.
11850e2753Smillert
12850e2753Smillert   <A>
13850e2753Smillert  /   \
14850e2753Smillert<B>   <C>
15850e2753Smillert  \   /
16850e2753Smillert   <D>
17850e2753Smillert
18850e2753Smillert=cut
19850e2753Smillert
20850e2753Smillert{
21850e2753Smillert    package Diamond_A;
22850e2753Smillert    use mro 'c3';
23850e2753Smillert    sub hello { 'Diamond_A::hello' }
24850e2753Smillert    sub foo { 'Diamond_A::foo' }
25850e2753Smillert}
26850e2753Smillert{
27850e2753Smillert    package Diamond_B;
28850e2753Smillert    use base 'Diamond_A';
29850e2753Smillert    use mro 'c3';
30850e2753Smillert    sub foo { 'Diamond_B::foo => ' . (shift)->next::method() }
31850e2753Smillert}
32850e2753Smillert{
33850e2753Smillert    package Diamond_C;
34850e2753Smillert    use mro 'c3';
35850e2753Smillert    use base 'Diamond_A';
36850e2753Smillert
37850e2753Smillert    sub hello { 'Diamond_C::hello => ' . (shift)->next::method() }
38850e2753Smillert    sub foo { 'Diamond_C::foo => ' . (shift)->next::method() }
39850e2753Smillert}
40850e2753Smillert{
41850e2753Smillert    package Diamond_D;
42850e2753Smillert    use base ('Diamond_B', 'Diamond_C');
43850e2753Smillert    use mro 'c3';
44850e2753Smillert
45850e2753Smillert    sub foo { 'Diamond_D::foo => ' . (shift)->next::method() }
46850e2753Smillert}
47850e2753Smillert
48850e2753Smillertok(eq_array(
49850e2753Smillert    mro::get_linear_isa('Diamond_D'),
50850e2753Smillert    [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ]
51850e2753Smillert), '... got the right MRO for Diamond_D');
52850e2753Smillert
53850e2753Smillertis(Diamond_D->hello, 'Diamond_C::hello => Diamond_A::hello', '... method resolved itself as expected');
54850e2753Smillert
55850e2753Smillertis(Diamond_D->can('hello')->('Diamond_D'),
56850e2753Smillert   'Diamond_C::hello => Diamond_A::hello',
57850e2753Smillert   '... can(method) resolved itself as expected');
58850e2753Smillert
59850e2753Smillertis(UNIVERSAL::can("Diamond_D", 'hello')->('Diamond_D'),
60850e2753Smillert   'Diamond_C::hello => Diamond_A::hello',
61850e2753Smillert   '... can(method) resolved itself as expected');
62850e2753Smillert
63850e2753Smillertis(Diamond_D->foo,
64850e2753Smillert    'Diamond_D::foo => Diamond_B::foo => Diamond_C::foo => Diamond_A::foo',
65850e2753Smillert    '... method foo resolved itself as expected');
66