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