Skip tests that require Data::Dumper if it is not built
[p5sagit/p5-mst-13.2.git] / ext / B / t / b.t
CommitLineData
ccc418af 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
1b026014 5 if ($^O eq 'MacOS') {
6 @INC = qw(: ::lib ::macos:lib);
7 } else {
8 @INC = '.';
9 push @INC, '../lib';
db5fd395 10 }
9cd8f857 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 }
ccc418af 16}
17
18$| = 1;
19use warnings;
20use strict;
01b509b0 21use Test::More tests => 41;
ccc418af 22
c5f0f3aa 23BEGIN { use_ok( 'B' ); }
ccc418af 24
08c6f5ec 25
87a42246 26package Testing::Symtable;
27use vars qw($This @That %wibble $moo %moo);
28my $not_a_sym = 'moo';
ccc418af 29
87a42246 30sub moo { 42 }
31sub car { 23 }
ccc418af 32
f70490b9 33
87a42246 34package Testing::Symtable::Foo;
35sub yarrow { "Hock" }
f70490b9 36
87a42246 37package Testing::Symtable::Bar;
38sub hock { "yarrow" }
9b86dfa2 39
87a42246 40package main;
41use vars qw(%Subs);
42local %Subs = ();
43B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
44 'Testing::Symtable::');
ccc418af 45
87a42246 46sub B::GV::find_syms {
47 my($symbol) = @_;
de3f1649 48
87a42246 49 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
cfe9256d 50}
ccc418af 51
87a42246 52my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
53 BEGIN);
54push @syms, "Testing::Symtable::Foo::yarrow";
ccc418af 55
87a42246 56# Make sure we hit all the expected symbols.
c5f0f3aa 57ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
1e1dbab6 58
87a42246 59# Make sure we only hit them each once.
c5f0f3aa 60ok( (!grep $_ != 1, values %Subs), '...and found once' );
61
62# Tests for MAGIC / MOREMAGIC
63ok( 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}
01b509b0 72
73my $iv = 1;
74my $iv_ref = B::svref_2object(\$iv);
75is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
76is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
77# Flag tests are needed still
78#diag $iv_ref->FLAGS();
79my $iv_ret = $iv_ref->object_2svref();
80is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
81is($$iv_ret, $iv, "Test object_2svref()");
82is($iv_ref->int_value, $iv, "Test int_value()");
83is($iv_ref->IV, $iv, "Test IV()");
84is($iv_ref->IVX(), $iv, "Test IVX()");
85is($iv_ref->UVX(), $iv, "Test UVX()");
86
87my $pv = "Foo";
88my $pv_ref = B::svref_2object(\$pv);
89is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
90is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
91# Flag tests are needed still
92#diag $pv_ref->FLAGS();
93my $pv_ret = $pv_ref->object_2svref();
94is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
95is($$pv_ret, $pv, "Test object_2svref()");
96is($pv_ref->PV(), $pv, "Test PV()");
97eval { is($pv_ref->RV(), $pv, "Test RV()"); };
98ok($@, "Test RV()");
99is($pv_ref->PVX(), $pv, "Test PVX()");
100
101my $nv = 1.1;
102my $nv_ref = B::svref_2object(\$nv);
103is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
104is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
105# Flag tests are needed still
106#diag $nv_ref->FLAGS();
107my $nv_ret = $nv_ref->object_2svref();
108is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
109is($$nv_ret, $nv, "Test object_2svref()");
110is($nv_ref->NV, $nv, "Test NV()");
111is($nv_ref->NVX(), $nv, "Test NVX()");
112
113my $null = undef;
114my $null_ref = B::svref_2object(\$null);
115is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
116is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
117# Flag tests are needed still
118#diag $null_ref->FLAGS();
119my $null_ret = $nv_ref->object_2svref();
120is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
121is($$null_ret, $nv, "Test object_2svref()");
122
123my $cv = sub{ 1; };
124my $cv_ref = B::svref_2object(\$cv);
125is($cv_ref->REFCNT, 1, "Test B::RV->REFCNT");
126is(ref $cv_ref, "B::RV", "Test B::RV return from svref_2object - code");
127my $cv_ret = $cv_ref->object_2svref();
128is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
129is($$cv_ret, $cv, "Test object_2svref()");
130
131my $av = [];
132my $av_ref = B::svref_2object(\$av);
133is(ref $av_ref, "B::RV", "Test B::RV return from svref_2object - array");
134
135my $hv = [];
136my $hv_ref = B::svref_2object(\$hv);
137is(ref $hv_ref, "B::RV", "Test B::RV return from svref_2object - hash");
138
139local *gv = *STDOUT;
140my $gv_ref = B::svref_2object(\*gv);
141is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
142ok(! $gv_ref->is_empty(), "Test is_empty()");
143is($gv_ref->NAME(), "gv", "Test NAME()");
144is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
145like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");