5 use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
9 # This is a mess. The stash can supposedly handle Unicode but the behavior
10 # is literally undefined before 5.16 (with crashes beyond the basic plane),
11 # and remains unclear past 5.16 with evalbytes and feature unicode_eval
12 # In any case - Sub::Name needs to *somehow* work with this, so we will do
13 # a heuristic with ambiguous eval and looking for octets in the stash
14 use if "$]" >= 5.016, feature => 'unicode_eval';
17 my $builder = Test::More->builder;
18 binmode $builder->output, ":encoding(utf8)";
19 binmode $builder->failure_output, ":encoding(utf8)";
20 binmode $builder->todo_output, ":encoding(utf8)";
23 sub compile_named_sub {
24 my ( $fullname, $body ) = @_;
25 my $sub = eval "sub $fullname { $body }" . '\\&{$fullname}';
33 my ( $sub, $expected, $type, $ord ) = @_;
35 local $Test::Builder::Level = $Test::Builder::Level + 1;
37 my $for_what = sprintf "when it contains \\x%s ( %s )", (
39 ? sprintf "{%X}", $ord
40 : sprintf "%02X", $ord
43 $ord > 255 ? unpack('H*', pack 'C0U', $ord )
44 : ($ord > 0x1f and $ord < 0x7f) ? sprintf "%c", $ord
49 $expected =~ s/'/::/g;
51 # this is apparently how things worked before 5.16
52 utf8::encode($expected) if "$]" < 5.016 and $ord > 255;
54 my $stash_name = join '::', map { $_->STASH->NAME, $_->NAME } svref_2object($sub)->GV;
56 is $stash_name, $expected, "stash name for $type is correct $for_what";
57 is $sub->(), $expected, "caller() in $type returns correct name $for_what";
59 skip '%DB::sub not populated when enabled at runtime', 1
61 my ($prefix) = $expected =~ /^(.*?test::[^:]+::)/;
62 my ($db_found) = grep /^$prefix/, keys %DB::sub;
63 is $db_found, $expected, "%DB::sub entry for $type is correct $for_what";
67 #######################################################################
69 use Sub::Name 'subname';
71 my @ordinal = ( 1 .. 255 );
73 # 5.14 is the first perl to start properly handling \0 in identifiers
77 # Unicode in 5.6 is not sane (crashes etc)
79 0x100, # LATIN CAPITAL LETTER A WITH MACRON
80 0x498, # CYRILLIC CAPITAL LETTER ZE WITH DESCENDER
81 0x2122, # TRADE MARK SIGN
82 0x1f4a9, # PILE OF POO
85 my $legal_ident_char = "A-Z_a-z0-9'";
86 $legal_ident_char .= join '', map chr, 0x100, 0x498
90 for my $ord (@ordinal) {
93 my $pkg = sprintf 'test::%s::SOME_%c_STASH', $uniq, $ord;
94 my $subname = sprintf 'SOME_%s_%c_NAME', $uniq, $ord;
95 my $fullname = join '::', $pkg, $subname;
97 $sub = subname $fullname => sub { (caller(0))[3] };
98 caller3_ok $sub, $fullname, 'renamed closure', $ord;
100 # test that we can *always* compile at least within the correct package
103 skip 'single quote as a package separator has been '.
104 ("$]" > 5.041001 ? 'removed' : 'deprecated'), 3
105 if $ord == 39 and "$]" > 5.037009;
107 if ( chr($ord) =~ m/^[$legal_ident_char]$/o ) { # compile directly
108 $expected = "native::$fullname";
109 $sub = compile_named_sub $expected => '(caller(0))[3]';
111 else { # not a legal identifier but at least test the package name by aliasing
112 $expected = "aliased::native::$fullname";
115 *palatable:: = *{"aliased::native::${pkg}::"};
116 # now palatable:: literally means aliased::native::${pkg}::
117 my $encoded_sub = $subname;
118 utf8::encode($encoded_sub) if "$]" < 5.016 and $ord > 255;
119 ${"palatable::$encoded_sub"} = 1;
120 ${"palatable::"}{"sub"} = ${"palatable::"}{$encoded_sub};
121 # and palatable::sub means aliased::native::${pkg}::${subname}
123 $sub = compile_named_sub 'palatable::sub' => '(caller(0))[3]';
125 caller3_ok $sub, $expected, 'natively compiled sub', $ord;