File: basic.t

package info (click to toggle)
libdevel-overloadinfo-perl 0.007-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: perl: 392; makefile: 8
file content (170 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More 0.88;

use Devel::OverloadInfo qw(overload_info overload_op_info is_overloaded);

sub MyModule::negate { -$_[0] }

my $num_sub;
BEGIN { $num_sub = sub { 0 } };
{
    package  # hide from PAUSE
        BaseClass;
    use overload (
        '""' => 'stringify',
        bool => 'boolify',
        '0+' => $num_sub,
    );

    sub boolify { 1 }
}

{
    package # hide from PAUSE
        ChildClass;
    use parent -norequire => 'BaseClass';

    use overload (
        neg => \&MyModule::negate,
        fallback => 1,
    );

    sub stringify { "foo" }
}

{
    package # hide from PAUSE
        EmptyOverload;
    use overload;
}

{
    package # hide from PAUSE
        InheritedOnly;
    use parent -norequire => 'BaseClass';
}

{
    package # hide from PAUSE
        NoOverload;

    sub wibble {}
}

for my $class (qw(BaseClass ChildClass EmptyOverload InheritedOnly)) {
    ok is_overloaded($class), "$class is overloaded";
}

for my $class (qw(NoOverload)) {
    ok !is_overloaded($class), "$class is not overloaded";
}

is overload_op_info('BaseClass', 'cmp'), undef,
    'overload_op_info() returns undef for non-overloaded op';

my $boi = overload_info('BaseClass');

# Whether undef fallback exists varies between perl versions
if (my $fallback = delete $boi->{fallback}) {
    is_deeply $fallback, {
        class => 'BaseClass',
        value => undef,
    }, 'BaseClass fallback is undef';
    is_deeply overload_op_info('BaseClass', 'fallback'),
        $fallback,
        "overload_op_info('BaseClass','fallback') matches overload_info()";
}
else {
    is overload_op_info('BaseClass', 'fallback'), undef,
        "overload_op_info('BaseClass','fallback') matches overload_info()";
}

is_deeply $boi,
    {
        '""' => {
            class => 'BaseClass',
            method_name => 'stringify',
        },
        bool => {
            class => 'BaseClass',
            method_name => 'boolify',
            code_class => 'BaseClass',
            code => \&BaseClass::boolify,
            code_name => "BaseClass::boolify",
        },
        '0+' => {
            class => 'BaseClass',
            code => $num_sub,
            code_name => 'main::__ANON__',
        },
    },
    "BaseClass overload info" or note explain $boi;

my $coi = overload_info('ChildClass');

is_deeply $coi,
    {
        fallback => {
            class => 'ChildClass',
            value => 1,
        },
        '""' => {
            class => 'BaseClass',
            method_name => 'stringify',
            code_class => 'ChildClass',
            code => \&ChildClass::stringify,
            code_name => 'ChildClass::stringify',
        },
        bool => {
            class => 'BaseClass',
            method_name => 'boolify',
            code_class => 'BaseClass',
            code => \&BaseClass::boolify,
            code_name => "BaseClass::boolify",
        },
        '0+' => {
            class => 'BaseClass',
            code => $num_sub,
            code_name => 'main::__ANON__',
        },
        neg => {
            class => 'ChildClass',
            code => \&MyModule::negate,
            code_name => 'MyModule::negate',
        },
    },
    "ChildClass overload info" or note explain $coi;

for my $op (sort keys %$coi) {
    is_deeply overload_op_info('ChildClass', $op),
        $coi->{$op},
        "overload_op_info('ChildClass', $op)";
}


is_deeply overload_info('InheritedOnly'),
    overload_info('BaseClass'),
    'InheritedOnly has same overloads as BaseClass';

is overload_op_info('NoOverload', 'fallback'), undef,
    'overload_op_info on non-overloaded class';

is_deeply overload_info('NoOverload'), {},
    'NoOverload has no overloads';

my $eoi = overload_info('EmptyOverload');

# Whether undef fallback exists varies between perl versions
if (my $fallback = delete $eoi->{fallback}) {
    is_deeply $fallback, {
        class => 'EmptyOverload',
        value => undef,
    }, 'EmptyOverload fallback is undef';
}

is_deeply $eoi, {},
    'EmptyOverload has no overloads';

done_testing;