1#!/usr/bin/perl
2
3use strict;
4BEGIN {
5	$|  = 1;
6	$^W = 1;
7	$ENV{PERL_PARAMS_UTIL_PP} ||= 1;
8}
9
10sub _CODELIKE($);
11
12use Test::More;
13use File::Spec::Functions ':ALL';
14use Scalar::Util qw(
15	blessed
16	reftype
17	refaddr
18);
19use overload;
20
21sub c_ok { is(
22	refaddr(_CODELIKE($_[0])),
23	refaddr($_[0]),
24	"callable: $_[1]",
25) }
26
27sub nc_ok {
28	my $left = shift;
29	$left = _CODELIKE($left);
30	is( $left, undef, "not callable: $_[0]" );
31}
32
33my @callables = (
34	"callable itself"                         => \&_CODELIKE,
35	"a boring plain code ref"                 => sub {},
36	'an object with overloaded &{}'           => C::O->new,
37	'a object build from a coderef'           => C::C->new,
38	'an object with inherited overloaded &{}' => C::O::S->new, 
39	'a coderef blessed into CODE'             => (bless sub {} => 'CODE'),
40);
41
42my @uncallables = (
43	"undef"                                   => undef,
44	"a string"                                => "a string",
45	"a number"                                => 19780720,
46	"a ref to a ref to code"                  => \(sub {}),
47	"a boring plain hash ref"                 => {},
48	'a class that builds from coderefs'       => "C::C",
49	'a class with overloaded &{}'             => "C::O",
50	'a class with inherited overloaded &{}'   => "C::O::S",
51	'a plain boring hash-based object'        => UC->new,
52	'a non-coderef blessed into CODE'         => (bless {} => 'CODE'),
53);
54
55my $tests = (@callables + @uncallables) / 2 + 2;
56
57if ( $] > 5.006 ) {
58	push @uncallables, 'a regular expression', qr/foo/;
59	$tests += 1;
60}
61
62plan tests => $tests;
63
64# Import the function
65use_ok( 'Params::Util', '_CODELIKE' );
66ok( defined *_CODELIKE{CODE}, '_CODELIKE imported ok' );
67
68while ( @callables ) {
69	my $name   = shift @callables;
70	my $object = shift @callables;
71	c_ok( $object, $name );
72}
73
74while ( @uncallables ) {
75	my $name   = shift @uncallables;
76	my $object = shift @uncallables;
77	nc_ok( $object, $name );
78}
79
80
81
82
83
84######################################################################
85# callable: is a blessed code ref
86
87package C::C;
88
89sub new {
90	bless sub {} => shift;
91}
92
93
94
95
96
97######################################################################
98# callable: overloads &{}
99# but only objects are callable, not class
100
101package C::O;
102
103sub new {
104	bless {} => shift;
105}
106use overload '&{}'  => sub { sub {} };
107use overload 'bool' => sub () { 1 };
108
109
110
111
112
113######################################################################
114# callable: subclasses C::O
115
116package C::O::S;
117
118use vars qw{@ISA};
119BEGIN {
120	@ISA = 'C::O';
121}
122
123
124
125
126
127######################################################################
128# uncallable: some boring object with no codey magic
129
130package UC;
131
132sub new {
133	bless {} => shift;
134}
135