]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/18-opinfo.t
Add support for ops of class UNOP_AUX
[perl/modules/Variable-Magic.git] / t / 18-opinfo.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 18 * (3 + 4) + 5 + 1;
7
8 use Config qw<%Config>;
9
10 use Variable::Magic qw<wizard cast dispell VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT>;
11
12 sub Variable::Magic::TestPkg::foo { }
13
14 my $is_5130_release = ("$]" == 5.013 && !$Config{git_describe}) ? 1 : 0;
15
16 my $aelem     = "$]" <= 5.008_003 ? 'aelem'
17                                   : ("$]" < 5.013 or $is_5130_release)
18                                                    ? 'aelemfast'
19                                                    : 'sassign';
20 my $aelemf    = ("$]" < 5.013 or $is_5130_release) ? 'aelemfast' : 'sassign';
21 my $aelemf_op = ($aelemf eq 'sassign')
22                    ? 'B::BINOP' : $Config{useithreads} ? 'B::PADOP' : 'B::SVOP';
23 my $meth_op   = ("$]" < 5.021_005) ? 'B::SVOP' : 'B::METHOP';
24 my $deref     = ("$]" < 5.021_007) ? 'helem' : 'multideref';
25 my $deref_op  = ($deref eq 'multideref') ? 'B::UNOP_AUX' : 'B::UNOP';
26
27 our @o;
28
29 my @tests = (
30  [ 'len', '@c',      'my @c',    'my $x = @c',     [ 'padav',   'B::OP'     ] ],
31  [ 'get', '$c[0]',   'my @c',    'my $x = $c[0]',  [ $aelem,    'B::OP'     ] ],
32  [ 'get', '$o[0]',   'local @o', 'my $x = $o[0]',  [ $aelemf,   $aelemf_op  ] ],
33  [ 'get', '$x->{a}', 'my $x',    'my $y = $x->{a}{b}',
34                                                    [ $deref,    $deref_op   ] ],
35  [ 'get', '$c',    'my $c = 1',  '++$c',           [ 'preinc',  'B::UNOP'   ] ],
36  [ 'get', '$c',    'my $c = 1',  '$c ** 2',        [ 'pow',     'B::BINOP'  ] ],
37  [ 'get', '$c',    'my $c = 1',  'my $x = $c',     [ 'sassign', 'B::BINOP'  ] ],
38  [ 'get', '$c',    'my $c = 1',  '1 if $c',        [ 'and',     'B::LOGOP'  ] ],
39  [ 'get', '$c',    'my $c = []', 'ref $c',         [ 'ref',     'B::UNOP'   ] ],
40  [ 'get', '$c',    'my $c = $0', '-f $c',          [ 'ftfile',  'B::UNOP'   ] ],
41  [ 'get', '$c',    'my $c = "Z"',
42                    'my $i = 1; Z:goto $c if $i--', [ 'goto',    'B::UNOP'   ] ],
43  [ 'set', '$c',    'my $c = 1',  'bless \$c, "main"',
44                                                    [ 'bless',   'B::LISTOP' ] ],
45  [ 'get', '$c',    'my $c = ""', '$c =~ /x/',      [ 'match',   'B::PMOP'   ] ],
46  [ 'get', '$c',    'my $c = "Variable::Magic::TestPkg"',
47                                  '$c->foo()', [ 'method_named', $meth_op    ] ],
48  [ 'get', '$c',    'my $c = ""', '$c =~ y/x/y/',   [ 'trans',   'B::PVOP'   ] ],
49  [ 'get', '$c',    'my $c = 1',  '1 for 1 .. $c',
50                                                  [ 'enteriter', 'B::LOOP'   ] ],
51  [ 'free','$c',    'my $c = 1',  'last',           [ 'last',    'B::OP'     ] ],
52  [ 'free','$c', 'L:{my $c = 1',  'last L}',        [ 'last',    'B::OP'     ] ],
53 );
54
55 our $done;
56
57 my $OP_INFO_NAME   = VMG_OP_INFO_NAME;
58 my $OP_INFO_OBJECT = VMG_OP_INFO_OBJECT;
59
60 for (@tests) {
61  my ($key, $var, $init, $test, $exp) = @$_;
62
63  for my $op_info ($OP_INFO_NAME, $OP_INFO_OBJECT) {
64   my $wiz;
65
66   # We must test for the $op correctness inside the callback because, if we
67   # bring it out, it will go outside of the eval STRING scope, and what it
68   # points to will no longer exist.
69   eval {
70    $wiz = wizard $key => sub {
71     return if $done;
72     my $op = $_[-1];
73     my $desc = "$key magic with op_info == $op_info";
74     if ($op_info == $OP_INFO_NAME) {
75      is $op, $exp->[0], "$desc gets the right op info";
76     } elsif ($op_info == $OP_INFO_OBJECT) {
77      isa_ok $op, $exp->[1], $desc;
78      is $op->name, $exp->[0], "$desc gets the right op info";
79     } else {
80      is $op, undef, "$desc gets the right op info";
81     }
82     $done = 1;
83     ()
84    }, op_info => $op_info
85   };
86   is $@, '', "$key wizard with op_info == $op_info doesn't croak";
87
88   local $done = 0;
89
90   my $testcase = "{ $init; cast $var, \$wiz; $test }";
91
92   eval $testcase;
93   is $@, '', "$key magic with op_info == $op_info doesn't croak";
94   diag $testcase if $@;
95  }
96 }
97
98 {
99  my $c;
100
101  my $wiz = eval {
102   wizard get => sub {
103     is $_[-1], undef, 'get magic with out of bounds op_info';
104    },
105    op_info => 3;
106  };
107  is $@, '', "get wizard with out of bounds op_info doesn't croak";
108
109  eval { cast $c, $wiz };
110  is $@, '', "get cast with out of bounds op_info doesn't croak";
111
112  eval { my $x = $c };
113  is $@, '', "get magic with out of bounds op_info doesn't croak";
114
115  eval { dispell $c, $wiz };
116  is $@, '', "get dispell with out of bounds op_info doesn't croak";
117 }
118
119 {
120  local $@;
121  my $wiz = eval {
122   local $SIG{__WARN__} = sub { die @_ };
123   wizard op_info => "hlagh";
124  };
125  like $@, qr/^Argument "hlagh" isn't numeric in subroutine entry at \Q$0\E/,
126       'wizard(op_info => "text") throws numeric warnings';
127 }