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