7 @INC = qw(: ::lib ::macos:lib);
16 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
17 print "1..0 # Skip -- Perl configured without B module\n";
25 use Test::More tests => 57;
27 BEGIN { use_ok( 'B' ); }
30 package Testing::Symtable;
31 use vars qw($This @That %wibble $moo %moo);
32 my $not_a_sym = 'moo';
38 package Testing::Symtable::Foo;
41 package Testing::Symtable::Bar;
47 B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
48 'Testing::Symtable::');
50 sub B::GV::find_syms {
53 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
56 my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
58 push @syms, "Testing::Symtable::Foo::yarrow";
60 # Make sure we hit all the expected symbols.
61 ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
63 # Make sure we only hit them each once.
64 ok( (!grep $_ != 1, values %Subs), '...and found once' );
66 # Tests for MAGIC / MOREMAGIC
67 ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
70 local $SIG{__DIE__} = sub { $e = $_[0] };
71 # Used to dump core, bug #16828
72 eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
73 like( $e, qr/Can't call method "TYPE" on an undefined value/,
74 '$. has no more magic' );
77 ok(B::svref_2object(qr/foo/)->MAGIC->precomp() eq 'foo', 'Get string from qr//');
78 like(B::svref_2object(qr/foo/)->MAGIC->REGEX(), qr/\d+/, "REGEX() returns numeric value");
80 my $iv_ref = B::svref_2object(\$iv);
81 is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
82 is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
83 # Flag tests are needed still
84 #diag $iv_ref->FLAGS();
85 my $iv_ret = $iv_ref->object_2svref();
86 is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
87 is($$iv_ret, $iv, "Test object_2svref()");
88 is($iv_ref->int_value, $iv, "Test int_value()");
89 is($iv_ref->IV, $iv, "Test IV()");
90 is($iv_ref->IVX(), $iv, "Test IVX()");
91 is($iv_ref->UVX(), $iv, "Test UVX()");
94 my $pv_ref = B::svref_2object(\$pv);
95 is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
96 is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
97 # Flag tests are needed still
98 #diag $pv_ref->FLAGS();
99 my $pv_ret = $pv_ref->object_2svref();
100 is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
101 is($$pv_ret, $pv, "Test object_2svref()");
102 is($pv_ref->PV(), $pv, "Test PV()");
103 eval { is($pv_ref->RV(), $pv, "Test RV()"); };
105 is($pv_ref->PVX(), $pv, "Test PVX()");
108 my $nv_ref = B::svref_2object(\$nv);
109 is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
110 is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
111 # Flag tests are needed still
112 #diag $nv_ref->FLAGS();
113 my $nv_ret = $nv_ref->object_2svref();
114 is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
115 is($$nv_ret, $nv, "Test object_2svref()");
116 is($nv_ref->NV, $nv, "Test NV()");
117 is($nv_ref->NVX(), $nv, "Test NVX()");
120 my $null_ref = B::svref_2object(\$null);
121 is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
122 is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
123 # Flag tests are needed still
124 #diag $null_ref->FLAGS();
125 my $null_ret = $nv_ref->object_2svref();
126 is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
127 is($$null_ret, $nv, "Test object_2svref()");
129 my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
131 my $cv_ref = B::svref_2object(\$cv);
132 is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
133 is(ref $cv_ref, "$RV_class",
134 "Test $RV_class return from svref_2object - code");
135 my $cv_ret = $cv_ref->object_2svref();
136 is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
137 is($$cv_ret, $cv, "Test object_2svref()");
140 my $av_ref = B::svref_2object(\$av);
141 is(ref $av_ref, "$RV_class",
142 "Test $RV_class return from svref_2object - array");
145 my $hv_ref = B::svref_2object(\$hv);
146 is(ref $hv_ref, "$RV_class",
147 "Test $RV_class return from svref_2object - hash");
150 my $gv_ref = B::svref_2object(\*gv);
151 is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
152 ok(! $gv_ref->is_empty(), "Test is_empty()");
153 is($gv_ref->NAME(), "gv", "Test NAME()");
154 is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
155 like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
157 # The following return B::SPECIALs.
158 is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
159 is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
160 is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
162 # More utility functions
163 is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
164 is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
165 is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
166 like(B::hash("wibble"), qr/0x[0-9a-f]*/, "Testing B::hash()");
167 is(B::cstring("wibble"), '"wibble"', "Testing B::cstring()");
168 is(B::perlstring("wibble"), '"wibble"', "Testing B::perlstring()");
169 is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
170 is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
171 is(B::opnumber("chop"), 38, "Testing opnumber with opname (chop)");
175 my $sg = B::sub_generation();
176 *UNIVERSAL::hand_waving = sub { };
177 ok( $sg < B::sub_generation, "sub_generation increments" );
181 my $ag = B::amagic_generation();
186 overload->import( '""' => sub {"What? You want more?!"} );
188 ok( $ag < B::amagic_generation, "amagic_generation increments" );