]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/28-uvar.t
Switch to qw<>
[perl/modules/Variable-Magic.git] / t / 28-uvar.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Variable::Magic qw<wizard cast dispell VMG_UVAR>;
9
10 if (VMG_UVAR) {
11  plan tests => 2 * 15 + 12 + 14 + (4 * 2 * 2 + 1 + 1) + 1;
12 } else {
13  plan skip_all => 'No nice uvar magic for this perl';
14 }
15
16 use lib 't/lib';
17 use Variable::Magic::TestWatcher;
18 use Variable::Magic::TestValue;
19
20 my $wiz = init_watcher [ qw<fetch store exists delete> ], 'uvar';
21
22 my %h = (a => 1, b => 2, c => 3);
23
24 my $res = watch { cast %h, $wiz } { }, 'cast';
25 ok $res, 'uvar: cast succeeded';
26
27 my $x = watch { $h{a} } { fetch => 1 }, 'fetch directly';
28 is $x, 1, 'uvar: fetch directly correctly';
29
30 $x = watch { "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
31 is $x, 2, 'uvar: fetch by interpolation correctly';
32
33 watch { $h{c} = 4 } { store => 1 }, 'store directly';
34
35 $x = watch { $h{c} = 5 } { store => 1 }, 'fetch and store';
36 is $x, 5, 'uvar: fetch and store correctly';
37
38 $x = watch { exists $h{c} } { exists => 1 }, 'exists';
39 ok $x, 'uvar: exists correctly';
40
41 $x = watch { delete $h{c} } { delete => 1 }, 'delete existing key';
42 is $x, 5, 'uvar: delete existing key correctly';
43
44 $x = watch { delete $h{z} } { delete => 1 }, 'delete non-existing key';
45 ok !defined $x, 'uvar: delete non-existing key correctly';
46
47 my $wiz2 = wizard get => sub { 0 };
48 cast %h, $wiz2;
49
50 $x = watch { $h{a} } { fetch => 1 }, 'fetch directly with also non uvar magic';
51 is $x, 1, 'uvar: fetch directly with also non uvar magic correctly';
52
53 SKIP: {
54  eval "use Tie::Hash";
55  skip 'Tie::Hash required to test uvar magic on tied hashes' => 2 * 5 + 4 if $@;
56  defined and diag "Using Tie::Hash $_" for $Tie::Hash::VERSION;
57
58  tie my %h, 'Tie::StdHash';
59  %h = (x => 7, y => 8);
60
61  $res = watch { cast %h, $wiz } { }, 'cast on tied hash';
62  ok $res, 'uvar: cast on tied hash succeeded';
63
64  $x = watch { $h{x} } { fetch => 1 }, 'fetch on tied hash';
65  is $x, 7, 'uvar: fetch on tied hash succeeded';
66
67  watch { $h{x} = 9 } { store => 1 }, 'store on tied hash';
68
69  $x = watch { exists $h{x} } { exists => 1 }, 'exists on tied hash';
70  ok $x, 'uvar: exists on tied hash succeeded';
71
72  $x = watch { delete $h{x} } { delete => 1 }, 'delete on tied hash';
73  is $x, 9, 'uvar: delete on tied hash succeeded';
74 }
75
76 $wiz2 = wizard fetch => sub { 0 };
77 my %h2 = (a => 37, b => 2, c => 3);
78 cast %h2, $wiz2;
79
80 $x = eval {
81  local $SIG{__WARN__} = sub { die };
82  $h2{a};
83 };
84 is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
85 is $x, 37, 'uvar: fetch with incomplete magic correctly';
86
87 eval {
88  local $SIG{__WARN__} = sub { die };
89  $h2{a} = 73;
90 };
91 is $@, '',     'uvar: store with incomplete magic doesn\'t croak';
92 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
93
94 my $wiz3 = wizard store => sub { ++$_[2]; 0 }, copy_key => 1;
95 my %h3 = (a => 3);
96 cast %h3, $wiz3;
97
98 for my $i (1 .. 2) {
99  my $key = 'a';
100  eval { $h3{$key} = 3 + $i };
101  is        $@,   '',  "uvar: change key in store doesn't croak ($i)";
102  is        $key, 'a', "uvar: change key didn't clobber \$key ($i)";
103  is_deeply \%h3, { a => 3, b => 3 + $i },
104                       "uvar: change key in store correcty ($i)";
105 }
106
107 for my $i (1 .. 2) {
108  eval { $h3{b} = 5 + $i };
109  is        $@,   '',    "uvar: change readonly key in store doesn't croak ($i)";
110  is_deeply \%h3, { a => 3, b => 5, c => 5 + $i },
111                         "uvar: change readonly key in store correcty ($i)";
112 }
113
114 {
115  my %val = (apple => 1);
116
117  init_value %val, 'fetch', 'uvar';
118
119  value { my $x = $val{apple} } { apple => 1 }, 'value store';
120 }
121
122 {
123  my %val = (apple => 1);
124
125  my $wv = init_value %val, 'store', 'uvar';
126
127  value { $val{apple} = 2 } { apple => 1 }, 'value store';
128
129  dispell %val, $wv;
130  is_deeply \%val, { apple => 2 }, 'uvar: value after store';
131 }
132
133 {
134  my %val = (apple => 1);
135
136  init_value %val, 'exists', 'uvar';
137
138  value { my $x = exists $val{apple} } { apple => 1 }, 'value exists';
139 }
140
141 {
142  my %val = (apple => 1, banana => 2);
143
144  my $wv = init_value %val, 'delete', 'uvar';
145
146  value { delete $val{apple} } { apple => 1, banana => 2 }, 'value delete';
147
148  dispell %val, $wv;
149  is_deeply \%val, { banana => 2 }, 'uvar: value after delete';
150 }