]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/44-localize_delete-magic.t
bf0c43d6eb6cc3ffe40b9c103fd6dee8c600f8f3
[perl/modules/Scope-Upper.git] / t / 44-localize_delete-magic.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Scope::Upper qw/localize_delete UP HERE/;
7
8 use Test::More tests => 9;
9
10 our $deleted;
11
12 {
13  package Scope::Upper::Test::TiedArray;
14
15  sub TIEARRAY { bless [], $_[0] }
16  sub STORE { $_[0]->[$_[1]] = $_[2] }
17  sub FETCH { $_[0]->[$_[1]] }
18  sub CLEAR { @{$_[0]} = (); }
19  sub FETCHSIZE { scalar @{$_[0]} }
20  sub DELETE { ++$main::deleted; delete $_[0]->[$_[1]] }
21  sub EXTEND {}
22
23  our $NEGATIVE_INDICES = 0;
24 }
25
26 our @a;
27
28 {
29  local @a;
30  tie @a, 'Scope::Upper::Test::TiedArray';
31  @a = (5 .. 7);
32  local $a[4] = 9;
33  is $deleted, undef, 'localize_delete @tied_array, $existent => HERE [not deleted]';
34  {
35   localize_delete '@a', 4 => HERE;
36   is $deleted, 1, 'localize_delete @tied_array, $existent => HERE [deleted]';
37   is_deeply \@a, [ 5 .. 7 ], 'localize_delete @tied_array, $existent => HERE [ok]';
38  }
39  is_deeply \@a, [ 5 .. 7, undef, 9 ], 'localize_elem @incomplete_tied_array, $nonexistent, 12 => HERE [end]';
40  is $deleted, 1, 'localize_delete @tied_array, $existent => HERE [not more deleted]';
41 }
42
43 {
44  local @a;
45  tie @a, 'Scope::Upper::Test::TiedArray';
46  @a = (4 .. 6);
47  local $a[4] = 7;
48  {
49   localize_delete '@main::a', -1 => HERE;
50   is_deeply \@a, [ 4 .. 6 ], 'localize_delete @tied_array, $existent_neg => HERE [ok]';
51  }
52  is_deeply \@a, [ 4 .. 6, undef, 7 ], 'localize_delete @tied_array, $existent_neg => HERE [end]';
53 }
54
55 SKIP:
56 {
57  skip '$NEGATIVE_INDICES has no special meaning on 5.8.0 and older' => 2 if $] < 5.008_001;
58  local $Scope::Upper::Test::TiedArray::NEGATIVE_INDICES = 1;
59  local @a;
60  tie @a, 'Scope::Upper::Test::TiedArray';
61  @a = (4 .. 6);
62  local $a[4] = 7;
63  {
64   localize_delete '@main::a', -1 => HERE;
65   is_deeply \@a, [ 4 .. 6 ], 'localize_delete @tied_array_wo_neg, $existent_neg => HERE [ok]';
66  }
67  is_deeply \@a, [ 4, 5, 7 ], 'localize_delete @tied_array_wo_neg, $existent_neg => HERE [end]';
68 }