]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - t/23-magic-uvar.t
ce80f2ec95ccb0cf2e3ac8827079bb71a41dd05a
[perl/modules/Lexical-Types.git] / t / 23-magic-uvar.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9  plan skip_all => 'Variable::Magic 0.35 on 5.10 required to test uvar magic'
10               unless eval "use Variable::Magic 0.35; Variable::Magic::VMG_UVAR";
11 }
12
13 {
14  package Lexical::Types::Test::Ref;
15
16  use Variable::Magic qw<wizard cast>;
17
18  our $wiz;
19  BEGIN {
20   $wiz = wizard data  => sub { +{ } },
21                 fetch => sub { ++$_[1]->{fetch}; () },
22                 store => sub { ++$_[1]->{store}; () };
23  }
24
25  sub TYPEDSCALAR {
26   my %h = ("_\L$_[2]" => (caller(0))[2]);
27   cast %h, $wiz;
28   $_[1] = \%h;
29   ();
30  }
31 }
32
33 { package Ref; }
34
35 BEGIN {
36  plan tests => 2 * 11;
37  defined and diag "Using Variable::Magic $_" for $Variable::Magic::VERSION;
38 }
39
40 use Lexical::Types as => 'Lexical::Types::Test';
41
42 sub check (&$$;$) {
43  my $got = Variable::Magic::getdata(%{$_[1]}, $Lexical::Types::Test::Ref::wiz);
44  my ($test, $exp, $desc) = @_[0, 2, 3];
45  my $want = wantarray;
46  my @ret;
47  {
48   local @{$got}{qw<fetch store>};
49   delete @{$got}{qw<fetch store>};
50   if ($want) {
51    @ret = eval { $test->() };
52   } elsif (defined $want) {
53    $ret[0] = eval { $test->() };
54   } else {
55    eval { $test->() };
56   }
57   local $Test::Builder::Level = $Test::Builder::Level + 1;
58   is_deeply $got, $exp, $desc;
59  }
60  return $want ? @ret : $ret[0];
61 }
62
63 for (1 .. 2) {
64  my Ref $x; my $lx = __LINE__;
65
66  my $y = check { $x->{_ref} } $x, { fetch => 1 }, 'fetch';
67  is $y, $lx, 'fetch correctly';
68
69  check { $x->{wat} = __LINE__ } $x, { store => 1 }, 'store'; my $l0 = __LINE__;
70  is $x->{wat}, $l0, 'store correctly';
71
72  my Ref $z = $x; my $lz = __LINE__;
73
74  $y = check { $x->{_ref} } $x, { fetch => 1 }, 'fetch after being assigned';
75  is $y, $lx, 'fetch after being assigned correctly';
76
77  $y = check { $z->{_ref} } $z, { fetch => 1 }, 'fetch after being assigned to';
78  is $y, $lx, 'fetch after being assigned to correctly';
79
80  check { $z->{wat} = $x->{wat} + __LINE__ - $l0 } $z, { fetch => 1, store => 1 }, 'fetch/store';
81  is $z->{wat}, __LINE__-1, 'fetch/store correctly';
82  is $x->{wat}, __LINE__-2, 'fetch/store correctly';
83 }