X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2F44-localize_delete-magic.t;fp=t%2F44-localize_delete-magic.t;h=0b930505f4a20896fd08684df21b4267de3c7285;hb=cd7bfde845927d38ca43bd7430503f388260d53a;hp=0000000000000000000000000000000000000000;hpb=bb65dbcf7daba3ac5c7e0f933745762bf6ea56bf;p=perl%2Fmodules%2FScope-Upper.git diff --git a/t/44-localize_delete-magic.t b/t/44-localize_delete-magic.t new file mode 100644 index 0000000..0b93050 --- /dev/null +++ b/t/44-localize_delete-magic.t @@ -0,0 +1,63 @@ +#!perl + +use strict; +use warnings; + +use Scope::Upper qw/localize_delete/; + +use Test::More tests => 9; + +our $deleted; + +{ + package Scope::Upper::Test::TiedArray; + + sub TIEARRAY { bless [], $_[0] } + sub STORE { $_[0]->[$_[1]] = $_[2] } + sub FETCH { $_[0]->[$_[1]] } + sub CLEAR { @{$_[0]} = (); } + sub FETCHSIZE { scalar @{$_[0]} } + sub DELETE { ++$main::deleted; delete $_[0]->[$_[1]] } + sub EXTEND {} + + our $NEGATIVE_INDICES = 0; +} + +our @a; + +tie @a, 'Scope::Upper::Test::TiedArray'; +{ + local @a = (5 .. 7); + local $a[4] = 9; + is $deleted, undef, 'localize_delete @tied_array, $existent => 0 [not deleted]'; + { + localize_delete '@a', 4 => 0; + is $deleted, 1, 'localize_delete @tied_array, $existent => 0 [deleted]'; + is_deeply \@a, [ 5 .. 7 ], 'localize_delete @tied_array, $existent => 0 [ok]'; + } + is_deeply \@a, [ 5 .. 7, undef, 9 ], 'localize_elem @incomplete_tied_array, $nonexistent, 12 => 0 [end]'; + is $deleted, 1, 'localize_delete @tied_array, $existent => 0 [not more deleted]'; +} + +{ + local @a = (4 .. 6); + local $a[4] = 7; + { + localize_delete '@main::a', -1, 0; + is_deeply \@a, [ 4 .. 6 ], 'localize_delete @tied_array, $existent_neg => 0 [ok]'; + } + is_deeply \@a, [ 4 .. 6, undef, 7 ], 'localize_delete @tied_array, $existent_neg => 0 [end]'; +} + +SKIP: +{ + skip '$NEGATIVE_INDICES has no special meaning on 5.8.0 and older' => 2 if $] < 5.008_001; + local $Scope::Upper::Test::TiedArray::NEGATIVE_INDICES = 1; + local @a = (4 .. 6); + local $a[4] = 7; + { + localize_delete '@main::a', -1, 0; + is_deeply \@a, [ 4 .. 6 ], 'localize_delete @tied_array_wo_neg, $existent_neg => 0 [ok]'; + } + is_deeply \@a, [ 4, 5, 7 ], 'localize_delete @tied_array_wo_neg, $existent_neg => 0 [end]'; +}