If we don't build B, we should skip all its tests.
[p5sagit/p5-mst-13.2.git] / ext / B / t / b.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     if ($^O eq 'MacOS') {
6         @INC = qw(: ::lib ::macos:lib);
7     } else {
8         @INC = '.';
9         push @INC, '../lib';
10     }
11     require Config;
12     if (($Config::Config{'extensions'} !~ /\bB\b/) ){
13         print "1..0 # Skip -- Perl configured without B module\n";
14         exit 0;
15     }
16 }
17
18 $|  = 1;
19 use warnings;
20 use strict;
21 use Test::More tests => 5;
22
23 BEGIN { use_ok( 'B' ); }
24
25
26 package Testing::Symtable;
27 use vars qw($This @That %wibble $moo %moo);
28 my $not_a_sym = 'moo';
29
30 sub moo { 42 }
31 sub car { 23 }
32
33
34 package Testing::Symtable::Foo;
35 sub yarrow { "Hock" }
36
37 package Testing::Symtable::Bar;
38 sub hock { "yarrow" }
39
40 package main;
41 use vars qw(%Subs);
42 local %Subs = ();
43 B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
44                 'Testing::Symtable::');
45
46 sub B::GV::find_syms {
47     my($symbol) = @_;
48
49     $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
50 }
51
52 my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
53                                                BEGIN);
54 push @syms, "Testing::Symtable::Foo::yarrow";
55
56 # Make sure we hit all the expected symbols.
57 ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
58
59 # Make sure we only hit them each once.
60 ok( (!grep $_ != 1, values %Subs), '...and found once' );
61
62 # Tests for MAGIC / MOREMAGIC
63 ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
64 {
65     my $e = '';
66     local $SIG{__DIE__} = sub { $e = $_[0] };
67     # Used to dump core, bug #16828
68     eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
69     like( $e, qr/Can't call method "TYPE" on an undefined value/, 
70         '$. has no more magic' );
71 }