6 use Test::More tests => 12 + 9 * (5 + 6 + 5);
8 use Variable::Magic qw/wizard cast dispell VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
10 my $wiz = eval { wizard get => sub { undef } };
11 is($@, '', 'wizard creation doesn\'t croak');
12 ok(defined $wiz, 'wizard is defined');
13 is(ref $wiz, 'SCALAR', 'wizard is a scalar ref');
15 my $n = int rand 1000;
18 my $res = eval { cast $a, $wiz };
19 is($@, '', 'cast doesn\'t croak');
20 ok($res, 'cast is valid');
24 local $SIG{__WARN__} = sub { die };
27 is($@, '', 'callback returning undef doesn\'t warn/croak');
28 is($x, $n, 'callback returning undef fails');
31 $wiz = wizard get => sub {
34 while (@c = caller $i++) {
35 push @callers, [ @c[0, 1, 2] ];
43 is_deeply(\@callers, [
44 [ 'main', $0, __LINE__-2 ],
45 ], 'caller into callback returns the right thing');
49 is_deeply(\@callers, [
50 [ 'main', $0, __LINE__-2 ],
51 ], 'caller into callback returns the right thing (second time)');
56 is_deeply(\@callers, [
57 [ 'main', $0, __LINE__-2 ],
58 ], 'caller into callback into block returns the right thing');
63 is($@, '', 'caller into callback doesn\'t croak');
64 is_deeply(\@callers, [
65 ([ 'main', $0, __LINE__-3 ]) x 2,
66 ], 'caller into callback into eval returns the right thing');
68 sub Variable::Magic::TestPkg::foo { }
71 [ 'len', '@c', undef, 'my $x = @c', [ 'padav', 'B::OP' ] ],
72 [ 'get', '$c', undef, '++$c', [ 'preinc', 'B::UNOP' ] ],
73 [ 'get', '$c', '$c = 1', '$c ** 2', [ 'pow', 'B::BINOP' ] ],
74 [ 'get', '$c', undef, 'my $x = $c', [ 'sassign', 'B::BINOP' ] ],
75 [ 'get', '$c', undef, '1 if $c', [ 'and', 'B::LOGOP' ] ],
76 [ 'set', '$c', undef, 'bless \$c, "main"', [ 'bless', 'B::LISTOP' ] ],
77 [ 'get', '$c', '$c = ""', '$c =~ /x/', [ 'match', 'B::PMOP' ] ],
78 [ 'get', '$c', '$c = "Variable::Magic::TestPkg"',
79 '$c->foo()', [ 'method_named', 'B::SVOP' ] ],
80 [ 'get', '$c', '$c = ""', '$c =~ y/x/y/', [ 'trans', 'B::PVOP' ] ],
84 my ($key, $var, $init, $test, $exp) = @$_;
86 for my $op_info (VMG_OP_INFO_NAME, VMG_OP_INFO_OBJECT, 3) {
90 # We must test for the $op correctness inside the callback because, if we
91 # bring it out, it will go outside of the eval STRING scope, and what it
92 # points to will no longer exist.
94 $wiz = wizard $key => sub {
97 my $desc = "$key magic with op_info == $op_info";
99 is $op, $exp->[0], "$desc gets the right op info";
100 } elsif ($op_info == 2) {
101 isa_ok $op, $exp->[1], $desc;
102 is $op->name, $exp->[0], "$desc gets the right op info";
104 is $op, undef, "$desc gets the right op info";
108 }, op_info => $op_info
110 is $@, '', "$key wizard with op_info == $op_info doesn't croak";
112 eval $init if defined $init;
114 eval "cast $var, \$wiz";
115 is $@, '', "$key cast with op_info == $op_info doesn't croak";
119 is $@, '', "$key magic with op_info == $op_info doesn't croak";
121 eval "dispell $var, \$wiz";
122 is $@, '', "$key dispell with op_info == $op_info doesn't croak";