]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/18-opinfo.t
Test loop ops
[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 => 12 * (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  [ 'get', '$c',    '$c = 1',  '1 for 1 .. $c',   [ 'enteriter', 'B::LOOP'   ] ],
33 );
34
35 for (@tests) {
36  my ($key, $var, $init, $test, $exp) = @$_;
37
38  for my $op_info (VMG_OP_INFO_NAME, VMG_OP_INFO_OBJECT) {
39   our $done;
40   my ($c, @c);
41   my $wiz;
42
43   # We must test for the $op correctness inside the callback because, if we
44   # bring it out, it will go outside of the eval STRING scope, and what it
45   # points to will no longer exist.
46   eval {
47    $wiz = wizard $key => sub {
48     return if $done;
49     my $op = $_[-1];
50     my $desc = "$key magic with op_info == $op_info";
51     if ($op_info == VMG_OP_INFO_NAME) {
52      is $op, $exp->[0], "$desc gets the right op info";
53     } elsif ($op_info == VMG_OP_INFO_OBJECT) {
54      isa_ok $op, $exp->[1], $desc;
55      is $op->name, $exp->[0], "$desc gets the right op info";
56     } else {
57      is $op, undef, "$desc gets the right op info";
58     }
59     $done = 1;
60     ()
61    }, op_info => $op_info
62   };
63   is $@, '', "$key wizard with op_info == $op_info doesn't croak";
64
65   local $done = 0;
66
67   eval $init if defined $init;
68
69   eval "cast $var, \$wiz";
70   is $@, '', "$key cast with op_info == $op_info doesn't croak";
71
72   eval $test;
73   is $@, '', "$key magic with op_info == $op_info doesn't croak";
74
75   eval "dispell $var, \$wiz";
76   is $@, '', "$key dispell with op_info == $op_info doesn't croak";
77  }
78 }
79
80 {
81  my $c;
82
83  my $op_info = VMG_OP_INFO_OBJECT;
84  my $wiz = eval {
85   wizard free => sub {
86     my $op = $_[-1];
87     my $desc = "free magic with op_info == $op_info";
88     isa_ok $op, 'B::OP', $desc;
89     is $op->name, 'leaveloop', "$desc gets the right op info";
90     ();
91    }, op_info => $op_info;
92  };
93  is $@, '', "get wizard with out of bounds op_info doesn't croak";
94
95  eval { cast $c, $wiz };
96  is $@, '', "get cast with out of bounds op_info doesn't croak";
97 }
98
99 {
100  my $c;
101
102  my $wiz = eval {
103   wizard get => sub {
104     is $_[-1], undef, 'get magic with out of bounds op_info';
105    },
106    op_info => 3;
107  };
108  is $@, '', "get wizard with out of bounds op_info doesn't croak";
109
110  eval { cast $c, $wiz };
111  is $@, '', "get cast with out of bounds op_info doesn't croak";
112
113  eval { my $x = $c };
114  is $@, '', "get magic with out of bounds op_info doesn't croak";
115
116  eval { dispell $c, $wiz };
117  is $@, '', "get dispell with out of bounds op_info doesn't croak";
118 }