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