Commit | Line | Data |
5d5c86d9 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2ffffc6d |
6 | use Test::More tests => 12; |
5d5c86d9 |
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'); |
2ffffc6d |
39 | } |
5d5c86d9 |
40 | |
5d5c86d9 |
41 | my $bar = Bar->new(); |
42 | isa_ok($bar, 'Bar'); |
08c29211 |
43 | isa_ok($bar, 'Foo'); |
44 | |
2ffffc6d |
45 | # test it working with with Sub::Name |
08c29211 |
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 | |
2ffffc6d |
57 | Class::C3::initialize(); |
58 | |
08c29211 |
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 | } |
2ffffc6d |
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 | } |
5d5c86d9 |
90 | } |