6 use Test::More tests => 19;
8 use Variable::Magic qw/wizard getdata cast dispell/;
13 wizard data => sub { return { foo => $_[1] || 12, bar => $_[3] || 27 } },
14 get => sub { $c += $_[1]->{foo}; $_[1]->{foo} = $c },
15 set => sub { $c += $_[1]->{bar}; $_[1]->{bar} = $c }
17 ok(!$@, "wizard creation error ($@)");
18 ok(defined $wiz, 'wizard is defined');
19 ok(ref $wiz eq 'SCALAR', 'wizard is a scalar ref');
22 my $res = eval { cast $a, $wiz };
23 ok(!$@, "cast croaks ($@)");
24 ok($res, 'cast invalid');
26 my $data = eval { getdata $a, $wiz };
27 ok(!$@, "getdata croaks ($@)");
28 ok($res, 'getdata invalid');
29 ok($data && ref($data) eq 'HASH'
30 && exists $data->{foo} && $data->{foo} == 12
31 && exists $data->{bar} && $data->{bar} == 27,
32 'private data creation ok');
35 ok($c == 13, 'get magic : pass data');
36 ok($data->{foo} == 13, 'get magic : data updated');
39 ok($c == 40, 'set magic : pass data');
40 ok($data->{bar} == 40, 'set magic : pass data');
42 $res = eval { dispell $a, $wiz };
43 ok(!$@, "dispell croaks ($@)");
44 ok($res, 'dispell invalid');
46 $res = eval { cast $a, $wiz, qw/z j t/ };
47 ok(!$@, "cast with arguments croaks ($@)");
48 ok($res, 'cast with arguments invalid');
50 $data = eval { getdata $a, $wiz };
51 ok(!$@, "getdata croaks ($@)");
52 ok($res, 'getdata invalid');
53 ok($data && ref($data) eq 'HASH'
54 && exists $data->{foo} && $data->{foo} eq 'z'
55 && exists $data->{bar} && $data->{bar} eq 't',
56 'private data creation with arguments ok');