File: is_callable.t

package info (click to toggle)
libdata-munge-perl 0.111-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116 kB
  • sloc: perl: 258; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
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
#!perl
use Test2::V0;
use Data::Munge;

{
    package OverloadedFuncall;
    use overload (
        '&{}'    => sub { $_[0]->to_coderef },
        fallback => 1,
    );

    sub new {
        my $class = shift;
        bless {@_}, $class
    }

    sub to_coderef {
        my $self = shift;
        sub { $self->{value} }
    }
}

ok !is_callable(undef), "undef is not callable";
ok !is_callable(42), "a number is not callable";
ok !is_callable(\$_), "a scalar ref is not callable";
ok !is_callable([]), "an arrayref is not callable";
ok !is_callable({}), "a hashref is not callable";
ok !is_callable(*is_callable), "a glob is not callable";
ok !is_callable(\*is_callable), "a globref is not callable";
ok !is_callable(qr/./), "a regex is not callable";
ok !is_callable("a random string"), "a random string is not callable";
ok !is_callable("main::is_callable"), "a sub name is not callable";
ok is_callable(sub { die; }), "an anon sub ref is callable";
ok is_callable(\&is_callable), "a named sub ref is callable";
ok is_callable(\&no_such_sub), "an undefined sub is deemed callable";
ok is_callable(bless sub {}, 'SomeRandomClass'), "a blessed sub is callable";
{
    my $obj = OverloadedFuncall->new(value => 42);
    is $obj->(), 42, "sanity check: overloaded function call returns 42";
    ok is_callable($obj), "an object with overloaded &{} is callable";
}

done_testing;