6 use Test::More tests => 19 * (3 + 4) + 5 + 1;
8 use Config qw<%Config>;
10 use Variable::Magic qw<wizard cast dispell VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT>;
12 sub Variable::Magic::TestPkg::foo { }
14 my $is_5130_release = ("$]" == 5.013 && !$Config{git_describe}) ? 1 : 0;
16 my $aelem = "$]" <= 5.008_003 ? 'aelem'
17 : ("$]" < 5.013 or $is_5130_release)
22 my $aelemf = ("$]" < 5.013 or $is_5130_release) ? 'aelemfast'
23 : ("$]" < 5.037_003) ? 'sassign'
26 my $assign_op = ("$]" < 5.037_003) ? 'sassign': 'padsv_store';
27 my $assign_op_cl = ("$]" < 5.037_003) ? 'B::BINOP': 'B::UNOP';
29 my $aelemf_op = ($aelemf eq 'sassign') ? 'B::BINOP'
30 : ($aelemf eq 'padsv_store') ? 'B::UNOP'
31 : $Config{useithreads} ? 'B::PADOP' : 'B::SVOP';
32 my $meth_op = ("$]" < 5.021_005) ? 'B::SVOP' : 'B::METHOP';
33 my $trutf_op = ($Config{useithreads} && "$]" >= 5.008_009)
34 ? 'B::PADOP' : 'B::SVOP';
35 my $deref = ("$]" < 5.021_007) ? 'helem' : 'multideref';
36 my $deref_op = ($deref eq 'multideref') ? 'B::UNOP_AUX' : 'B::UNOP';
41 [ 'len', '@c', 'my @c', 'my $x = @c', [ 'padav', 'B::OP' ] ],
42 [ 'get', '$c[0]', 'my @c', 'my $x = $c[0]', [ $aelem, 'B::OP' ] ],
43 [ 'get', '$o[0]', 'local @o', 'my $x = $o[0]', [ $aelemf, $aelemf_op ] ],
44 [ 'get', '$x->{a}', 'my $x', 'my $y = $x->{a}{b}',
45 [ $deref, $deref_op ] ],
46 [ 'get', '$c', 'my $c = 1', '++$c', [ 'preinc', 'B::UNOP' ] ],
47 [ 'get', '$c', 'my $c = 1', '$c ** 2', [ 'pow', 'B::BINOP' ] ],
48 [ 'get', '$c', 'my $c = 1', 'my $x = $c', [ $assign_op, $assign_op_cl ] ],
49 [ 'get', '$c', 'my $c = 1', '1 if $c', [ 'and', 'B::LOGOP' ] ],
50 [ 'get', '$c', 'my $c = []', 'ref $c', [ 'ref', 'B::UNOP' ] ],
51 [ 'get', '$c', 'my $c = $0', '-f $c', [ 'ftfile', 'B::UNOP' ] ],
52 [ 'get', '$c', 'my $c = "Z"',
53 'my $i = 1; Z:goto $c if $i--', [ 'goto', 'B::UNOP' ] ],
54 [ 'set', '$c', 'my $c = 1', 'bless \$c, "main"',
55 [ 'bless', 'B::LISTOP' ] ],
56 [ 'get', '$c', 'my $c = ""', '$c =~ /x/', [ 'match', 'B::PMOP' ] ],
57 [ 'get', '$c', 'my $c = "Variable::Magic::TestPkg"',
58 '$c->foo()', [ 'method_named', $meth_op ] ],
59 [ 'get', '$c', 'my $c = ""', '$c =~ y/x/y/', [ 'trans', 'B::PVOP' ] ],
60 [ 'get', '$c', 'my $c = ""', '$c =~ y/\x{100}//',
61 [ 'trans', $trutf_op ] ],
62 [ 'get', '$c', 'my $c = 1', '1 for 1 .. $c',
63 [ 'enteriter', 'B::LOOP' ] ],
64 [ 'free','$c', 'my $c = 1', 'last', [ 'last', 'B::OP' ] ],
65 [ 'free','$c', 'L:{my $c = 1', 'last L}', [ 'last', 'B::OP' ] ],
70 my $OP_INFO_NAME = VMG_OP_INFO_NAME;
71 my $OP_INFO_OBJECT = VMG_OP_INFO_OBJECT;
74 my ($key, $var, $init, $test, $exp) = @$_;
76 for my $op_info ($OP_INFO_NAME, $OP_INFO_OBJECT) {
79 # We must test for the $op correctness inside the callback because, if we
80 # bring it out, it will go outside of the eval STRING scope, and what it
81 # points to will no longer exist.
83 $wiz = wizard $key => sub {
86 my $desc = "$key magic with op_info == $op_info";
87 if ($op_info == $OP_INFO_NAME) {
88 is $op, $exp->[0], "$desc gets the right op info";
89 } elsif ($op_info == $OP_INFO_OBJECT) {
90 isa_ok $op, $exp->[1], $desc;
91 is $op->name, $exp->[0], "$desc gets the right op info";
93 is $op, undef, "$desc gets the right op info";
97 }, op_info => $op_info
99 is $@, '', "$key wizard with op_info == $op_info doesn't croak";
103 my $testcase = "{ $init; cast $var, \$wiz; $test }";
106 is $@, '', "$key magic with op_info == $op_info doesn't croak";
107 diag $testcase if $@;
116 is $_[-1], undef, 'get magic with out of bounds op_info';
120 is $@, '', "get wizard with out of bounds op_info doesn't croak";
122 eval { cast $c, $wiz };
123 is $@, '', "get cast with out of bounds op_info doesn't croak";
126 is $@, '', "get magic with out of bounds op_info doesn't croak";
128 eval { dispell $c, $wiz };
129 is $@, '', "get dispell with out of bounds op_info doesn't croak";
135 local $SIG{__WARN__} = sub { die @_ };
136 wizard op_info => "hlagh";
138 like $@, qr/^Argument "hlagh" isn't numeric in subroutine entry at \Q$0\E/,
139 'wizard(op_info => "text") throws numeric warnings';