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