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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
# -*- perl -*-
use warnings;
use strict;
use Test::More 'no_plan';
use Config;
use File::Spec;
use lib qw( ../lib lib );
use B::Keywords ':all';
BEGIN {
if (defined &Test::More::note) {
*diag = *Test::More::note;
}
}
# Translate control characters into ^A format
# Leave others alone.
my @control_map = (undef, "A".."Z");
sub _map_control_char {
my $char = shift;
my $ord = ord $char;
return "^".$control_map[$ord] if $ord <= 26;
return $char;
}
sub kopen {
my @files = ();
# this is the default location for 'kewords.h', unless we're on macOS
push @files, File::Spec->catfile( $Config{archlibexp}, 'CORE', 'keywords.h' );
# Apple has restructured it's file system, if we're on macOS and the above
# doesn't exist, we're probably using system perl. try an alternate location
# via Apple's tools
if ($^O eq "darwin" && $Config{archlibexp} =~ m!^/System/Library/Perl!) {
my $p = '';
$p = qx/xcrun --show-sdk-path/;
chomp $p;
push @files, File::Spec->catdir($p, $files[0]);
}
# return the first FH we can open from the list above. 99.44% of
# the time it's going to be the first (and correct) one
for my $file (@files) {
if (open my $fh, "<", $file) {
return $fh;
}
}
# if we're here something else happend or apple is up to new tricks
die "Can't open any of @files";
}
# Test everything in keywords.h is covered.
{
# get a filehandle to 'keywords.h'
my $fh = kopen();
local $/;
chomp( my @keywords = <$fh> =~ /^\#define \s+ KEY_(\S+) /xmsg );
close $fh;
my $usedevel = $Config{usedevel};
my %covered = map { $_ => 1 } @Symbols, @Barewords;
diag "check if all keywords.h have \@Symbols or \@Barewords";
TODO: {
for my $keyword (@keywords) {
local $TODO = "old blead version, wait for the release" if $Config{usedevel} && !$covered{$keyword};
ok $covered{$keyword}, "keyword: $keyword";
}
}
diag "reverse: check if all \@Symbols and \@Barewords are in keywords.h";
my %keyword = map { $_ => 1 } @keywords;
TODO: {
for my $key (@Barewords, @Functions) {
if ($key =~ /^-/ or $key eq 'err') { # skip file test ops, err fails my tests
diag "not in keywords.h: $key. skipped";
} else {
local $TODO = "old blead version, wait for the release" if $Config{usedevel} && !$keyword{$key};
ok $keyword{$key}, "keywords.h: $key";
}
}
}
}
# Test all the single character globals in main
{
my @globals = map { _map_control_char($_) }
grep { length $_ == 1 and /\W/ }
keys %main::;
my %symbols = map { s/^.//; $_ => 1 } (@Scalars, @Arrays, @Hashes);
diag "check if all single-character \@Scalars, \@Arrays, \@Hashes are as globals in \%main::";
for my $global (@globals) {
ok $symbols{$global}, "global: $global";
}
#my %globals = map { $_ => 1 } @globals;
diag "the reverse is not true as most globals are auto-created";
#for my $sym (grep { length $_ == 1 } keys %symbols) {
# ok $globals{$sym}, "\%main:: $sym";
#}
}
# Cannot test all the other globals in main. They are mostly created on-the-fly.
if (0) {
my %globals = map { _map_control_char($_) => 1 }
grep { length $_ > 1 && $_ !~ /^_</ && $_ !~ /::$/ }
keys %main::;
require English;
English->import;
diag "check if all multi-character \@Scalars, \@Arrays, \@Hashes are as globals in \%main::";
my %symbols = map { s/^[\$\%\@]//; $_ => 1 } (@Scalars, @Arrays, @Hashes);
for my $sym (grep { length $_ > 1 } keys %symbols) {
ok $globals{$sym}, "\%main:: $sym";
}
# and the reverse is troubled by namespace pollution. %main:: contains B, Test, ...
}
|