set filehandle encodings in a shorter way
[p5sagit/Sub-Name.git] / t / exotic_names.t
CommitLineData
ea5fecfc 1use strict;
2use warnings;
3
4use Test::More;
5use B 'svref_2object';
67415d50 6BEGIN { $^P |= 0x210 }
ea5fecfc 7
8# This is a mess. The stash can supposedly handle Unicode but the behavior
9# is literally undefined before 5.16 (with crashes beyond the basic plane),
10# and remains unclear past 5.16 with evalbytes and feature unicode_eval
11# In any case - Sub::Name needs to *somehow* work with this, so we will do
12# a heuristic with ambiguous eval and looking for octets in the stash
b7d49226 13use if "$]" >= 5.016, feature => 'unicode_eval';
ea5fecfc 14
67fcad43 15use if "$]" >= 5.008, open => ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
f81503dc 16
ea5fecfc 17sub compile_named_sub {
18 my ( $fullname, $body ) = @_;
19 my $sub = eval "sub $fullname { $body }" . '\\&{$fullname}';
20 return $sub if $sub;
21 my $e = $@;
22 require Carp;
23 Carp::croak $e;
24}
25
26sub caller3_ok {
27 my ( $sub, $expected, $type, $ord ) = @_;
28
29 local $Test::Builder::Level = $Test::Builder::Level + 1;
30
31 my $for_what = sprintf "when it contains \\x%s ( %s )", (
32 ( ($ord > 255)
33 ? sprintf "{%X}", $ord
34 : sprintf "%02X", $ord
35 ),
36 (
37 $ord > 255 ? unpack('H*', pack 'C0U', $ord )
38 : ($ord > 0x1f and $ord < 0x7f) ? sprintf "%c", $ord
39 : sprintf '\%o', $ord
40 ),
41 );
42
43 $expected =~ s/'/::/g;
44
45 # this is apparently how things worked before 5.16
b7d49226 46 utf8::encode($expected) if "$]" < 5.016 and $ord > 255;
ea5fecfc 47
48 my $stash_name = join '::', map { $_->STASH->NAME, $_->NAME } svref_2object($sub)->GV;
49
50 is $stash_name, $expected, "stash name for $type is correct $for_what";
51 is $sub->(), $expected, "caller() in $type returns correct name $for_what";
3f1762c7 52 SKIP: {
53 skip '%DB::sub not populated when enabled at runtime', 1
54 unless keys %DB::sub;
55 my ($prefix) = $expected =~ /^(.*?test::[^:]+::)/;
56 my ($db_found) = grep /^$prefix/, keys %DB::sub;
57 is $db_found, $expected, "%DB::sub entry for $type is correct $for_what";
58 }
ea5fecfc 59}
60
61#######################################################################
62
4d96bce5 63use Sub::Name 'subname';
64
ea5fecfc 65my @ordinal = ( 1 .. 255 );
66
67# 5.14 is the first perl to start properly handling \0 in identifiers
68unshift @ordinal, 0
b7d49226 69 unless "$]" < 5.014;
ea5fecfc 70
71# Unicode in 5.6 is not sane (crashes etc)
72push @ordinal,
73 0x100, # LATIN CAPITAL LETTER A WITH MACRON
74 0x498, # CYRILLIC CAPITAL LETTER ZE WITH DESCENDER
75 0x2122, # TRADE MARK SIGN
76 0x1f4a9, # PILE OF POO
b7d49226 77 unless "$]" < 5.008;
ea5fecfc 78
67415d50 79plan tests => @ordinal * 2 * 3;
ea5fecfc 80
81my $legal_ident_char = "A-Z_a-z0-9'";
82$legal_ident_char .= join '', map chr, 0x100, 0x498
b7d49226 83 unless "$]" < 5.008;
ea5fecfc 84
67415d50 85my $uniq = 'A000';
ea5fecfc 86for my $ord (@ordinal) {
87 my $sub;
67415d50 88 $uniq++;
89 my $pkg = sprintf 'test::%s::SOME_%c_STASH', $uniq, $ord;
90 my $subname = sprintf 'SOME_%s_%c_NAME', $uniq, $ord;
ea5fecfc 91 my $fullname = join '::', $pkg, $subname;
92
4d96bce5 93 $sub = subname $fullname => sub { (caller(0))[3] };
94 caller3_ok $sub, $fullname, 'renamed closure', $ord;
95
ea5fecfc 96 # test that we can *always* compile at least within the correct package
97 my $expected;
98 if ( chr($ord) =~ m/^[$legal_ident_char]$/o ) { # compile directly
67415d50 99 $expected = "native::$fullname";
100 $sub = compile_named_sub $expected => '(caller(0))[3]';
ea5fecfc 101 }
102 else { # not a legal identifier but at least test the package name by aliasing
63be23c8 103 $expected = "aliased::native::$fullname";
104 {
105 no strict 'refs';
106 *palatable:: = *{"aliased::native::${pkg}::"};
107 # now palatable:: literally means aliased::native::${pkg}::
a24a6249 108 my $encoded_sub = $subname;
109 utf8::encode($encoded_sub) if "$]" < 5.016 and $ord > 255;
110 ${"palatable::$encoded_sub"} = 1;
111 ${"palatable::"}{"sub"} = ${"palatable::"}{$encoded_sub};
63be23c8 112 # and palatable::sub means aliased::native::${pkg}::${subname}
113 }
114 $sub = compile_named_sub 'palatable::sub' => '(caller(0))[3]';
ea5fecfc 115 }
116 caller3_ok $sub, $expected, 'natively compiled sub', $ord;
117}