]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/12-data.t
Importing Variable-Magic-0.03.tar.gz
[perl/modules/Variable-Magic.git] / t / 12-data.t
1 #!perl -T
2
3 use Test::More tests => 19;
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 => $_[1] || 12, bar => $_[3] || 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 croaks ($@)");
21 ok($res, 'cast invalid');
22
23 my $data = eval { getdata $a, $wiz };
24 ok(!$@, "getdata croaks ($@)");
25 ok($res, 'getdata invalid');
26 ok($data && ref($data) eq 'HASH'
27          && exists $data->{foo} && $data->{foo} == 12
28          && exists $data->{bar} && $data->{bar} == 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 croaks ($@)");
41 ok($res, 'dispell invalid');
42
43 $res = eval { cast $a, $wiz, qw/z j t/ };
44 ok(!$@, "cast with arguments croaks ($@)");
45 ok($res, 'cast with arguments invalid');
46
47 $data = eval { getdata $a, $wiz };
48 ok(!$@, "getdata croaks ($@)");
49 ok($res, 'getdata invalid');
50 ok($data && ref($data) eq 'HASH'
51          && exists $data->{foo} && $data->{foo} eq 'z'
52          && exists $data->{bar} && $data->{bar} eq 't',
53    'private data creation with arguments ok');