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