6 use Test::More tests => 33 + 41;
8 use Variable::Magic qw/wizard cast dispell VMG_UVAR/;
15 my ($cb, $tests) = @_;
16 for (my $i = 0; $i < $n; ++$i) {
17 my $res = eval { $cb->($i) };
18 $tests->($i, $res, $@);
22 eval { $w[0] = wizard get => sub { ++$c[0] }, set => sub { --$c[0] } };
23 is($@, '', 'wizard 0 creation doesn\'t croak');
24 eval { $w[1] = wizard get => sub { ++$c[1] }, set => sub { --$c[1] } };
25 is($@, '', 'wizard 1 creation doesn\'t croak');
26 eval { $w[2] = wizard get => sub { ++$c[2] }, set => sub { --$c[2] } };
27 is($@, '', 'wizard 2 creation doesn\'t croak');
33 my ($i, $res, $err) = @_;
34 ok(defined $res, "wizard $i is defined");
35 is(ref $w[$i], 'SCALAR', "wizard $i is a scalar ref");
44 my ($i, $res, $err) = @_;
45 is($err, '', "cast magic $i doesn't croak");
46 ok($res, "cast magic $i is valid");
50 for (0 .. $n - 1) { is($c[$_], 1, "get magic $_"); }
53 for (0 .. $n - 1) { is($c[$_], 0, "set magic $_"); }
55 my $res = eval { dispell $a, $w[1] };
56 is($@, '', 'dispell magic 1 doesn\'t croak');
57 ok($res, 'dispell magic 1 is valid');
60 for (0, 2) { is($c[$_], 1, "get magic $_ after dispelled 1"); }
63 for (0, 2) { is($c[$_], 0, "set magic $_ after dispelled 1"); }
65 $res = eval { dispell $a, $w[0] };
66 is($@, '', 'dispell magic 0 doesn\'t croak');
67 ok($res, 'dispell magic 0 is valid');
70 is($c[2], 1, 'get magic 2 after dispelled 1 & 0');
73 is($c[2], 0, 'set magic 2 after dispelled 1 & 0');
75 $res = eval { dispell $a, $w[2] };
76 is($@, '', 'dispell magic 2 doesn\'t croak');
77 ok($res, 'dispell magic 2 is valid');
80 skip 'No nice uvar magic for this perl' => 41 unless VMG_UVAR;
85 eval { $w[0] = wizard fetch => sub { ++$c[0] }, store => sub { --$c[0] } };
86 is($@, '', 'wizard with uvar 0 doesn\'t croak');
87 eval { $w[1] = wizard fetch => sub { ++$c[1] }, store => sub { --$c[1] } };
88 is($@, '', 'wizard with uvar 1 doesn\'t croak');
89 eval { $w[2] = wizard fetch => sub { ++$c[2] }, store => sub { --$c[2] } };
90 is($@, '', 'wizard with uvar 2 doesn\'t croak');
96 my ($i, $res, $err) = @_;
97 ok(defined $res, "wizard with uvar $i is defined");
98 is(ref $w[$i], 'SCALAR', "wizard with uvar $i is a scalar ref");
101 my %h = (a => 1, b => 2);
107 my ($i, $res, $err) = @_;
108 is($err, '', "cast uvar magic $i doesn't croak");
109 ok($res, "cast uvar magic $i is valid");
113 is($s, 1, 'fetch magic doesn\'t clobber');
114 for (0 .. $n - 1) { is($c[$_], 1, "fetch magic $_"); }
117 for (0 .. $n - 1) { is($c[$_], 0, "store magic $_"); }
118 is($h{a}, 3, 'store magic doesn\'t clobber');
119 # $c[$_] == 1 for 0 .. 2
121 my $res = eval { dispell %h, $w[1] };
122 is($@, '', 'dispell uvar magic 1 doesn\'t croak');
123 ok($res, 'dispell uvar magic 1 is valid');
126 is($s, 2, 'fetch magic after dispelled 1 doesn\'t clobber');
127 for (0, 2) { is($c[$_], 2, "fetch magic $_ after dispelled 1"); }
130 for (0, 2) { is($c[$_], 1, "store magic $_ after dispelled 1"); }
131 is($h{b}, 4, 'store magic after dispelled 1 doesn\'t clobber');
132 # $c[$_] == 2 for 0, 2
134 $res = eval { dispell %h, $w[2] };
135 is($@, '', 'dispell uvar magic 2 doesn\'t croak');
136 ok($res, 'dispell uvar magic 2 is valid');
139 is($s, 4, 'fetch magic after dispelled 1,2 doesn\'t clobber');
140 for (0) { is($c[$_], 3, "fetch magic $_ after dispelled 1,2"); }
143 for (0) { is($c[$_], 2, "store magic $_ after dispelled 1,2"); }
144 is($h{b}, 6, 'store magic after dispelled 1,2 doesn\'t clobber');
147 $res = eval { dispell %h, $w[0] };
148 is($@, '', 'dispell uvar magic 0 doesn\'t croak');
149 ok($res, 'dispell uvar magic 0 is valid');
152 is($s, 6, 'fetch magic after dispelled 1,2,0 doesn\'t clobber');
154 is($h{b}, 8, 'store magic after dispelled 1,2,0 doesn\'t clobber');