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
|
#!perl
use strict;
use utf8;
use open qw( :utf8 :std );
use Test::More tests => 22;
use XS::APItest;
# This test must happen outside of any warnings scope
{
local $^W;
my $w;
local $SIG{__WARN__} = sub { $w .= shift };
sub frimple() { 78 }
newCONSTSUB_flags(\%::, "frimple", 0, undef);
like $w, qr/Constant subroutine frimple redefined at /,
'newCONSTSUB constant redefinition warning is unaffected by $^W=0';
undef $w;
newCONSTSUB_flags(\%::, "frimple", 0, undef);
is $w, undef, '...unless the const SVs are the same';
eval 'sub frimple() { 78 }';
undef $w;
newCONSTSUB_flags(\%::, "frimple", 0, "78");
is $w, undef, '...or the const SVs have the same value';
}
use warnings;
my ($const, $glob) =
XS::APItest::newCONSTSUB(\%::, "sanity_check", 0, undef);
ok $const;
ok *{$glob}{CODE};
($const, $glob) =
XS::APItest::newCONSTSUB(\%::, "\x{30cb}", 0, undef);
ok $const, "newCONSTSUB generates the constant,";
ok *{$glob}{CODE}, "..and the glob,";
ok !$::{"\x{30cb}"}, "...but not the right one";
($const, $glob) =
XS::APItest::newCONSTSUB_flags(\%::, "\x{30cd}", 0, undef);
ok $const, "newCONSTSUB_flags generates the constant,";
ok *{$glob}{CODE}, "..and the glob,";
ok $::{"\x{30cd}"}, "...the right one!";
eval q{
BEGIN {
no warnings;
my $w;
local $SIG{__WARN__} = sub { $w .= shift };
*foo = sub(){123};
newCONSTSUB_flags(\%::, "foo", 0, undef);
is $w, undef, 'newCONSTSUB uses calling scope for redefinition warnings';
}
};
{
no strict 'refs';
*{"foo::\x{100}"} = sub(){return 123};
my $w;
local $SIG{__WARN__} = sub { $w .= shift };
newCONSTSUB_flags(\%foo::, "\x{100}", 0, undef);
like $w, qr/Subroutine \x{100} redefined at /,
'newCONSTSUB redefinition warning + utf8';
undef $w;
newCONSTSUB_flags(\%foo::, "\x{100}", 0, 54);
like $w, qr/Constant subroutine \x{100} redefined at /,
'newCONSTSUB constant redefinition warning + utf8';
}
# XS::APItest was not handling references correctly here
package Counter {
our $count = 0;
sub new {
++$count;
my $o = bless [];
return $o;
}
sub DESTROY {
--$count;
}
};
foreach (['newCONSTSUB', 'ZZIP'],
['newCONSTSUB_flags', 'BRRRAPP']) {
my ($using, $name) = @$_;
is($Counter::count, 0, 'No objects exist before we start');
my $sub = XS::APItest->can($using);
($const, $glob) = $sub->(\%::, $name, 0, Counter->new());
is($const, 1, "subroutine generated by $using is CvCONST");
is($Counter::count, 1, '1 object now exists');
{
no warnings 'redefine';
*$glob = sub () {};
}
is($Counter::count, 0, 'no objects remain');
}
|