]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/18-opinfo.t
Fix t/18-opinfo.t failures with perl 5.13
[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;
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 $aelem     = $] <= 5.008003 ? 'aelem' : $] < 5.013 ? 'aelemfast' : 'sassign';
15 my $aelemf    = $] < 5.013 ? 'aelemfast' : 'sassign';
16 my $aelemf_op = $aelemf eq 'sassign'
17                    ? 'B::BINOP' : $Config{useithreads} ? 'B::PADOP' : 'B::SVOP';
18
19 our @o;
20
21 my @tests = (
22  [ 'len', '@c',    'my @c',     'my $x = @c',      [ 'padav',   'B::OP'     ] ],
23  [ 'get', '$c[0]', 'my @c',     'my $x = $c[0]',   [ $aelem,    'B::OP'     ] ],
24  [ 'get', '$o[0]', 'local @o',  'my $x = $o[0]',   [ $aelemf,   $aelemf_op  ] ],
25  [ 'get', '$c',    'my $c = 1', '++$c',            [ 'preinc',  'B::UNOP'   ] ],
26  [ 'get', '$c',    'my $c = 1', '$c ** 2',         [ 'pow',     'B::BINOP'  ] ],
27  [ 'get', '$c',    'my $c = 1', 'my $x = $c',      [ 'sassign', 'B::BINOP'  ] ],
28  [ 'get', '$c',    'my $c = 1', '1 if $c',         [ 'and',     'B::LOGOP'  ] ],
29  [ 'get', '$c',    'my $c = []','ref $c',          [ 'ref',     'B::UNOP'   ] ],
30  [ 'get', '$c',    'my $c = $0','-f $c',           [ 'ftfile',  'B::UNOP'   ] ],
31  [ 'get', '$c',    'my $c = "Z"',
32                    'my $i = 1; Z:goto $c if $i--', [ 'goto',    'B::UNOP'   ] ],
33  [ 'set', '$c',    'my $c = 1', 'bless \$c, "main"',
34                                                    [ 'bless',   'B::LISTOP' ] ],
35  [ 'get', '$c',    'my $c = ""','$c =~ /x/',       [ 'match',   'B::PMOP'   ] ],
36  [ 'get', '$c',    'my $c = "Variable::Magic::TestPkg"',
37                                 '$c->foo()',  [ 'method_named', 'B::SVOP'   ] ],
38  [ 'get', '$c',    'my $c = ""','$c =~ y/x/y/',    [ 'trans',   'B::PVOP'   ] ],
39  [ 'get', '$c',    'my $c = 1', '1 for 1 .. $c',
40                                                  [ 'enteriter', 'B::LOOP'   ] ],
41  [ 'free','$c',    'my $c = 1', 'last',            [ 'last',    'B::OP'     ] ],
42  [ 'free','$c', 'L:{my $c = 1', 'last L}',         [ 'last',    'B::OP'     ] ],
43 );
44
45 our $done;
46
47 for (@tests) {
48  my ($key, $var, $init, $test, $exp) = @$_;
49
50  for my $op_info (VMG_OP_INFO_NAME, VMG_OP_INFO_OBJECT) {
51   my $wiz;
52
53   # We must test for the $op correctness inside the callback because, if we
54   # bring it out, it will go outside of the eval STRING scope, and what it
55   # points to will no longer exist.
56   eval {
57    $wiz = wizard $key => sub {
58     return if $done;
59     my $op = $_[-1];
60     my $desc = "$key magic with op_info == $op_info";
61     if ($op_info == VMG_OP_INFO_NAME) {
62      is $op, $exp->[0], "$desc gets the right op info";
63     } elsif ($op_info == VMG_OP_INFO_OBJECT) {
64      isa_ok $op, $exp->[1], $desc;
65      is $op->name, $exp->[0], "$desc gets the right op info";
66     } else {
67      is $op, undef, "$desc gets the right op info";
68     }
69     $done = 1;
70     ()
71    }, op_info => $op_info
72   };
73   is $@, '', "$key wizard with op_info == $op_info doesn't croak";
74
75   local $done = 0;
76
77   my $testcase = "{ $init; cast $var, \$wiz; $test }";
78
79   eval $testcase;
80   is $@, '', "$key magic with op_info == $op_info doesn't croak";
81   diag $testcase if $@;
82  }
83 }
84
85 {
86  my $c;
87
88  my $wiz = eval {
89   wizard get => sub {
90     is $_[-1], undef, 'get magic with out of bounds op_info';
91    },
92    op_info => 3;
93  };
94  is $@, '', "get wizard with out of bounds op_info doesn't croak";
95
96  eval { cast $c, $wiz };
97  is $@, '', "get cast with out of bounds op_info doesn't croak";
98
99  eval { my $x = $c };
100  is $@, '', "get magic with out of bounds op_info doesn't croak";
101
102  eval { dispell $c, $wiz };
103  is $@, '', "get dispell with out of bounds op_info doesn't croak";
104 }