]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/16-huf.t
Rename test functions init() and check() to init_watcher() and watch()
[perl/modules/Variable-Magic.git] / t / 16-huf.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 skip_all => 'No nice uvar magic for this perl';
12 }
13
14 eval "use Hash::Util::FieldHash";
15 if ($@) {
16  plan skip_all => 'Hash::Util::FieldHash required for testing uvar interaction';
17 } else {
18  plan tests => 2 * 5 + 7 + 1;
19  my $v = $Hash::Util::FieldHash::VERSION;
20  diag "Using Hash::Util::FieldHash $v" if defined $v;
21 }
22
23 use lib 't/lib';
24 use Variable::Magic::TestWatcher;
25
26 my $wiz = init_watcher [ qw/fetch store/ ], 'huf';
27 ok defined($wiz),       'huf: wizard with uvar is defined';
28 is ref($wiz), 'SCALAR', 'huf: wizard with uvar is a scalar ref';
29
30 Hash::Util::FieldHash::fieldhash(\my %h);
31
32 my $obj = { };
33 bless $obj, 'Variable::Magic::Test::Mock';
34 $h{$obj} = 5;
35
36 my ($res) = watch { cast %h, $wiz } { }, 'cast uvar magic on fieldhash';
37 ok $res, 'huf: cast uvar magic on fieldhash succeeded';
38
39 my ($s) = watch { $h{$obj} } { fetch => 1 }, 'fetch on magical fieldhash';
40 is $s, 5, 'huf: fetch on magical fieldhash succeeded';
41
42 watch { $h{$obj} = 7 } { store => 1 }, 'store on magical fieldhash';
43 is $h{$obj}, 7, 'huf: store on magical fieldhash succeeded';
44
45 ($res) = watch { dispell %h, $wiz } { }, 'dispell uvar magic on fieldhash';
46 ok $res, 'huf: dispell uvar magic on fieldhash succeeded';
47
48 $h{$obj} = 11;
49 $s = $h{$obj};
50 is $s, 11, 'huf: store/fetch on fieldhash after dispell still ok';
51
52 $Variable::Magic::TestWatcher::mg_end = { fetch => 1 };