]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/10-simple.t
Don't store the sig in the mg_private field
[perl/modules/Variable-Magic.git] / t / 10-simple.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 46;
7
8 use Variable::Magic qw/wizard gensig getsig cast dispell MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/;
9
10 my $args = 8;
11 ++$args if MGf_COPY;
12 ++$args if MGf_DUP;
13 ++$args if MGf_LOCAL;
14 $args += 5 if VMG_UVAR;
15 for (0 .. 20) {
16  next if $_ == $args;
17  eval { Variable::Magic::_wizard(('hlagh') x $_) };
18  like($@, qr/Wrong\s+number\s+of\s+arguments\s+at\s+\Q$0\E/, '_wizard called directly with a wrong number of arguments croaks');
19 }
20
21 for (0 .. 3) {
22  eval { wizard(('dong') x (2 * $_ + 1)) };
23  like($@, qr/Wrong\s+number\s+of\s+arguments\s+for\s+&?wizard\(\)\s+at\s+\Q$0\E/, 'wizard called with an odd number of arguments croaks');
24 }
25
26 my $sig = gensig;
27
28 my $wiz = eval { wizard sig => $sig };
29 is($@, '',             'wizard doesn\'t croak');
30 ok(defined $wiz,       'wizard is defined');
31 is(ref $wiz, 'SCALAR', 'wizard is a scalar ref');
32 is($sig, getsig $wiz,  'wizard signature is correct');
33
34 my $a = 1;
35 my $res = eval { cast $a, $wiz };
36 is($@, '', 'cast doesn\'t croak');
37 ok($res,   'cast is valid');
38
39 $res = eval { dispell $a, $wiz };
40 is($@, '', 'dispell from wizard doesn\'t croak');
41 ok($res,   'dispell from wizard is valid');
42
43 $res = eval { cast $a, $wiz };
44 is($@, '', 're-cast doesn\'t croak');
45 ok($res,   're-cast is valid');
46
47 $res = eval { dispell $a, gensig };
48 like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 're-dispell from wrong sig croaks');
49 is($res, undef, 're-dispell from wrong sig doesn\'t return anything');
50
51 $res = eval { dispell $a, undef };
52 like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 're-dispell from undef croaks');
53 is($res, undef, 're-dispell from undef doesn\'t return anything');
54
55 $res = eval { dispell $a, $sig };
56 is($@, '', 're-dispell from good sig doesn\'t croak');
57 ok($res,   're-dispell from good sig is valid');
58
59 $res = eval { dispell my $b, $wiz };
60 is($@, '',  'dispell non-magic object doesn\'t croak');
61 is($res, 0, 'dispell non-magic object returns 0');
62
63 $sig = gensig;
64 {
65  my $wiz = wizard sig => $sig;
66  my $b = 2;
67  my $res = cast $b, $wiz;
68 }
69 my $c = 3;
70 $res = eval { cast $c, $sig };
71 is($@, '',      'cast from obsolete signature doesn\'t croak');
72 is($res, undef, 'cast from obsolete signature returns undef');
73
74 $res = eval { cast $c, undef };
75 like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 'cast from undef croaks');
76 is($res, undef, 'cast from undef doesn\'t return anything');