Debian Codegraph @

cpan/Scalar-List-Utils/t/scalarutil-proto.t

raw
1 #!./perl
2
3 use strict;
4 use warnings;
5
6 use Scalar::Util ();
7 use Test::More (grep { /set_prototype/ } @Scalar::Util::EXPORT_FAIL)
8 ? (skip_all => 'set_prototype requires XS version')
9 : (tests => 14);
10
11 Scalar::Util->import('set_prototype');
12
13 sub f { }
14 is( prototype('f'), undef, 'no prototype');
15
16 my $r = set_prototype(\&f,'$');
17 is( prototype('f'), '$', 'set prototype');
18 is( $r, \&f, 'return value');
19
20 set_prototype(\&f,undef);
21 is( prototype('f'), undef, 'remove prototype');
22
23 set_prototype(\&f,'');
24 is( prototype('f'), '', 'empty prototype');
25
26 sub g (@) { }
27 is( prototype('g'), '@', '@ prototype');
28
29 set_prototype(\&g,undef);
30 is( prototype('g'), undef, 'remove prototype');
31
32 sub stub;
33 is( prototype('stub'), undef, 'non existing sub');
34
35 set_prototype(\&stub,'$$$');
36 is( prototype('stub'), '$$$', 'change non existing sub');
37
38 sub f_decl ($$$$);
39 is( prototype('f_decl'), '$$$$', 'forward declaration');
40
41 set_prototype(\&f_decl,'\%');
42 is( prototype('f_decl'), '\%', 'change forward declaration');
43
44 eval { &set_prototype( 'f', '' ); };
45 print "not " unless
46 ok($@ =~ /^set_prototype: not a reference/, 'not a reference');
47
48 eval { &set_prototype( \'f', '' ); };
49 ok($@ =~ /^set_prototype: not a subroutine reference/, 'not a sub reference');
50
51 # RT 72080
52
53 {
54 package TiedCV;
55 sub TIESCALAR {
56 my $class = shift;
57 return bless {@_}, $class;
58 }
59 sub FETCH {
60 return \&my_subr;
61 }
62 sub my_subr {
63 }
64 }
65
66 my $cv;
67 tie $cv, 'TiedCV';
68
69 &Scalar::Util::set_prototype($cv, '$$');
70 is( prototype($cv), '$$', 'set_prototype() on tied CV ref' );