]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/12-data.t
Importing Variable-Magic-0.01
[perl/modules/Variable-Magic.git] / t / 12-data.t
1 #!perl -T
2
3 use Test::More tests => 14;
4
5 use Variable::Magic qw/wizard getdata cast dispell/;
6
7 my $c = 1;
8
9 my $wiz = eval {
10  wizard data => sub { return { foo => 12, bar => 27 } },
11          get => sub { $c += $_[1]->{foo}; $_[1]->{foo} = $c },
12          set => sub { $c += $_[1]->{bar}; $_[1]->{bar} = $c }
13 };
14 ok(!$@, "wizard creation error ($@)");
15 ok(defined $wiz, 'wizard is defined');
16 ok(ref $wiz eq 'SCALAR', 'wizard is a scalar ref');
17
18 my $a = 75;
19 my $res = eval { cast $a, $wiz };
20 ok(!$@, "cast error 1 ($@)");
21 ok($res, 'cast error 2');
22
23 my $data = eval { getdata $a, $wiz };
24 ok(!$@, "getdata error 1 ($@)");
25 ok($res, 'getdata error 2');
26 ok($data && ref($data) eq 'HASH'
27          && exists $data->{foo} && $data->{foo} eq 12
28          && exists $data->{bar} && $data->{bar} eq 27,
29    'private data creation ok');
30
31 my $b = $a;
32 ok($c == 13, 'get magic : pass data');
33 ok($data->{foo} == 13, 'get magic : data updated');
34
35 $a = 57;
36 ok($c == 40, 'set magic : pass data');
37 ok($data->{bar} == 40, 'set magic : pass data');
38
39 $res = eval { dispell $a, $wiz };
40 ok(!$@, "dispell error 1 ($@)");
41 ok($res, 'dispell error 2');