]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/28-uvar.t
Amend last change to make t/28-uvar.t pass again
[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 + 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
19 my $wiz = init [ qw/fetch store exists delete/ ], 'uvar';
20
21 my %h = (a => 1, b => 2, c => 3);
22
23 my $res = check { cast %h, $wiz } { }, 'cast';
24 ok $res, 'uvar: cast succeeded';
25
26 my $x = check { $h{a} } { fetch => 1 }, 'fetch directly';
27 is $x, 1, 'uvar: fetch directly correctly';
28
29 $x = check { "$h{b}" } { fetch => 1 }, 'fetch by interpolation';
30 is $x, 2, 'uvar: fetch by interpolation correctly';
31
32 check { $h{c} = 4 } { store => 1 }, 'store directly';
33
34 $x = check { $h{c} = 5 } { store => 1 }, 'fetch and store';
35 is $x, 5, 'uvar: fetch and store correctly';
36
37 $x = check { exists $h{c} } { exists => 1 }, 'exists';
38 ok $x, 'uvar: exists correctly';
39
40 $x = check { delete $h{c} } { delete => 1 }, 'delete existing key';
41 is $x, 5, 'uvar: delete existing key correctly';
42
43 $x = check { delete $h{z} } { delete => 1 }, 'delete non-existing key';
44 ok !defined $x, 'uvar: delete non-existing key correctly';
45
46 my $wiz2 = wizard get => sub { 0 };
47 cast %h, $wiz2;
48
49 $x = check { $h{a} } { fetch => 1 }, 'fetch directly with also non uvar magic';
50 is $x, 1, 'uvar: fetch directly with also non uvar magic correctly';
51
52 SKIP: {
53  eval "use Tie::Hash";
54  skip 'Tie::Hash required to test uvar magic on tied hashes' => 2 * 5 + 4 if $@;
55  diag "Using Tie::Hash $Tie::Hash::VERSION" if defined $Tie::Hash::VERSION;
56
57  tie my %h, 'Tie::StdHash';
58  %h = (x => 7, y => 8);
59
60  $res = check { cast %h, $wiz } { }, 'cast on tied hash';
61  ok $res, 'uvar: cast on tied hash succeeded';
62
63  $x = check { $h{x} } { fetch => 1 }, 'fetch on tied hash';
64  is $x, 7, 'uvar: fetch on tied hash succeeded';
65
66  check { $h{x} = 9 } { store => 1 }, 'store on tied hash';
67
68  $x = check { exists $h{x} } { exists => 1 }, 'exists on tied hash';
69  ok $x, 'uvar: exists on tied hash succeeded';
70
71  $x = check { delete $h{x} } { delete => 1 }, 'delete on tied hash';
72  is $x, 9, 'uvar: delete on tied hash succeeded';
73 }
74
75 $wiz2 = wizard fetch => sub { 0 };
76 my %h2 = (a => 37, b => 2, c => 3);
77 cast %h2, $wiz2;
78
79 $x = eval {
80  local $SIG{__WARN__} = sub { die };
81  $h2{a};
82 };
83 is $@, '', 'uvar: fetch with incomplete magic doesn\'t croak';
84 is $x, 37, 'uvar: fetch with incomplete magic correctly';
85
86 eval {
87  local $SIG{__WARN__} = sub { die };
88  $h2{a} = 73;
89 };
90 is $@, '',     'uvar: store with incomplete magic doesn\'t croak';
91 is $h2{a}, 73, 'uvar: store with incomplete magic correctly';
92
93 my $wiz3 = wizard store => sub { ++$_[2]; 0 }, copy_key => 1;
94 my %h3 = (a => 3);
95 cast %h3, $wiz3;
96
97 for my $i (1 .. 2) {
98  my $key = 'a';
99  eval { $h3{$key} = 3 + $i };
100  is        $@,   '',  "uvar: change key in store doesn't croak ($i)";
101  is        $key, 'a', "uvar: change key didn't clobber \$key ($i)";
102  is_deeply \%h3, { a => 3, b => 3 + $i },
103                       "uvar: change key in store correcty ($i)";
104 }
105
106 for my $i (1 .. 2) {
107  eval { $h3{b} = 5 + $i };
108  is        $@,   '',    "uvar: change readonly key in store doesn't croak ($i)";
109  is_deeply \%h3, { a => 3, b => 5, c => 5 + $i },
110                         "uvar: change readonly key in store correcty ($i)";
111 }