X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fop%2Fref.t;h=a2baab8e3b911f339fced7b54d722cdab3677655;hb=5cec1e3b3b86ef84555da325ea92c9cc0b18b7b2;hp=cace1e14bdcafa1a68f495ce989cc2245129711c;hpb=79072805bf63abe5b5978b5928ab00d360ea3e7f;p=p5sagit%2Fp5-mst-13.2.git diff --git a/t/op/ref.t b/t/op/ref.t index cace1e1..a2baab8 100755 --- a/t/op/ref.t +++ b/t/op/ref.t @@ -1,6 +1,6 @@ #!./perl -print "1..37\n"; +print "1..56\n"; # Test glob operations. @@ -73,7 +73,7 @@ print ${$$ref[2]}[2] == 5 ? "ok 16\n" : "not ok 16\n"; print scalar @{$$ref[0]} == 0 ? "ok 17\n" : "not ok 17\n"; print $ref->[1] == 2 ? "ok 18\n" : "not ok 18\n"; -print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 18\n"; +print $ref->[2]->[0] == 3 ? "ok 19\n" : "not ok 19\n"; # Test references to hashes of references. @@ -81,14 +81,14 @@ $refref = \%whatever; $refref->{"key"} = $ref; print $refref->{"key"}->[2]->[0] == 3 ? "ok 20\n" : "not ok 20\n"; -# Test to see if anonymous subarrays sprint into existence. +# Test to see if anonymous subarrays spring into existence. $spring[5]->[0] = 123; $spring[5]->[1] = 456; push(@{$spring[5]}, 789); print join(':',@{$spring[5]}) eq "123:456:789" ? "ok 21\n" : "not ok 21\n"; -# Test to see if anonymous subhashes sprint into existence. +# Test to see if anonymous subhashes spring into existence. @{$spring2{"foo"}} = (1,2,3); $spring2{"foo"}->[3] = 4; @@ -101,7 +101,7 @@ $subref = \&mysub; &$subref; $subrefref = \\&mysub2; -&$$subrefref("ok 24\n"); +$$subrefref->("ok 24\n"); sub mysub2 { print shift } # Test the ref operator. @@ -134,7 +134,8 @@ print ref $object2 eq MYHASH ? "ok 32\n" : "not ok 32\n"; sub mymethod { local($THIS, @ARGS) = @_; - die "Not a MYHASH" unless ref $THIS eq MYHASH; + die 'Got a "' . ref($THIS). '" instead of a MYHASH' + unless ref $THIS eq MYHASH; print $THIS->{FOO} eq BAR ? "ok $ARGS[0]\n" : "not ok $ARGS[0]\n"; } @@ -144,13 +145,14 @@ $string = "not ok 34\n"; $object = "foo"; $string = "ok 34\n"; $main'anonhash2 = "foo"; -$string = "not ok 34\n"; +$string = ""; -sub DESTROY { +DESTROY { + return unless $string; print $string; - # Test that the object has already been "cursed". - print ref shift eq HASH ? "ok 35\n" : "not ok 35\n"; + # Test that the object has not already been "cursed". + print ref shift ne HASH ? "ok 35\n" : "not ok 35\n"; } # Now test inheritance of methods. @@ -175,5 +177,119 @@ print $foo eq foo ? "ok 37\n" : "not ok 37\n"; sub BASEOBJ'doit { local $ref = shift; die "Not an OBJ" unless ref $ref eq OBJ; - $ref->{shift}; + $ref->{shift()}; +} + +package UNIVERSAL; +@ISA = 'LASTCHANCE'; + +package LASTCHANCE; +sub foo { print $_[1] } + +package WHATEVER; +foo WHATEVER "ok 38\n"; + +# +# test the \(@foo) construct +# +package main; +@foo = (1,2,3); +@bar = \(@foo); +@baz = \(1,@foo,@bar); +print @bar == 3 ? "ok 39\n" : "not ok 39\n"; +print grep(ref($_), @bar) == 3 ? "ok 40\n" : "not ok 40\n"; +print @baz == 3 ? "ok 41\n" : "not ok 41\n"; + +my(@fuu) = (1,2,3); +my(@baa) = \(@fuu); +my(@bzz) = \(1,@fuu,@baa); +print @baa == 3 ? "ok 42\n" : "not ok 42\n"; +print grep(ref($_), @baa) == 3 ? "ok 43\n" : "not ok 43\n"; +print @bzz == 3 ? "ok 44\n" : "not ok 44\n"; + +# test for proper destruction of lexical objects + +sub larry::DESTROY { print "# larry\nok 45\n"; } +sub curly::DESTROY { print "# curly\nok 46\n"; } +sub moe::DESTROY { print "# moe\nok 47\n"; } + +{ + my ($joe, @curly, %larry); + my $moe = bless \$joe, 'moe'; + my $curly = bless \@curly, 'curly'; + my $larry = bless \%larry, 'larry'; + print "# leaving block\n"; +} + +print "# left block\n"; + +# another glob test + +$foo = "not ok 48"; +{ local(*bar) = "foo" } +$bar = "ok 48"; +local(*bar) = *bar; +print "$bar\n"; + +$var = "ok 49"; +$_ = \$var; +print $$_,"\n"; + +# test if reblessing during destruction results in more destruction + +{ + package A; + sub new { bless {}, shift } + DESTROY { print "# destroying 'A'\nok 51\n" } + package _B; + sub new { bless {}, shift } + DESTROY { print "# destroying '_B'\nok 50\n"; bless shift, 'A' } + package main; + my $b = _B->new; +} + +# test if $_[0] is properly protected in DESTROY() + +{ + my $i = 0; + local $SIG{'__DIE__'} = sub { + my $m = shift; + if ($i++ > 4) { + print "# infinite recursion, bailing\nnot ok 52\n"; + exit 1; + } + print "# $m"; + if ($m =~ /^Modification of a read-only/) { print "ok 52\n" } + }; + package C; + sub new { bless {}, shift } + DESTROY { $_[0] = 'foo' } + { + print "# should generate an error...\n"; + my $c = C->new; + } + print "# good, didn't recurse\n"; +} + +# test if refgen behaves with autoviv magic + +{ + my @a; + $a[1] = "ok 53\n"; + print ${\$_} for @a; +} + +# test global destruction + +package FINALE; + +{ + $ref3 = bless ["ok 56\n"]; # package destruction + my $ref2 = bless ["ok 55\n"]; # lexical destruction + local $ref1 = bless ["ok 54\n"]; # dynamic destruction + 1; # flush any temp values on stack +} + +DESTROY { + print $_[0][0]; }