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