]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/10-simple.t
70b4f6ef778f77556ff9dfdf470879159a0dcaed
[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 => 43;
7
8 use Variable::Magic qw<wizard 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 = 7;
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 $wiz = eval { wizard };
29 is($@, '',             'wizard doesn\'t croak');
30 ok(defined $wiz,       'wizard is defined');
31 is(ref $wiz, 'SCALAR', 'wizard is a scalar ref');
32
33 my $res = eval { cast $a, $wiz };
34 is($@, '', 'cast doesn\'t croak');
35 ok($res,   'cast is valid');
36
37 $res = eval { dispell $a, $wiz };
38 is($@, '', 'dispell from wizard doesn\'t croak');
39 ok($res,   'dispell from wizard is valid');
40
41 $res = eval { cast $a, $wiz };
42 is($@, '', 're-cast doesn\'t croak');
43 ok($res,   're-cast is valid');
44
45 $res = eval { dispell $a, \"blargh" };
46 like($@, $inv_wiz_obj, 're-dispell from wrong wizard croaks');
47 is($res, undef,        're-dispell from wrong wizard doesn\'t return anything');
48
49 $res = eval { dispell $a, undef };
50 like($@, $inv_wiz_obj, 're-dispell from undef croaks');
51 is($res, undef,        're-dispell from undef doesn\'t return anything');
52
53 $res = eval { dispell $a, $wiz };
54 is($@, '', 're-dispell from good wizard doesn\'t croak');
55 ok($res,   're-dispell from good wizard is valid');
56
57 $res = eval { dispell my $b, $wiz };
58 is($@, '',  'dispell non-magic object doesn\'t croak');
59 is($res, 0, 'dispell non-magic object returns 0');
60
61 my $c = 3;
62 $res = eval { cast $c, undef };
63 like($@, $inv_wiz_obj, 'cast from undef croaks');
64 is($res, undef,        'cast from undef doesn\'t return anything');