6 use Test::More tests => (2 * 14 + 2) + 2 * (2 * 8 + 4) + 1;
8 use Variable::Magic qw/cast dispell/;
11 use Variable::Magic::TestWatcher;
14 [ qw/get set len clear free copy dup local fetch store exists delete/ ],
17 my $n = int rand 1000;
20 check { cast $a, $wiz } { }, 'cast';
23 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
24 check { $b = $a } { get => 1 }, 'assign to';
25 is $b, $n, 'scalar: assign to correctly';
27 $b = check { "X${a}Y" } { get => 1 }, 'interpolate';
28 is $b, "X${n}Y", 'scalar: interpolate correctly';
30 $b = check { \$a } { }, 'reference';
32 check { $a = 123; () } { set => 1 }, 'assign to';
34 check { ++$a; () } { get => 1, set => 1 }, 'increment';
36 check { --$a; () } { get => 1, set => 1 }, 'decrement';
38 check { $a *= 1.5; () } { get => 1, set => 1 }, 'multiply in place';
40 check { $a /= 1.5; () } { get => 1, set => 1 }, 'divide in place';
44 check { cast $b, $wiz } { }, 'cast 2';
45 } { free => 1 }, 'scope end';
47 check { undef $a } { set => 1 }, 'undef';
49 check { dispell $a, $wiz } { }, 'dispell';
55 check { cast $a[1], $wiz } { }, 'array element: cast';
57 check { $a[1] = 6; () } { set => 1 }, 'array element: set';
59 $b = check { $a[1] } { get => 1 }, 'array element: get';
60 is $b, 6, 'scalar: array element: get correctly';
62 check { $a[0] = 5 } { }, 'array element: set other';
64 $b = check { $a[2] } { }, 'array element: get other';
65 is $b, 9, 'scalar: array element: get other correctly';
67 $b = check { exists $a[1] } { }, 'array element: exists';
68 is $b, 1, 'scalar: array element: exists correctly';
70 # $b has to be set inside the block for the test to pass on 5.8.3 and lower
71 check { $b = delete $a[1] } { get => 1, free => ($] > 5.008005 ? 1 : 0) }, 'array element: delete';
72 is $b, 6, 'scalar: array element: delete correctly';
74 check { $a[1] = 4 } { }, 'array element: set after delete';
78 my %h = (a => 7, b => 8);
80 check { cast $h{b}, $wiz } { }, 'hash element: cast';
82 check { $h{b} = 6; () } { set => 1 }, 'hash element: set';
84 $b = check { $h{b} } { get => 1 }, 'hash element: get';
85 is $b, 6, 'scalar: hash element: get correctly';
87 check { $h{a} = 5 } { }, 'hash element: set other';
89 $b = check { $h{a} } { }, 'hash element: get other';
90 is $b, 5, 'scalar: hash element: get other correctly';
92 $b = check { exists $h{b} } { }, 'hash element: exists';
93 is $b, 1, 'scalar: hash element: exists correctly';
95 $b = check { delete $h{b} } { get => 1, free => 1 }, 'hash element: delete';
96 is $b, 6, 'scalar: hash element: delete correctly';
98 check { $h{b} = 4 } { }, 'hash element: set after delete';