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